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

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


当前回答

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

其他回答

该任务指导用户在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)

对我有用的(Ubuntu 16.04, mysql 5.7):

停止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

然后:

UPDATE mysql.user SET authentication_string=PASSWORD('YOURNEWPASSWORD'), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';
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 5.6,这个命令有效,你可以通过向导设置密码:

sudo dpkg-reconfigure mysql-server-5.6

下面的步骤对我很有效。我使用MySQL 8。*在Ubuntu上

停止MySQL服务,检查状态确认服务已停止

Sudo systemctl停止mysql Sudo systemctl status mysql

编辑systemd配置文件,这样您就可以在没有权限检查的情况下访问MySQL Sudo systemctl编辑mysql 复制并粘贴以下3行

(服务) ExecStart = ExecStart=/usr/sbin/mysqld——skip-grant-tables——skip-networking

粘贴行后按CTRL+0保存,然后按CTRL+X退出

重新加载mysql服务并启动它(使用——skip-grant-table启动服务)

Sudo systemctl daemon-reload Sudo systemctl启动mysql

5.现在无需密码即可连接MySQL服务器

sudo mysql -u root

6.通过运行加载授权表

mysql> FLUSH PRIVILEGES;

重置root密码

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

关闭mysql连接

mysql >退出

恢复对mysql systemd文件所做的修改

Sudo systemctl恢复mysql

重新加载mysql守护进程以进行更改。

Sudo systemctl daemon-reload

最后重新启动MySQL服务

Sudo systemctl restart mysql

现在用第7步设置的新密码连接mysql

你可以访问这个链接重置mysql 8的根密码了解更多细节。