MySQL修改密码 === ## mysqld_safe不存在修改密码 1. 停止 MySQL: ```bash systemctl stop mysqld ``` 2. 设置MySQL环境选项 ```bash systemctl set-environment MYSQLD_OPTS="--skip-grant-tables" ``` 3. 使用您刚才设置的选项启动MySQL ```bash systemctl start mysqld ``` 4. 以root身份登录 ```bash mysql -u root ``` 5. 使用这些MySQL命令更新root用户密码 ```bash mysql> UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPassword') -> WHERE User = 'root' AND Host = 'localhost'; mysql> FLUSH PRIVILEGES; mysql> quit ``` 6. 停止mysql ```bash systemctl stop mysqld ``` 7. 取消设置MySQL环境选项,以便下次启动 ```bash systemctl unset-environment MYSQLD_OPTS ``` 8. 正常启动MySQL: ```bash systemctl start mysqld ``` 9. 尝试使用您的新密码登录: ```bash mysql -u root -p ``` ## 启动修改丢失的mysql单实例root密码方法 1. 首先停止mysql ```bash sudo /etc/init.d/mysql stop # 或者下面 sudo /usr/local/mysql/bin/mysql stop # 或者 service mysqld stop ``` 2. 使用--skip-grant-tables启动mysql,忽略授权登录验证,服务不要停止,新开窗口做接下来的操作。 ```bash sudo mysqld_safe --skip-grant-tables --user=mysql # 提示:在启动时加--skip-grant-tables 参数,表示忽略授权验证 # 或者下面 sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables --user=mysql ``` 3. 进入数据库系统 ```bash # 在终端中运行 mysql ``` 4. 修改mysqlroot密码:update ```bash # 终端进入MySQL数据中运行 mysql> update mysql.user set password=password("123456") where user='root' and host='localhost'; mysql> flush privileges; # 终端中运行 mysqladmin -uroot -p123456 shutdown ``` 5. 重新启动mysql ```bash # 终端中运行 /etc/init.d/mysql start mysql -uroot -p123456 ``` ## Mac下重置MySQL的root密码 以前的老版本root的默认密码为空,Mysql 5.6及以后版本出处于安全考虑,root密码已经不为空了。 ```bash # 表示没有生成root的临时密码 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) # 表示生成了root的临时密码 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) ``` 使用 `mysql -uroot -p` 登录进去。 1、停止 mysql server 在 Mac 中可以使用GUI停止 MySQL 服务,步骤:`系统偏好设置` > `MySQL` > `Stop MySQL Server`。您也可以使用命令行来停止服务,命令如下: ```bash # Mac OS 里面可以这种方式 sudo /usr/local/mysql/support-files/mysql.server stop # centos 停止mysql数据库 sudo /etc/init.d/mysqld stop ``` 2、终端,输入进入安全模式,运行之后进程保持挂载。 ```bash sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables ``` 3、新开终端,输入 ```bash sudo /usr/local/mysql/bin/mysql -u root # 输入 Mac 系统用户密码后, # 直接敲回车进入 mysql 终端,输入: mysql> FLUSH PRIVILEGES; mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass'; # 输入 \q 退出 mysql 终端 \q ``` 4、重启MySQL. ```bash ps -ef | grep mysql ``` - 通过上面命令查看进程编号。 - 通过`sudo kill 93`杀掉`mysql`进程 - 启动服务`系统偏好设置` > `MySQL` > `Start MySQL Server` 命令行中更快捷的重启 ```bash sudo /usr/local/mysql/support-files/mysql.server start ``` ## 不重启mysqld修改密码的方法 1、首先得有一个可以拥有修改权限的mysql数据库账号,当前的mysql实例账号(较低权限的账号,比如可以修改test数据库)或者其他相同版本实例的账号。把data/mysql目录下面的user表相关的文件复制到data/test目录下面。 ``` [root@localhost mysql]# cp mysql/user.* test/ [root@localhost mysql]# chown mysql.mysql test/user.* ``` 2、使用另一个较低权限的账号链接数据库,设置test数据库中的user存储的密码数据。 ``` [root@localhost mysql]# mysql -utest -p12345 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 17 Server version: 5.5.25a-log Source distribution Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use test Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> update user set password=password('yayun') where user='root'; Query OK, 0 rows affected (0.00 sec) Rows matched: 5 Changed: 0 Warnings: 0 mysql> ``` 3、把修改后的user.MYD和user.MYI复制到mysql目录下,记得备份之前的文件。 ```bash mv mysql/user.MYD mysql/user.MYD.bak mv mysql/user.MYI mysql/user.MYI.bak cp test/user.MY* mysql/ chown mysql.mysql mysql/user.* ``` 4、查找mysql进程号,并且发送SIGHUP信号,重新加载权限表。 ``` [root@localhost mysql]# pgrep -n mysql 2184 [root@localhost mysql]# [root@localhost mysql]# kill -SIGHUP 2184 ``` 5、登陆测试 ```bash [root@localhost mysql]# mysql -uroot -pyayun Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 20 Server version: 5.5.25a-log Source distribution Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> ``` ## 官网参考 - [How to Reset the Root Password](http://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html)