植物变异大招
29.65 MB · 2025-10-29
记录一下mysql中root用户密码的管理方式,mysql中root用户密码管理方式主要分为三个场景:
#此方式是给root用户设置临时密码,但应用临时密码登录数据库服务后,需要将临时密码修改,否则不能进行任何操作
mysqld --initialize --user=mysql --datadir=/data/3306/data --basedir=/usr/local/mysql
# 参数解析
# --user:指定Linux系统用户
# --datadir:指定数据目录
# --basedir:指定按照目录
mysqladmin -uroot password '新密码';
#或者 此方法需要登录到MySQL系统中
alter user root@'localhost' identified by '新密码';
mysqladmin -uroot password '新密码';
#或者 此方法需要登录到MySQL系统中
alter user root@'localhost' identified by '新密码';
set password for 'oldboy'@'localhost'=PASSWORD('新密码');
flush privileges;
补充:flush privileges的作用:
此命令只针对授权表中的数据
mysqladmin -uroot -p老密码 password '新密码'
#或者登录进数据库中执行
alter mysql.user root@'localhost' identified by '新密码';
# 需登录进数据库中
update mysql.user set authentication_string=PASSWORD('新密码') where user='root' and host='localhost';
flush privileges;
# 需登录进数据库中
set password for 'root'@'localhost'=PASSWORD('新密码');
flush privileges;
[root@master ~]# /etc/init.d/mysqld stop
# 检查是否已关闭
[root@master ~]# ps -ef | grep mysqld
root 4070896 4068830 0 15:24 pts/0 00:00:00 grep mysqld
# 输入mysqld_safe --skip-grant-tables --skip-networking & 即可
[root@master ~]# mysqld_safe --skip-grant-tables --skip-networking &
[1] 4071155
[root@master ~]# 2025-10-28T07:25:16.184768Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2025-10-28T07:25:16.211299Z mysqld_safe Starting mysqld daemon with databases from /data00/data/mysql
[root@master ~]# 2025-10-28T07:25:16.184768Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2025-10-28T07:25:16.211299Z mysqld_safe Starting mysqld daemon with databases from /data00/data/mysql
# 输入mysql -uroot 即可
mysql -uroot
# 修改密码
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> alter user root@'localhost' identified by 'huangsir';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
# 退出数据库
mysql> exit;
# 先杀掉进程
[root@master ~]# pkill mysql
# 检查进程是否杀掉
[root@master ~]# ps -ef | grep mysql
root 4072026 4068830 0 15:29 pts/0 00:00:00 grep mysql
# 启动mysql
[root@master ~]# /etc/init.d/mysqld start
Starting mysqld (via systemctl): mysqld.service.
[root@master ~]# mysql -uroot -phuangsir
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 8
Server version: 8.0.26 MySQL Community Server - GPL
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql>
本文来自博客园,作者:huangSir-devops,转载请注明原文链接:https://www.cnblogs.com/huangSir-devops/p/19171958,微信Vac666666,欢迎交流