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

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


当前回答

正如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服务器…

如果你在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操作系统下设置/修改/重置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)

回应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用户的密码:

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