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

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


当前回答

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密码在Linux Ubuntu。

参考来自博客(dbrnd.com)

步骤1: 停止MySQL服务。

sudo stop mysql

步骤2: 关闭所有运行mysqld。

sudo killall -9 mysqld

步骤3: 以安全模式启动mysqld。

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

步骤4: 启动mysql客户端

mysql -u root

步骤5: 登录成功后,如果需要修改密码,请执行该命令。

FLUSH PRIVILEGES;

步骤6: 您可以更新mysql root密码。

UPDATE mysql.user SET Password=PASSWORD('newpwd') WHERE User='root';

注意:在MySQL 5.7中,Password列被称为authentication_string。

第七步: 请执行该命令。

FLUSH PRIVILEGES;

您可以使用该命令:

UPDATE mysql.user SET Password=PASSWORD('newpwd') WHERE User='root';

之后请冲洗:

FLUSH PRIVILEGES;

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 8.0指南中推荐的官方步骤,这是唯一适合我的步骤。下面是这些步骤的总结。

Stop the MySQL server if it is running. Look in /var/lib/mysql/, /var/run/mysqld/, or /usr/local/mysql/data/ to find the pid file with the server's process ID. Generally the file begins with either mysqld or your system's host name and ends with .pid. Replace mysql-data-directory and host_name that you just found, in the following command: $ sudo kill `sudo cat /mysql-data-directory/host_name.pid` This command will create a text file in /tmp/mysql-init with the SQL statement and makes the mysql user the owner. Replace in the command MyNewPass with your own password. $ echo "ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';" > /tmp/mysql-init && sudo chown mysql /tmp/mysql-init Start the MySQL server by running the following command on the command line. After this the password is updated and you can close the server again with CTRL+C. $ sudo mysqld --user=mysql --init-file=/tmp/mysql-init & Remove the temporary file with your password: $ sudo rm /tmp/mysql-init

首先执行以下命令:

sudo mysql

然后你应该检查你的MySQL用户帐户使用的身份验证方法。运行这个命令

SELECT user,authentication_string,plugin,host FROM mysql.user;

现在你可以看到这样的东西:

+------------------+-------------------------------------------+-----------------------+-----------+
| user             | authentication_string                     | plugin                | host      |
+------------------+-------------------------------------------+-----------------------+-----------+
| root             |                                           | auth_socket           | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+

在上面的表格中,你可以看到你所有的mysql用户的帐户状态&如果你已经设置了root帐户的密码,然后在插件列中看到mysql_native_password而不是auth_socket。 总之,要更改root密码,您应该运行以下命令:

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

请务必将密码更改为您选择的强密码。 然后重新加载你的服务器,把你的新变化生效运行这个;

FLUSH PRIVILEGES;

所以再次检查你的mysql所使用的认证方法,通过下面的命令:

SELECT user,authentication_string,plugin,host FROM mysql.user;

现在输出是:

+------------------+-------------------------------------------+-----------------------+-----------+
| user             | authentication_string                     | plugin                | host      |
+------------------+-------------------------------------------+-----------------------+-----------+
| root             | *3636DACC8616D997782ADD0839F92C1571D6D78F | mysql_native_password | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+

正如您在授权表中看到的,您的根帐户具有mysql_native_password . 现在你可以退出MYSQL shell了

exit;

就是这样。只是你应该通过sudo service mysql restart重新启动mysql。 现在你可以用root帐号和密码轻松登录mysql。