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

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


当前回答

当你在你想要更改密码的系统上使用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.

其他回答

正如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).

你可以尝试以下步骤来重置mysql的root密码:

首先停止Mysql服务

sudo /etc/init.d/mysql stop 

以root用户登录,不需要密码 Sudo mysqld_safe—skip-grant-tables &

登录mysql终端后,你需要执行更多的命令:

use mysql;




UPDATE mysql.user SET authentication_string=PASSWORD('solutionclub3@*^G'), plugin='mysql_native_password' WHERE User='root';


flush privileges;


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

重启mysql服务器后 如果你仍然面临错误,你必须访问: 重置MySQL的root密码

对我有用的(Ubuntu 16.04, mysql 5.7):

停止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

然后:

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

关闭MySQL。

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

正常启动MySQL服务。

sudo service mysql start

我遇到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;

如果部署在xampp上,您可以通过提供的phpadmin gui轻松更改mysql密码。

phpMyAdmin -> User Accounts -> Edit Privileges (Select the intended user) -> Change Password (Tab)