相关链接:
Ubuntu16.04 修改mysql5.7 的数据目录
- 修改MySQL配置文件
$ vim /etc/mysql/mysql.conf.d/mysqld.cnf
# 在配置文件中加入:
[mysqld] #模块后加入
skip-grant-tables #这一行配置让 mysqld 启动时不对密码进行验证
- 重启 mysqld 服务:
service mysql restart
- 登录MySQL
mysql # 登录MySQL
use mysql;
select user,host,plugin from user;
mysql> select user,plugin,host from user;
+---------------+-----------------------+-----------+
| user | plugin | host |
+---------------+-----------------------+-----------+
| root | mysql_native_password | localhost |
| mysql.session | mysql_native_password | localhost |
| mysql.sys | mysql_native_password | localhost |
| nst | mysql_native_password | 127.0.0.1 |
+---------------+-----------------------+-----------+
- 修改root密码
# 修改root密码
> update user set authentication_string=password('新密码'),plugin="mysql_native_password" where user='root' and host="localhost";
> flush privileges;
- 再将我们第一步在配置文件中加入的
skip-grant-tables
注释掉
$ vim /etc/mysql/mysql.conf.d/mysqld.cnf
# 在配置文件中加入:
[mysqld] #模块后加入
#skip-grant-tables #在这一行前加入`#`
- 重启 mysqld 服务[并使用新密码登录MySQL]:
service mysql restart
mysql -u root -p