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

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


当前回答

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

mysql - username -p

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

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

然后使用:

> flush privileges;

然后你就完成了。

其他回答

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

正如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 root密码,在终端输入:

Sudo dpkg-reconfigure mysql-server-5.5

MySQL守护进程将被停止,并提示您输入新密码。

我遇到ubuntu 18.04和mysql 5.7的问题,这是解决方案

执行命令前请尝试重新启动mysql-server

sudo service mysql restart

Mysql-server >= 5.7

sudo mysql -uroot -p
USE mysql;
UPDATE user SET authentication_string=PASSWORD('YOUR_PASSWORD') WHERE User='root';
UPDATE user SET plugin="mysql_native_password";
FLUSH PRIVILEGES;
quit;

Mysql-server < 5.7

sudo mysql -uroot -p
USE mysql;
UPDATE user SET password=PASSWORD('YOUR_PASSWORD') WHERE User='root';
UPDATE user SET plugin="mysql_native_password";
FLUSH PRIVILEGES;
quit;

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