找回mysql root 密码

来自艾毕思知识库
跳转至: 导航搜索

当你忘记mysql数据库的root密码时,可以通过下面的步骤修改数据库root用户密码。

mysql版本5及以上

停止mysql服务

FreeBSD

# /usr/local/etc/rc.d/mysql-server stop

Linux

# /etc/init.d/mysql stop

OS X

$ mysql.server stop

以安全模式启动mysql服务

FreeBSD

# /usr/local/bin/mysqld_safe --skip-grant-tables &

Linux

# mysqld_safe --skip-grant-tables &

OS X

$ mysqld_safe --skip-grant-tables &

从本地mysql客户端链接到mysql服务

# mysql -u root

正常情况输出类似:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.16 Homebrew

Copyright (c) 2000, 2014, 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>

修改root用户密码

mysql> use mysql;
mysql> update user set password=PASSWORD("新密码") where User='root';
mysql> flush privileges;
mysql> quit

停止mysql安全模式服务

FreeBSD

# /usr/local/etc/rc.d/mysql-server stop

Linux

# /etc/init.d/mysql stop

OS X

$ mysql.server stop

启动mysql服务

FreeBSD

# /usr/local/etc/rc.d/mysql-server start

Linux

# /etc/init.d/mysql start

OS X

$ mysql.server start

完成

使用新密码登录验证

# mysql -u root -p