阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

忘记MySQL/MariaDB root密码的解决方法

155次阅读
没有评论

共计 2216 个字符,预计需要花费 6 分钟才能阅读完成。

MySQL/MariaDB 的 root 密码忘了,不知道改怎么办,下面是修改 root 密码步骤。

(1)登录到数据库所在服务器,手工 kill 掉 MySQL 进程:
kill `cat /mysql-data-directory/hostname.pid`
其中,/mysql-data-directory/hostname.pid 指的是 MySQL 数据目录下的.pid 文件,它记录了
MySQL 服务的进程号。
[root@www.linuxidc.com mysql]# kill `cat /data/mysql/maridb1.pid`

(2)使用—skip-grant-tables 选项重启 MySQL 服务:
[root@www.linuxidc.com mysql]# cd /usr/local/mysql/bin
[root@www.linuxidc.com bin]# mysqld_safe –skip-grant-tables –user=mysql &
其中 –skip-grant-tables 意思是启动 MySQL 服务的时候跳过权限表认证。
启动后,连接到 MySQL 的 root 将不需要口令
查看服务是否启动
[root@www.linuxidc.com ~]#  ps -ef|grep mysql
root      3895  3056  0 11:16 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe –skip-grant-tables –user=mysql
mysql    4045  3895  0 11:16 pts/0    00:00:00 /usr/local/mysql/bin/mysqld –basedir=/usr/local/mysql/ –datadir=/data/mysql –plugin-dir=/usr/local/mysql//lib/plugin –user=mysql –skip-grant-tables –log-error=/data/mysql/maridb1.err –pid-file=maridb1.pid –socket=/tmp/mysql.sock –port=3306
root      4172  4130  0 11:20 pts/1    00:00:00 grep –color=auto mysql

(3)用空密码的 root 用户连接到 MySQL,并且更改 root 口令:
[root@www.linuxidc.com ~]# mysql -uroot
Welcome to the MariaDB monitor.  Commands end with ; or /g.
Your MariaDB connection id is 9
Server version: 10.2.10-MariaDB-log MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type ‘help;’ or ‘/h’ for help. Type ‘/c’ to clear the current input statement.

MariaDB [(none)]> set password = password(‘123’);
ERROR 1290 (HY000): The MariaDB server is running with the –skip-grant-tables option so it cannot execute this statement
MariaDB [(none)]> use mysql
Database changed
MariaDB [mysql]> update user set password=password(‘root’) where user=’root’ and host=’localhost’;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0
此时,由于使用了 –skip-grant-tables 选项启动,使用“set password”命令更改密码失败,直
接更新 user 表的 password 字段后更改密码成功。

(4)刷新权限表,使得权限认证重新生效:
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

(5)重新用 root 登录时,必须输入新口令:
[root@www.linuxidc.com ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: NO)
[root@www.linuxidc.com ~]# mysql -uroot -proot
Welcome to the MariaDB monitor.  Commands end with ; or /g.
Your MariaDB connection id is 12
Server version: 10.2.10-MariaDB-log MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type ‘help;’ or ‘/h’ for help. Type ‘/c’ to clear the current input statement.

至此密码修改成功

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计2216字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中