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

MySQL的skip-grant-tables

作者头像
bisal
发布2021-09-06 15:34:39
1.4K0
发布2021-09-06 15:34:39
举报
文章被收录于专栏:bisal的个人杂货铺

MySQL在Windows、Linux平台的安装,可以参考,

初探MySQL-小白的Linux安装笔记

Windows环境安装MySQL ZIP Archive

MySQL 5.6 rpm安装方法和碰见的问题

MySQL的rpm和源码两种安装操作

Windows下安装的MySQL 5.7,可以用如下指令,进行初始化、服务注册、启动服务,

代码语言:javascript
复制
C:\bisal\mysql\bin>mysqld --initialize --user=mysql --console
2021-01-12T11:46:53.608737Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-01-12T11:46:53.889730Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-01-12T11:46:53.981412Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: ddb683f6-54cb-11eb-ac61-0250f2000002.
2021-01-12T11:46:53.996569Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-01-12T11:46:56.054507Z 0 [Warning] CA certificate ca.pem is self signed.
2021-01-12T11:46:56.563731Z 1 [Note] A temporary password is generated for root@localhost: Bgbo>f4-Uv1j


C:\bisal\mysql\bin>mysqld install
Service successfully installed.


C:\bisal\mysql\bin>net start mysql
MySQL 服务正在启动 .
MySQL 服务已经启动成功。

此时data路径下,就会出现这些数据文件,

看到一些教程,说在首次配置时,可以在配置文件my.ini中设置skip-grant-tables参数,

代码语言:javascript
复制
skip-grant-tables

顾名思义,该命令作用是跳过授权表,就是说谁都能进入MySQL看到所有数据表,输入任意字符账号密码都可以,当忘记账号密码时可以使用改命令修改密码,但是要随用随关,重启mysql,不然服务器上会有很大的风险。

输入登陆的指令,但是可任意输入密码,都可以登录,

代码语言:javascript
复制
C:\bisal\mysql\bin>mysql -u root -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.32 MySQL Community Server (GPL)


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>

查看用户,显示的是skip-grants user,说明确实skip-grants参数起了作用,

代码语言:javascript
复制
mysql> select current_user;
+-----------------------------------+
| current_user                      |
+-----------------------------------+
| skip-grants user@skip-grants host |
+-----------------------------------+
1 row in set (0.00 sec)

登陆后,可以update更新root的密码,注意5.7的user存储密码的字段名称是authentication_string,

代码语言:javascript
复制
mysql> update mysql.user set authentication_string=password('mysql') where user='root' and host='localhost';
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 1

密码用password函数,进行了加密,

代码语言:javascript
复制
mysql> select password('mysql') from dual;
+-------------------------------------------+
| password('mysql')                         |
+-------------------------------------------+
| *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
+-------------------------------------------+
1 row in set, 1 warning (0.00 sec)


mysql> select host, user, authentication_string from user;
+-----------+---------------+-------------------------------------------+
| host      | user          | authentication_string                     |
+-----------+---------------+-------------------------------------------+
| localhost | root          | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys     | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+-----------+---------------+-------------------------------------------+
3 rows in set (0.00 sec)

当前模式下,而且支持用mysql直接进行登录,

代码语言:javascript
复制
C:\bisal\mysql\bin>mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.32 MySQL Community Server (GPL)


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>

但是这种跳过授权表的操作,毕竟不安全,相当于开了后门,现在将skip-grant-tables注释,

代码语言:javascript
复制
# skip-grant-tables

然后在Windows的服务窗口重启MySQL服务,cmd-services.msc,

此时使用mysql,会提示1045,

代码语言:javascript
复制
C:\bisal\mysql\bin>mysql
ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO)

用mysql -u -root -p登录,同样提示1045的错误,

代码语言:javascript
复制
C:\bisal\mysql\bin>mysql -u -root -p
Enter password: ************
ERROR 1045 (28000): Access denied for user '-root'@'localhost' (using password: YES)

可以指定-h登录,

代码语言:javascript
复制
C:\bisal\mysql\bin>mysql -u root -h localhost -p
Enter password: *****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.32 MySQL Community Server (GPL)


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>

他会提示使用ALTER USRE重置密码,

代码语言:javascript
复制
mysql> use mysql
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.


mysql> alter user 'root'@'localhost' identified by 'mysql' password expire never;
Query OK, 0 rows affected (0.00 sec)


mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

此时登陆的用户,不再是skip-grants,

代码语言:javascript
复制
mysql> select current_user;
+----------------+
| current_user   |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

MySQL我算小白,但是发现其实一些很小的知识点,例如登录,还是蕴涵了很多的原理,理解他的原理,多多实践,可能才会更深入的了解MySQL,当然这个过程,可能是很艰辛,还是要量变引起质变,各位共勉了。

参考,

https://blog.csdn.net/ibsfn/article/details/88963040

https://www.cnblogs.com/zhoushiya/p/12107201.html

https://www.cnblogs.com/jet-angle/p/11906671.html

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

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

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

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

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