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

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


该任务指导用户在Ubuntu操作系统下设置/修改/重置MySQL root密码。在终端中输入以下行。

停止MySQL服务器:sudo /etc/init.d / mysql停止 (在某些情况下,如果/var/run/mysqld不存在,你必须首先创建它:sudo mkdir -v /var/run/mysqld && sudo chown mysql /var/run/mysqld 启动mysqld配置:sudo mysqld——skip-grant-tables & 以root身份登录MySQL: MySQL -u root MySQL 用你的新密码替换YOURNEWPASSWORD:

MySQL < 8.0

UPDATE mysql.user SET Password = PASSWORD('YOURNEWPASSWORD') WHERE User = 'root';
FLUSH PRIVILEGES;

如果你的MySQL使用新的认证插件,你将需要使用:update user set plugin="mysql_native_password" where user ='root';在冲洗特权之前。

注意:在某些版本,如果密码列不存在,你可能想尝试: UPDATE user SET authentication_string=password('YOURNEWPASSWORD') WHERE user='root';

注意:这种方法不是最安全的重置密码的方法,但它是有效的。

对于MySQL >= 8.0

FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'YOURNEWPASSWORD';
FLUSH PRIVILEGES;

最后一步:

正如@lambart的评论中所提到的,你可能需要杀死你启动的临时无密码mysql进程,即sudo killall -9 mysqld,然后启动正常的守护进程:sudo service mysql start

引用:

该任务指导用户在Ubuntu Linux下设置/修改/重置MySQL root密码 如何重置Root用户密码(v5.6) 如何重置Root用户密码(v8.0)


修改MySQL root密码。

此方法将密码暴露到命令行历史记录中,这些命令应该以root身份运行。

通过mysql命令行工具登录: Mysql -uroot -poldpassword 执行如下命令: SET PASSWORD FOR root@ localhost = PASSWORD('newpassword');

or

运行此命令,为当前用户设置密码(在本例中为'root'): SET PASSWORD = PASSWORD('newpassword');


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


官方和简单的方法来重置根密码在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


唯一对我有效的方法是这里描述的(我运行的是ubuntu 14.04)。为了清晰起见,以下是我遵循的步骤:

sudo vim /etc/mysql/my.cnf Add the following lines at the end: [mysqld] skip-grant-tables sudo service mysql restart mysql -u root use mysql select * from mysql.user where user = 'root'; - Look at the top to determine whether the password column is called password or authentication_string UPDATE mysql.user set *password_field from above* = PASSWORD('your_new_password') where user = 'root' and host = 'localhost'; - Use the proper password column from above FLUSH PRIVILEGES; exit sudo vim /etc/mysql/my.cnf Remove the lines added in step 2 if you want to keep your security standards. sudo service mysql restart

参考:https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html


我分享一步一步的最终解决方案,重置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 root密码,在终端输入:

Sudo dpkg-reconfigure mysql-server-5.5

MySQL守护进程将被停止,并提示您输入新密码。


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.


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

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

您可以使用该命令:

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

之后请冲洗:

FLUSH PRIVILEGES;

当更改/重置MySQL密码时,上面列出的以下命令没有帮助。我发现进入终端并使用这些命令是毫无意义的。相反,使用命令sudo停止一切。如果有帮助的话,删除windows的system32。


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

sudo dpkg-reconfigure mysql-server-5.6

如果你知道你当前的密码,你不需要停止mysql服务器。 打开ubuntu终端。 登录mysql使用:

mysql - username -p

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

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

然后使用:

> flush privileges;

然后你就完成了。


对我有用的(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 16.04做的一样。 完全归功于下面的链接,因为我从那里得到了它。 [https://coderwall.com/p/j9btlg/reset-the-mysql-5-7-root-password-in-ubuntu-16-04-lts] [1]

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

更新root用户密码。 确保下面的查询至少更新了根帐户。 如果您愿意,进行一些选择并检查现有的值

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

关闭MySQL。

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

正常启动MySQL服务。

sudo service mysql start

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


回应rogerdpack的评论:如果你不知道MySQL的根密码,并且你不关心MySQL的数据/设置,你可以重新安装它并重置根密码,如下所示:

sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo rm -rf /var/lib/mysql
sudo apt-get install -y mysql-server mysql-client 

安装过程中,您可以选择root用户的密码:


I had to go this route on Ubuntu 16.04.1 LTS. It is somewhat of a mix of some of the other answers above - but none of them helped. I spent an hour or more trying all other suggestions from MySql website to everything on SO, I finally got it working with: Note: while it showed Enter password for user root, I didnt have the original password so I just entered the same password to be used as the new password. Note: there was no /var/log/mysqld.log only /var/log/mysql/error.log Also note this did not work for me: sudo dpkg-reconfigure mysql-server-5.7 Nor did: sudo dpkg-reconfigure --force mysql-server-5.5 Make MySQL service directory. sudo mkdir /var/run/mysqld Give MySQL user permission to write to the service directory. sudo chown mysql: /var/run/mysqld Then:

kill the current mysqld pid run mysqld with sudo /usr/sbin/mysqld & run /usr/bin/mysql_secure_installation Output from mysql_secure_installation root@myServer:~# /usr/bin/mysql_secure_installation Securing the MySQL server deployment. Enter password for user root: VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin? Press y|Y for Yes, any other key for No: no Using existing password for root. Change the password for root ? ((Press y|Y for Yes, any other key for No) : y New password: Re-enter new password: By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? (Press y|Y for Yes, any other key for No) : y Success. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y Success. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y Dropping test database... Success. Removing privileges on test database... Success. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y Success. All done!


要更新“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 -u root -p

然后按照mysql>提示修改当前用户的密码:

mysql> set password=password('the_new_password');
mysql> flush privileges;

你可以尝试以下步骤来重置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密码


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

5.无需密码登录。 Mysql -uroot Mysql

6.更新root用户密码。

更新mysql。user SET authentication_string=PASSWORD('YOURNEWPASSWORD'), plugin='mysql_native_password' WHERE user ='root' AND Host='%'; 退出;

关闭MySQL。 sudo mysqladmin -S /var/run/mysqld/mysqld.袜子关闭 正常启动MySQL服务。 Sudo服务mysql启动


我遇到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。 通过使用套接字身份验证登录MySQL,您可以做到这一点。

sudo mysql -u root

然后可以运行以下命令。

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

详情请点击这里。


正如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工作: https://stackoverflow.com/a/46076838/2400373

但是重要的是最后一步的改变:

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

修改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”。


对于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

现在享受


设置MySQL密码

停止MySQL数据库服务

sudo /etc/init.d/mysql stop

创建一个新的mysqld目录

Sudo mkdir /var/run/mysqld/

给予mysql用户访问权限

Sudo chown mysql /var/run/mysqld/

以安全模式启动MySQL…

Sudo mysqld_safe—skip-grant-tables &

无需密码登录数据库服务器

Sudo mysql -u root

使用默认的mysql数据库

使用mysql;

修改root密码

update user set authentication_string=PASSWORD(" new_passwor_here ") where user ='root';

保存更改 冲洗特权; 退出; 停止MySQL安全模式,启动MySQL默认服务

sudo /etc/init.d/mysql stop

sudo /etc/init.d/mysql start

使用root新密码重新登录MySQL数据库

-u root -p

***来源:https://websiteforstudents.com/resetting-mysql-root-password-on-ubuntu-16-04-17-10-and-18-04-lts/


如果你知道“root”用户密码,请使用该凭证登录mysql。然后执行以下查询以更新密码。

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_passowrd';

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


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

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

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

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


在我的例子中,这个选项很有用: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

1.打开nano / vim,创建一个包含以下内容的文件,并将文件保存为~/mysql-pwd

ALTER USER 'root'@'localhost' IDENTIFIED BY 'NEWPASSWORD';

停止mysql sudo systemctl停止mysql 执行sudo mysqld -init-file=~/mysql-pwd命令 重启mysql sudo systemctl start mysql 现在登录mysql -u root -p。password将是你的NEWPASSWORD


下面的步骤对我很有效。我使用MySQL 8。*在Ubuntu上

停止MySQL服务,检查状态确认服务已停止

Sudo systemctl停止mysql Sudo systemctl status mysql

编辑systemd配置文件,这样您就可以在没有权限检查的情况下访问MySQL Sudo systemctl编辑mysql 复制并粘贴以下3行

(服务) ExecStart = ExecStart=/usr/sbin/mysqld——skip-grant-tables——skip-networking

粘贴行后按CTRL+0保存,然后按CTRL+X退出

重新加载mysql服务并启动它(使用——skip-grant-table启动服务)

Sudo systemctl daemon-reload Sudo systemctl启动mysql

5.现在无需密码即可连接MySQL服务器

sudo mysql -u root

6.通过运行加载授权表

mysql> FLUSH PRIVILEGES;

重置root密码

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

关闭mysql连接

mysql >退出

恢复对mysql systemd文件所做的修改

Sudo systemctl恢复mysql

重新加载mysql守护进程以进行更改。

Sudo systemctl daemon-reload

最后重新启动MySQL服务

Sudo systemctl restart mysql

现在用第7步设置的新密码连接mysql

你可以访问这个链接重置mysql 8的根密码了解更多细节。