如何修改ubuntu服务器的MySQL root密码和用户名?我需要停止mysql服务之前,设置任何更改?
我有一个phpmyadmin设置以及,phpmyadmin会自动更新?
如何修改ubuntu服务器的MySQL root密码和用户名?我需要停止mysql服务之前,设置任何更改?
我有一个phpmyadmin设置以及,phpmyadmin会自动更新?
当前回答
首先执行以下命令:
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。
其他回答
该任务指导用户在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密码在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).
关于这个话题的大多数答案都过时了;在写这个答案之前,MySQL发生了两个主要的变化:
1-用户表中的“Password”字段已被“authentication_string”列取代。
2-“Password”加密功能:Password(“of some text”)已弃用。
更多信息请参考此链接:dev.mysql.com/doc/refman/8.0/en/resetting-permissions.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