为了演示, 找个测试环境来操作, 千万不要线上这么搞
pkill mysqld 关闭掉 MySQL
跳过 MySQL 的权限表启动 MySQL 服务:
--skip-grant-tables 启动 MySQL
/usr/local/mysql8013/bin/mysqld_safe --defaults-file=/data/mysql8/my8.cnf --skip-grant-tables &
将密码置空:
- [email protected] [(none)]>update MySQL.user set authentication_string=''where user="root"and host="localhost";
- Query OK, 1 row affected (0.06 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
- [email protected] [(none)]>
pkill mysqld 关闭掉 MySQL
再次启动 MySQL 服务
[[email protected] ~]# /usr/local/mysql8013/bin/mysqld --defaults-file=/data/mysql8/my8.cnf &
无密码登录 MySQL 服务并设置新的密码:
- [email protected] [(none)]>select version();
- +-----------+
- | version() |
- +-----------+
- | 8.0.13 |
- +-----------+
- [email protected] [(none)]>alter user user() identified by '[email protected]';
- [[email protected] ~]# MySQL -uroot -p'[email protected]' -e "select version();"
- MySQL: [Warning] Using a password on the command line interface can be insecure.
- +-----------+
- | version() |
- +-----------+
- | 8.0.13 |
- +-----------+
密码写入. my.cnf 文件就可以 MySQL 直接登录 MySQL 窗口:
- [[email protected] ~]# cat .my.cnf
- [MySQL]
- prompt="\[email protected]\h:\p \R:\m:\s[\d]>"
- no-auto-rehash
- user=root
- [email protected]
- [[email protected] ~]# MySQL -e "select version();"
- +-----------+
- | version() |
- +-----------+
- | 8.0.13 |
- +-----------+
演示完毕
来源: http://www.bubuko.com/infodetail-3415568.html