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

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


当前回答

你不需要这些。只需登录:

Mysql -u root -p

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

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

其他回答

该任务指导用户在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密码时,上面列出的以下命令没有帮助。我发现进入终端并使用这些命令是毫无意义的。相反,使用命令sudo停止一切。如果有帮助的话,删除windows的system32。

这就像我为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

唯一对我有效的方法是这里描述的(我运行的是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 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