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

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


当前回答

我遇到ubuntu 18.04和mysql 5.7的问题,这是解决方案

执行命令前请尝试重新启动mysql-server

sudo service mysql restart

Mysql-server >= 5.7

sudo mysql -uroot -p
USE mysql;
UPDATE user SET authentication_string=PASSWORD('YOUR_PASSWORD') WHERE User='root';
UPDATE user SET plugin="mysql_native_password";
FLUSH PRIVILEGES;
quit;

Mysql-server < 5.7

sudo mysql -uroot -p
USE mysql;
UPDATE user SET password=PASSWORD('YOUR_PASSWORD') WHERE User='root';
UPDATE user SET plugin="mysql_native_password";
FLUSH PRIVILEGES;
quit;

其他回答

这就像我为Ubuntu 16.04做的一样。 完全归功于下面的链接,因为我从那里得到了它。 [https://coderwall.com/p/j9btlg/reset-the-mysql-5-7-root-password-in-ubuntu-16-04-lts] [1]

停止MySQL

sudo service mysql stop

制作MySQL服务目录。 Sudo mkdir /var/run/mysqld

赋予MySQL用户写入服务目录的权限。

sudo chown mysql: /var/run/mysqld

手动启动MySQL,不需要权限检查或联网。

sudo mysqld_safe --skip-grant-tables --skip-networking &

无需密码即可登录。

mysql -uroot mysql

更新root用户密码。 确保下面的查询至少更新了根帐户。 如果您愿意,进行一些选择并检查现有的值

UPDATE mysql.user SET 
authentication_string=PASSWORD('YOURNEWPASSWORD'), 
plugin='mysql_native_password' WHERE User='root';
EXIT;

关闭MySQL。

sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown

正常启动MySQL服务。

sudo service mysql start

关于这个话题的大多数答案都过时了;在写这个答案之前,MySQL发生了两个主要的变化:

1-用户表中的“Password”字段已被“authentication_string”列取代。

2-“Password”加密功能:Password(“of some text”)已弃用。

更多信息请参考此链接:dev.mysql.com/doc/refman/8.0/en/resetting-permissions.html

Instead of resetting the password there is a work around on the local machine if you have setup phpmyadmin to connect without giving the password or username. Check this out by starting mysql, apache etc. I have xampp installed in my local machine. So starting the xampp will start all the necessary services. Now going to http://localhost/phpmyadmin shows me all the databases. This confirms that you have saved the username and passsword in the config file of phpmyadmin which can be found in the phpmyadmin install location. If you have xampp installed the phpmyadmin folder can be found in the root folder of xampp installation. Search for the word password in the config.inc.php file. There you will find the password and username.

你不需要这些。只需登录:

Mysql -u root -p

然后按照mysql>提示修改当前用户的密码:

mysql> set password=password('the_new_password');
mysql> flush privileges;

修改MySQL root密码。

此方法将密码暴露到命令行历史记录中,这些命令应该以root身份运行。

通过mysql命令行工具登录: Mysql -uroot -poldpassword 执行如下命令: SET PASSWORD FOR root@ localhost = PASSWORD('newpassword');

or

运行此命令,为当前用户设置密码(在本例中为'root'): SET PASSWORD = PASSWORD('newpassword');