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

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


当前回答

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

其他回答

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

mysql - username -p

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

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

然后使用:

> flush privileges;

然后你就完成了。

回应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密码在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;

您可以使用该命令:

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

之后请冲洗:

FLUSH PRIVILEGES;

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!