如何修改ubuntu服务器的MySQL root密码和用户名?我需要停止mysql服务之前,设置任何更改?

我有一个phpmyadmin设置以及,phpmyadmin会自动更新?


当前回答

下面的步骤对我很有效。我使用MySQL 8。*在Ubuntu上

停止MySQL服务,检查状态确认服务已停止

Sudo systemctl停止mysql Sudo systemctl status mysql

编辑systemd配置文件,这样您就可以在没有权限检查的情况下访问MySQL Sudo systemctl编辑mysql 复制并粘贴以下3行

(服务) ExecStart = ExecStart=/usr/sbin/mysqld——skip-grant-tables——skip-networking

粘贴行后按CTRL+0保存,然后按CTRL+X退出

重新加载mysql服务并启动它(使用——skip-grant-table启动服务)

Sudo systemctl daemon-reload Sudo systemctl启动mysql

5.现在无需密码即可连接MySQL服务器

sudo mysql -u root

6.通过运行加载授权表

mysql> FLUSH PRIVILEGES;

重置root密码

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourPasswordHere';

关闭mysql连接

mysql >退出

恢复对mysql systemd文件所做的修改

Sudo systemctl恢复mysql

重新加载mysql守护进程以进行更改。

Sudo systemctl daemon-reload

最后重新启动MySQL服务

Sudo systemctl restart mysql

现在用第7步设置的新密码连接mysql

你可以访问这个链接重置mysql 8的根密码了解更多细节。

其他回答

如果你知道你当前的密码,你不需要停止mysql服务器。 打开ubuntu终端。 登录mysql使用:

mysql - username -p

然后输入密码。 这将带您进入mysql控制台。 在控制台内部,输入:

> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

然后使用:

> flush privileges;

然后你就完成了。

唯一对我有效的方法是这里描述的(我运行的是ubuntu 14.04)。为了清晰起见,以下是我遵循的步骤:

sudo vim /etc/mysql/my.cnf Add the following lines at the end: [mysqld] skip-grant-tables sudo service mysql restart mysql -u root use mysql select * from mysql.user where user = 'root'; - Look at the top to determine whether the password column is called password or authentication_string UPDATE mysql.user set *password_field from above* = PASSWORD('your_new_password') where user = 'root' and host = 'localhost'; - Use the proper password column from above FLUSH PRIVILEGES; exit sudo vim /etc/mysql/my.cnf Remove the lines added in step 2 if you want to keep your security standards. sudo service mysql restart

参考:https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html

I had to go this route on Ubuntu 16.04.1 LTS. It is somewhat of a mix of some of the other answers above - but none of them helped. I spent an hour or more trying all other suggestions from MySql website to everything on SO, I finally got it working with: Note: while it showed Enter password for user root, I didnt have the original password so I just entered the same password to be used as the new password. Note: there was no /var/log/mysqld.log only /var/log/mysql/error.log Also note this did not work for me: sudo dpkg-reconfigure mysql-server-5.7 Nor did: sudo dpkg-reconfigure --force mysql-server-5.5 Make MySQL service directory. sudo mkdir /var/run/mysqld Give MySQL user permission to write to the service directory. sudo chown mysql: /var/run/mysqld Then:

kill the current mysqld pid run mysqld with sudo /usr/sbin/mysqld & run /usr/bin/mysql_secure_installation Output from mysql_secure_installation root@myServer:~# /usr/bin/mysql_secure_installation Securing the MySQL server deployment. Enter password for user root: VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin? Press y|Y for Yes, any other key for No: no Using existing password for root. Change the password for root ? ((Press y|Y for Yes, any other key for No) : y New password: Re-enter new password: By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? (Press y|Y for Yes, any other key for No) : y Success. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y Success. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y Dropping test database... Success. Removing privileges on test database... Success. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y Success. All done!

要更新“root”Mysql用户密码,你必须记住,你将需要超级用户权限。如果您拥有超级用户权限,请尝试执行以下命令:

MySQL 5.7.6及以上版本

sudo su
service mysql stop
mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
\q;
exit
mysql -u root -p MyNewPass

MySQL 5.7.5及更早版本

sudo su
service mysql stop
mysql -u root
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');
\q;
exit
mysql -u root -p MyNewPass

设置MySQL密码

停止MySQL数据库服务

sudo /etc/init.d/mysql stop

创建一个新的mysqld目录

Sudo mkdir /var/run/mysqld/

给予mysql用户访问权限

Sudo chown mysql /var/run/mysqld/

以安全模式启动MySQL…

Sudo mysqld_safe—skip-grant-tables &

无需密码登录数据库服务器

Sudo mysql -u root

使用默认的mysql数据库

使用mysql;

修改root密码

update user set authentication_string=PASSWORD(" new_passwor_here ") where user ='root';

保存更改 冲洗特权; 退出; 停止MySQL安全模式,启动MySQL默认服务

sudo /etc/init.d/mysql stop

sudo /etc/init.d/mysql start

使用root新密码重新登录MySQL数据库

-u root -p

***来源:https://websiteforstudents.com/resetting-mysql-root-password-on-ubuntu-16-04-17-10-and-18-04-lts/