前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >忘记MySQL/MariaDB root密码的解决方法

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

作者头像
星哥玩云
发布2022-08-18 16:22:05
1.1K0
发布2022-08-18 16:22:05
举报
文章被收录于专栏:开源部署

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.

至此密码修改成功

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档