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

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


当前回答

对我有用的(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

其他回答

要更新“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 root密码。用更简单的方式

所有这些命令都应该以root用户身份运行。

使用旧密码登录MySQL命令行工具:

步骤1

mysql -uroot -p"your_old_password"

然后执行以下命令:

步骤2

SET PASSWORD FOR root@'localhost' = PASSWORD('your_new_password');

方法-2(使用上述命令首次使用旧密码登录)

为当前用户设置密码:

SET PASSWORD = PASSWORD('your_new_password');

以上命令用于当前用户。如果您要更改其他用户的密码,可以输入用户名而不是“root”。

当你在你想要更改密码的系统上使用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服务器。 打开ubuntu终端。 登录mysql使用:

mysql - username -p

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

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

然后使用:

> flush privileges;

然后你就完成了。

该解决方案属于以前版本的MySQL。 通过使用套接字身份验证登录MySQL,您可以做到这一点。

sudo mysql -u root

然后可以运行以下命令。

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

详情请点击这里。