mysql 5.7、8.0版本 改密码

5.7版本(centos7 里测试)

1、忘记密码,跳过检测

第一种,用docker安装的,进入容器/etc/mysql 和外部挂载的直接修改文件

[root@wjy mysqlconfig]# docker exec -it 97ca731b5ff4 /bin/bash

root@97ca731b5ff4:/# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

root@97ca731b5ff4:/# cd /etc/mysql/conf.d

root@97ca731b5ff4:/# vi docker.cnf
[mysqld]
skip-host-cache
skip-name-resolve
skip-grant-tables #加这一行

如果容器里没有vi指令,也不愿意安装,也可以从挂载出来的路径里去修改,我这里用docker-compose安装的

[root@wjy ~]# cat docker-compose.yml
version: "2.1"
services:
  mysql:
    image: mysql:5.7.20
    restart: unless-stopped
    ports:
     - "3306:3306"
    environment:
     MYSQL_ROOT_PASSWORD: 123456
    volumes:
     - /data/docker/mysql/mysql:/var/lib/mysql
     - /data/docker/mysql/mysqlconfig:/etc/mysql

[root@wjy ~]#
[root@wjy conf.d]# pwd
/data/docker/mysql/mysqlconfig/conf.d

[root@wjy conf.d]# vi docker.cnf
[mysqld]
skip-host-cache
skip-name-resolve
skip-grant-tables #加这一行

如果这里也没有,就再用docker run运行一个测试用的mysql,然后用docker cp 去复制出来容器内部的/etc/mysql目录,注意不要把目录挂载出来,要不然白玩
例子:
注意这里要把整个目录弄出来,在我试验过程中,文件不齐全的情况下,容器是起不来的

[root@wjy tmp]# docker cp 97ca731b5ff4:/etc/mysql .

这里是正式运行环境的mysql容器挂载目录,上边提到过

[root@wjy tmp]# cp mysql/* /data/docker/mysql/mysqlconfig/

第二种,直接将mysql安装在主机的,我没做测试,网上说修改/etc/mysql/my.cnf 文件,也是增加skip-grant-tables

2、清空密码

# 直接写入新密码的话,新旧密码都登录不了
root@97ca731b5ff4:/# mysql -uroot -p
Enter password:
# 显示内容省略了

mysql> use mysql
mysql> update user set authentication_string='' where  user = 'root';

3、写入新密码

mysql> use mysql
mysql> update user set authentication_string=password('123456')  where user='root';

# 用下边的写法也行,方便点
mysql> update mysql.user set authentication_string=password('123456')  where user='root';

4、刷新权限

mysql> flush privileges;

5、赋予root用户所有数据库的所有权限

mysql> grant all privileges on *.* to root@'%' identified by '123456';
mysql> flush privileges;

8.0版本(Ubuntu 20.04.3里做的,别人的机器,不好做太多试验)

1、默认安装自动生成账密

在安装过程中,直接安装在了主机里,使用的自动生成的账号密码,所以不知道root密码
自动生成的账号密码
账号:debian-sys-maint
密码:BcJnVUVx3JlY0Vq5
因为有这个密码,所以直接登录后重置了root密码,没有做设置免密登录那一步

2、清空密码(与5.7有区别)

zsw@ubuntu:~$ mysql -udebian-sys-maint -pBcJnVUVx3JlY0Vq5
# 在这时,稀里糊涂的把密码加密方式给改了,倒是没有影响就没管
mysql> update mysql.user set authentication_string=PASSWORD('newPwd'), plugin='mysql_native_password' where user='root';
mysql> select user ,plugin from mysql.user;
+------------------+-----------------------+
| user             | plugin                |
+------------------+-----------------------+
| root             | mysql_native_password |
| debian-sys-maint | caching_sha2_password |
| mysql.infoschema | caching_sha2_password |
| mysql.session    | caching_sha2_password |
| mysql.sys        | caching_sha2_password |
| root             | mysql_native_password |
| zsw              | caching_sha2_password |
+------------------+-----------------------+
7 rows in set (0.00 sec)
mysql> use mysql
mysql> update user set authentication_string=''  where  user='root';

# 在网上看的,authentication_string='' 这里没有了password字段,是因为下边的表里内容在mysql 8.0里改了
mysql> show fields from user; #或是使用describe user;
# 这里内容省略
| plugin                   | char(64)                          | NO   |     | caching_sha2_password |       |
| authentication_string    | text                              | YES  |     | NULL                  |       |
# 这里内容省略

3、写入新密码

mysql> use mysql
mysql> update user set authentication_string='123456#Abc'  where  user='root';

4、刷新权限

mysql> flush privileges;

5、赋予root用户所有数据库的所有权限

mysql> grant all privileges on *.* to root@'%' identified by '123456#Abc';
mysql> flush privileges;

补充:
// 删除用户。

mysql>use mysql;
mysql>delete from user where User="test" and Host="localhost";
mysql>flush privileges;

// 删除用户的数据库

mysql>drop database test;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值