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

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


当前回答

对于mysql 5.6,这个命令有效,你可以通过向导设置密码:

sudo dpkg-reconfigure mysql-server-5.6

其他回答

我分享一步一步的最终解决方案,重置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;

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

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

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

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

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

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

对于Ubuntu 18.04和mysql版本14.14,Distrib 5.7.22,请按照以下步骤重置mysql密码。

步骤1

sudo systemctl stop mysql

步骤2

sudo systemctl edit mysql

这个命令将在nano编辑器中打开一个新文件,您将使用该文件编辑MySQL的服务覆盖。这些更改MySQL的默认服务参数。该文件将为空,因此添加以下内容:

[Service]
ExecStart=
ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid --skip-grant-tables --skip-networking

步骤3

sudo systemctl daemon-reload
sudo systemctl start mysql

步骤4

sudo mysql -u root

步骤5

FLUSH PRIVILEGES;

步骤6

UPDATE mysql.user SET authentication_string = PASSWORD('new_password') WHEREuser = 'root';

步骤7

UPDATE mysql.user SET plugin ='mysql_native_password' WHERE user = 'root';

步骤8

sudo systemctl revert mysql

最后

sudo systemctl restart mysql

现在享受