前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL8.0使用skip-grant-tables

MySQL8.0使用skip-grant-tables

原创
作者头像
云鹏
修改2021-05-21 10:06:32
4.1K0
修改2021-05-21 10:06:32
举报
文章被收录于专栏:MySQL捉虫MySQL捉虫

在忘记MySQL密码的时候,会使用skip-grant-tables选项,下面记录一下在MySQL8.0环境中使用skip-grant-tables的情况。

mysqld程序使用skip-grant-tables选项启动

代码语言:javascript
复制
# ./bin/mysqld --skip-grant-tables --user=mysql

update修改密码

登录MySQL数据库,设置密码

由于mysql 8.0不在支持password函数

The PASSWORD() function. Additionally, PASSWORD() removal means that SET PASSWORD ... = PASSWORD('auth_string') syntax is no longer available.

因此如下命令无法执行,

代码语言:javascript
复制
mysql> update mysql.user set authentication_string=password('123456') where user='root';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('123456') where user='root'' at line 1

替代方法将密码字段设置为空

代码语言:javascript
复制
mysql> update mysql.user set authentication_string='' where user='root' and host='localhost';
mysql> flush privileges;

使用set password修改密码

代码语言:javascript
复制
# mysql -uroot --socket=/tmp/mysql.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.22-debug Source distribution
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> set password for 'root'@'localhost'='123456';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> flush privileges;
Query OK, 0 rows affected (0.13 sec)
mysql> set password for 'root'@'localhost'='123456';
Query OK, 0 rows affected (0.01 sec)

第一次执行执行set password命令失败,代码如下

代码语言:javascript
复制
int mysql_execute_command(THD *thd, bool first_level) {
...
 /* Check if the statement fulfill the requirements on ACL CACHE */
  if (!command_satisfy_acl_cache_requirement(lex->sql_command)) {
    my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--skip-grant-tables");
    goto error;
  }
...
}

command_satisfy_acl_cache_requirement函数会调用skip_grant_tables函数

代码语言:javascript
复制
bool skip_grant_tables(void) { return !initialized; }

由于使用了skip-grant-tables选项,initialized变量为false。

执行flush privileges命令会让MySQL实例重新读取授权表并将initialized变量设置为true,因此第二次的set password命令正常执行。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

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

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