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

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


当前回答

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

其他回答

该解决方案属于以前版本的MySQL。 通过使用套接字身份验证登录MySQL,您可以做到这一点。

sudo mysql -u root

然后可以运行以下命令。

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

详情请点击这里。

设置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/

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

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

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

sudo dpkg-reconfigure mysql-server-5.6