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

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


当前回答

我还没有看到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

其他回答

当你在你想要更改密码的系统上使用MySQL的PASSWORD()时,它可能会导致密码在MySQL日志中以cleartext [source]显示。对我来说,让它们和备份等像密码一样安全听起来像是噩梦,所以我喜欢这样做:

On your local machine, run this with your password: mysql -u someuser -p < <(echo "SELECT PASSWORD('mypass');") Note the space in front to prevent it from turning up in the bash history (for other distros than Ubuntu, this might work differently – source). On your server machine, execute the following command to change its MySQL root password (replace myhash with your password's hash as printed by the first command): mysql -u root -p < <(echo "SET PASSWORD FOR root@localhost = 'myhash';") Optionally, let's be a bit paranoid: On your local machine, clear your terminal screen with clear and purge your virtual terminal scrollback, to hide the cleartext password appearing in the command above.

重置或修改密码请输入sudo dpkg-reconfigure mysql-server-X。X (X.X是mysql版本你已经安装,即5.6,5.7),然后你会提示一个屏幕,你必须设置新密码,然后在下一步确认密码,只是等待一会儿。就是这样。

官方和简单的方法来重置根密码在ubuntu服务器…

如果你在16.04 14.04 12.04

sudo dpkg-reconfigure mysql-server-5.5

如果你在10.04:

sudo dpkg-reconfigure mysql-server-5.1

如果您不确定安装了哪个mysql-server版本,您可以尝试:

dpkg --get-selections | grep mysql-server

mysql-server-5.7的更新说明

请注意,如果您使用的是mysql-server-5.7,则不能使用上面所示的更简单的dpkg-reconfigure方法。

如果你知道密码,登录并运行这个:

UPDATE mysql.user SET authentication_string=PASSWORD('my-new-password') WHERE USER='root';
FLUSH PRIVILEGES;

或者,您可以使用以下方法:

sudo mysql_secure_installation

这将询问您一系列关于保护安装的问题(强烈推荐),包括是否需要提供新的根密码。

如果你不知道root密码,请参考这个以ubuntu为中心的进程写上去。

查看更多信息:

https://help.ubuntu.com/16.04/serverguide/mysql.html https://help.ubuntu.com/14.04/serverguide/mysql.html

正如mysql文档中password()函数所说:

此函数在MySQL 8.0.11中被移除。

这将使mysql v8.0.11及更新版本的所有现有答案都失效。

根据mysql文档,重置root密码的新通用方法如下:

The preceding sections provide password-resetting instructions specifically for Windows and Unix and Unix-like systems. Alternatively, on any platform, you can reset the password using the mysql client (but this approach is less secure): Stop the MySQL server if necessary, then restart it with the --skip-grant-tables option. This enables anyone to connect without a password and with all privileges, and disables account-management statements such as ALTER USER and SET PASSWORD. Because this is insecure, if the server is started with the --skip-grant-tables option, it enables --skip-networking automatically to prevent remote connections. Connect to the MySQL server using the mysql client; no password is necessary because the server was started with --skip-grant-tables: shell> mysql In the mysql client, tell the server to reload the grant tables so that account-management statements work: mysql> FLUSH PRIVILEGES; Then change the 'root'@'localhost' account password. Replace the password with the password that you want to use. To change the password for a root account with a different host name part, modify the instructions to use that host name. mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass'; You should now be able to connect to the MySQL server as root using the new password. Stop the server and restart it normally (without the --skip-grant-tables and --skip-networking options).

在我的例子中,这个选项很有用:https://stackoverflow.com/a/49610152/13760371 谢谢你,拉胡尔。

除了下面的时刻,当我尝试输入命令:

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

控制台发出警告:

1681 'password' is deprecated and will be removed in a future release

用这个命令治愈:

UPDATE mysql.user SET authentication_string=CONCAT('*', UPPER(SHA1(UNHEX(SHA1('NEWPASSWORD'))))), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';

MySQL版本5.7.X

我的版本:

1. > sudo service mysql stop
2. > sudo mkdir /var/run/mysqld
3. > sudo chown mysql: /var/run/mysqld
4. > sudo mysqld_safe --skip-grant-tables --skip-networking &
5. > mysql -uroot mysql
6. > UPDATE mysql.user SET authentication_string=CONCAT('*', UPPER(SHA1(UNHEX(SHA1('NEWPASSWORD'))))), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';
7. > \q;
8. > sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown
9. > sudo service mysql start