insert into user插入表的方式添加mysql数据库用户

@

insert into user (host,user,password,select_priv,insert_priv,updat_priv) values (‘localhost’,‘guest’,password(‘guest123’),‘y’,‘y’,‘y’);

上面命令报错的解决办法亲测有效,
如下:
mysql学习笔记,
今天跟随菜鸟教程,
学习mysql,
遇到很多问题,
通过查看网友相关文章结合自己的理解,
找到解决办法,
分享给大家,
有一点感触,
不要随波逐流人云亦云,
亲身实践是最好的教科书,
尤其是写代码,
无数次听老师说代码只有多写才有收获,

mysql> insert into user(host,user,password,select_priv,insert_priv,updat_priv) values (‘localhost’,‘guest’,password(‘guest123’),‘y’,‘y’,‘y’);

报错:
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 '('guest123'),'y','y','y')' at line 1
报错是说,
有一处在 ‘(‘guest123’),‘y’,‘y’,‘y’)’ 附近的SQL语法是错误的,

先看菜鸟教程中的"注意",

注意
在 MySQL5.7 中 user 表的 password 已换成了authentication_string。

这说明,
我们使用的命令中出现的password存在问题,
知道是谁有问题就好办了,
继续看

请注意,

mysql>
insert into user (host,user,password,select_priv,insert_priv,updat_priv) values (‘localhost’,‘guest’,password(‘guest123’),‘y’,‘y’,‘y’);

命令中有两处password,

第一个password和host,user在一个括号里,
而host,user我们都知道它们是字段,
所以第一个password也是一个字段,

而第二个password,它的后面跟的是小括号,
这说明它是一个函数而不是字段,

但具体是第几个password出现了问题,
就不知道了,
那就一个一个试吧.

不妨我们先假设问题出在第一个password上,
那这说明在新版本中user(host,user,password,select_priv,insert_priv,updat_priv)里的password这个字段已经不存在了,
被换成了authentication_string字段,

根据这个提示先作个实验看看,
用select 语句到数据库里去查询一下新版本的SQL中还有没有password这个字段了,
命令如下:

mysql> seletct host,user,password from user;

报错如下:
ERROR 1054 (42S22): Unknown **column** '**password**' in 'field list'
错误是说,
password这个字段有问题,
实验结果充分说明字段password已经在新版本mysql中不存在了,

根据第一个实验的结果,
我们马上开始做第二个实验,
验证新版mysql中password字段是不是已经变成了authentication_string字段,
继续使用select语句来查数据库看有没有authentication_string 字段,

mysql> select host,user,authentication_string from user;

回显结果:

±----------±-----------------±------------------------------------------------------------
| host | user | authentication_string |
±----------±-----------------±------------------------------------------------------------
| localhost | mysql.infoschema | $A 005 005 005THI…

4 rows in set (0.00 sec)

命令正确执行,
说明新版mysql中password字段已经变成了authentication_string字段了,
那么使用insert into user 插入表方式来添加mysql的用户是可行的,
只是

insert into user (host,user,password,select_priv,insert_priv,updat_priv) values (‘localhost’,‘guest’,password(‘guest123’),‘y’,‘y’,‘y’);
命令中,
一些关键字在新版本中被替换了所以导致之前的命令执行失败报错,
所以需要我们改变一下命令中的关键字,
就可以使用insert into user 插入表方式来添加mysql用户了,

接下来我们将

insert into user (host,user,password,select_priv,insert_priv,updat_priv) values (‘localhost’,‘guest’,password(‘guest123’),‘y’,‘y’,‘y’);

进行修正,
因为尚且不知道是哪个 password 有问题,
索性我们把两个password都给它换成authentication_string再看结果,

命令如下,

mysql> insert into user (host,user,authentication_string,select_priv,insert_priv,update_priv) values(‘localhost’,‘guest’,authentication_string(‘guest123’),‘Y’,‘Y’,‘Y’);

产生新的报错如下:
ERROR 1305 (42000): **FUNCTION** mysql.**authentication_string** does not exist,
报错是说: 没有authentication_string 这个函数 ,
虽然仍然有报错,
但已经不是之前的,
ERROR 1064的报错了,
说明我们将原始命令中两个password替换掉起作用了,

而且第一个password被替换后没有报错,
说明替换的操作是正确的,
而报错是因为第二个 **password** 也被替换成 **authentication_string**  造成的,

要想解决这个报错就需要继续看菜鸟教程中的第二个注意了,如下:

注意
password() 加密函数已经在 8.0.11 中移除了,可以使用 MD5() 函数代替

可以看出,
这个注意说,
password() 加密函数已经不存在了,
取而代之的是MD5() 函数,

而我们使用的命令是 authentication_string(‘guest123’),‘Y’,‘Y’,‘Y’);
很显然用 authentication_string() 替换 password() 加密函数是不对的,
说明,用 authentication_string() 取代第二个 password 是错误的操作,
我们应该按注意中说到的,使用 MD5() 函数代 替换 password() 加密函数 ,
实际上,从原始命令中的两个password所在的位置和表现形式,
就可以看出来,
第一个password是一个字段,
第二个password()则是一个函数.

更改命令如下,
并运行,

mysql> insert into user (host,user,authentication_string,select_priv,insert_priv,update_priv) values (‘localhost’,‘guest’,MD5 (‘guest123’),‘Y’,‘Y’,‘Y’);

上述命令执行后,
得到新的报错:如下
ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value
报错是说,字段ssl_cipher没有被赋值,

mysql>
insert into user (host,user, password ,select_priv,insert_priv,updat_priv) values (‘localhost’,‘guest’, password (‘guest123’),‘y’,‘y’,‘y’);
替换为:
mysql>
insert into user (host,user,authentication_string,select_priv,insert_priv,update_priv) values(‘localhost’,‘guest’, MD5 (‘guest123’),‘Y’,‘Y’,‘Y’);

原始命令中两处存在问题的 password 已经都修正了,
第一个 password 这个字段已经被 authentication_string 字段替代了,
第二处的 password() 函数 也已经被函数 MD5 函数替换了,

到这里,
就意味着,

我们把原始命令中的两处 password 分别改成 authentication_stringMD5 并被执行后没有再报之前的错误,

说明我们已经很接近使用 insert into user 插入表的方式 为mysql添加新用户的目的了,

接下来就是解决出现的新的报错,
就可以了.



接下来分析
ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value,
错误变成了ssl_cipher字段没有默认值,
就是说我们在使用insert into user 插入表的方式为mysql添加用户时,
需要给ssl_cipher字段设定初始值,
那这个值应该是多少呢?
我们可以通过查看已经存在的用户,
它们关于ssl_cipher字段的值来作为我们的参考,



首先,
用select语句查看mysql数据库中都有哪些用户,

mysql> select user from user;

回显结果如下:

±-----------------+
| user |
±-----------------+
| guest |
| mysql.infoschema |
| mysql.session |
| mysql.sys |
| root |
±-----------------+
5 rows in set (0.00 sec)



接下来,
用select语句来查看系统管理员root的ssl_cipher字段值来作我们的参考,

mysql> select ssl_cipher from user where user=‘root’\G

这里先解释一下上面命令结尾处的**\G**,
正常的mysql命令都是以分号";"结尾的,

这里使用 \G 作为命令的结尾,
意思是格式化输出结果,

就是将mysql> select ssl_cipher from user where user='root';的输出结果进行重新排列并显示,

如果不加 \G ,
那么mysql> select ssl_cipher from user where user='root';的输出结果没有经过重新排版看起来就特别的乱.
而使用 \G 结尾,就会让输出的结果层次鲜明行列清晰,

当然也可以使用 \g,
\G和**\g** 差不多,
都是格式化输出结果,
以方便人们查看和阅读,



加了 \G 作为结尾的mysql语句执行结果如下,

*************************** 1. row ***************************
ssl_cipher: 0x
1 row in set (0.01 sec)
当然,
因为这里只有一条回显结果,
所以上面加不加\G都不要紧,

可以看到,
回显的结果中,
ssl_cipher的值是0x,

所以,
我们需要在 insert into user 命令中添加新的字段ssl_cipher并为其赋初值 0x
命令修正如下,

mysql> insert into user (host,user,authentication_string,select_priv,insert_priv,update_priv,ssl_cipher) values(‘localhost’,‘guest’,MD5(‘guest123’),‘Y’,‘Y’,‘Y’,'0x');



出现了新的报错,
如下:
ERROR 1364 (HY000): Field '**x509_issuer**' doesn't have a default value
这是一个新的字段 ‘x509_issuer’ ,

分析原因,
是跟ssl_cipher字段一样的赋初值问题,
解决方法同ssl_cipher字段的处理过程一样,
也是首先用select语句去查看并参考root用户的x509_issuer字段值是多少,
来作为我们命令的参考值.



命令如下,

mysql> select x509_issuer from user where user=‘root’\G

结果如下:

*************************** 1. row ***************************
x509_issuer: 0x
1 row in set (0.00 sec)

可以看到root用户的x509_issuer 字段值是0x,
所以我们以此作为参考,
将命令 insert into user 继续完善,



在命令中增加新的字段x509_issuer并赋初值**‘0x’**

如下:

mysql> insert into user (host,user,authentication_string,select_priv,insert_priv,update_priv,ssl_cipher,x509_issuer) values(‘localhost’,‘guest’,MD5(‘guest123’),‘Y’,‘Y’,‘Y’,‘0x’,'0x');

命令执行后又出现新的报错,如下:
ERROR 1364 (HY000): Field '**x509_subject**' doesn't have a default value
还是字段赋初值的问题,
解决方案同ssl_cipher字段和x509_issuer 字段,
先查看root用户的x509_subject字段值,
用来作参考,

命令:

mysql> select x509_subject from user where user=‘root’\G

结果:

*************************** 1. row ***************************
x509_subject: 0x
1 row in set (0.00 sec)

可以看到root用户的x509_subject 字段值也是0x,
所以我们以此作为参考,
将命令insert into user继续完善,

如下:

mysql> insert into user (host,user,authentication_string,select_priv,insert_priv,update_priv,ssl_cipher,x509_issuer,x509_subject) values(‘localhost’,‘guest’,MD5(‘guest123’),‘Y’,‘Y’,‘Y’,‘0x’,‘0x’,'0x');

最终结果,
如下:

Query OK, 1 row affected (0.05 sec)

终于成功了,
没再报任何错误,
说明我们使用insert into user 插入表方式为mysql数据库增加了新的用户guest,
密码为guest123,



使用 select语句查看mysql数据库的用户表,
是否有我们刚才使用insert into user 插入表方式增加的用户guest,

命令:

mysql> select user from user;

结果如下:

±-----------------+
| user |
±-----------------+
| guest |
| mysql.infoschema |
| mysql.session |
| mysql.sys |
| root |
±-----------------+
5 rows in set (0.00 sec)

mysql> insert into user (host,user,authentication_string,select_priv,insert_priv,update_priv,ssl_cipher,x509_issuer,x509_subject) values(‘localhost’,‘guest’,MD5(‘guest123’),‘Y’,‘Y’,‘Y’,‘0x’,‘0x’,‘0x’);执行正确并成功添加新用户guest,

用户表里有我们用这种方法创建的用户guest,



这里再重申一遍,
我们在使用insert into user插入表的方式添加mysql用户时,
需要使用新版本的新字段authentication_string而不是使用老版本的老字段password,

原始命令中的第二个password后面跟的是一个括号,
这说明它是一个函数,

password() 函数在新版本的mysql中也不存在了,
所以我们需要使用MD5()函数,
MD5
(‘guest123’),
而不是password(‘guest123’),

当使用authentication_string字段和MD5() 函数替换原始命令中的;
两处password后,
就已经是将最主要的两个问题解决了,

后续出现的几个错误,
都与新版本mysql中的要求几个必须有默认值的字段有关,
添加这些字段并相应赋值就可以了.



最后,
总结一下使用insert into user插入表的方式添加mysql用户的命令是可以执行的,
但也就是可以执行而已,
因为以这种方式创建的用户一点用处也没没有,
下面我会讲到,

原始命令没有执行成功的原因是新版本的mysql有些字段和函数已经随版本更新变更了,
只要我们将insert into user语句进行相应的调整后,
就可以使用insert into user插入表的方式添加mysql用户了,

网上也有很多其他的添加用户的方式,
比如使用grantcreat等方式添加用户.
grantcreat是新版本MYSQL的添加用户的正确方式.

2020 october the 19th monday 2020



对上面文章的继续补充,
此种方式虽然可以创建mysql数据库的用户,
可是这种方式创建的新用户既不可以登录数据库,
又不能被管理员操作,
实际上等于是废的账户,



为了说明问题,
首先,
咱们用select语句查看用户和密码两个字段,

mysql> select user,authentication_string from user ;

±-----------------±-----------------------------------------------------------------------+
| user | authentication_string |
±-----------------±-----------------------------------------------------------------------+
| guest | fcf41657f02f88137a1bcf068a32c0a3 |
| root | $A$005$ ~C,;Wp5dDhp3... |
±-----------------±-----------------------------------------------------------------------+
2 rows in set (0.01 sec)

从查询user,authentication_string字段得到的结果看,
insert into user插入表的方式创建的mysql的新用户guest 密码的加密方式和root用户截然不同,



这里说的加密方式,
是指所有可以被正常使用的MYSQL用户的密码都是以$开始的(除了root以外,
还可以通过使用select语句查看其他的系统用户来确认这一点),
而用insert into user插入表的方式创建的mysql的新用户的密码则没有以$开头,
这说明它们的加密方式是完全不同的.

guest | fcf41657f02f88137a1bcf068a32c0a3
root | $A 005 005 005 ~C,;Wp5dDhp3…



所以,虽然insert into user插入表的方式创建的mysql的新用户,
看起来在用户表里可以找到,
但实际上是不行的,
一点用都没有,
会报各种错误.

连登录数据库都拒绝的账户,又能有什么用呢?



所以,
insert into user插入表的方式创建的mysql的新用户的这个坑就不要再去踩了,
没有必要研究,
只会浪费时间.
这是因为,
新版的mysql已经舍弃了老版的很多命令,
取而代之的是新的命令,
所以我们没必要纠结老命令是不是还能用,
而应该追随新版本去使用新的命令,
当然,
版本太新也有一个问题,
那就是版本太新连遇到的问题在网上都没有解决方案,
所以,
还是选择适合自己的稳定的版本最好了,
最终的要点就是命令和版本要一致,
新版本就用新命令,
老版本就用老命令,
如果版本和命令对不上,
就会出各种奇怪的问题.



给大家看一下我使用的版本,
命令:

mysql> select version();

结果:

±----------+
| version() |
±----------+
| 8.0.21 |
±----------+
1 row in set (0.00 sec)

由于8.0.21 是最新的版本,
我在网上找了很多地方都没有8.0.21 的使用说明,
所以,
找来了跟8.0.21 版本比较接近的8.0.19版本的操作说明,
尝试了一下感觉可行,
就整理写到下面,



1、在8.0以上版本,
在mysql数据库创建新用户,
要使用新的命令,

如下:

mysql> create user ‘myself’@‘localhost’ identified by ‘guest123’;
结果如下:
Query OK, 0 rows affected (0.09 sec)

我创建了一个名字叫myself的用户,
给它配置了密码为guest123,



接下来使用select语句查询这个新建用户,

mysql> select host,user,authentication_string from user;

结果如下:

±----------±-----------------±-----------------------------------------------------------------------+
| host | user | authentication_string |
±----------±-----------------±-----------------------------------------------------------------------+
| localhost | guest | fcf41657f02f88137a1bcf068a32c0a3 |
| localhost | myself | $A 005 005 005!H(~F.0oQS2<JX … |
| localhost | root | $A 005 005 005 ~C,;Wp5dDhp3… | |
±----------±-----------------±-----------------------------------------------------------------------+
3 rows in set (0.00 sec)

通过查看authentication_string字段命令可以看出来,

使用新版本的创建用户的命令create 创建的新用户myself 和使用insert into user插入表的方式创建的新用户 guest 的密码字段中显示的密码加密方式完全不同,

而使用新版本的创建用户的命令create 创建的新用户myself
和系统用户root  的密码字段中的密码加密风格是 一样的,
`$`A$,
用新版本的创建用户的命令create创建的新用户理论上是可以使用的,



接下来我们对此结论做个测试,

用show语句查看新建用户myself的权限,
如果mysql正确执行,
说明新建用户好使

mysql> show grants for myself@localhost;

结果如下:

±-------------------------------------------+
| Grants for myself@localhost |
±-------------------------------------------+
| GRANT USAGE ON . TO myself@localhost |
±-------------------------------------------+
1 row in set (0.00 sec)

通过结果可见使用,show grants命令查看以新版本的创建用户的命令create 创建的新用户myself的权限,
是不报错的,
而同样使用show语句去查看用insert into user插入表的方式创建的新用户 guest 的权限,
就会报错

mysql> show grants for guest@localhost;

ERROR 1141 (42000): There is no such grant defined for user 'guest' on host 'localhost'
这更加说明在mysql的新版本中不要再去使用老版本中的命令了.



接下来继续测试使用新版本的创建用户的命令create 创建的新用户myself其他方面是否可以正常使用.



mysql> flush privileges;

Query OK, 0 rows affected (0.03 sec)

使用 ** flush privileges** 命令刷新权限,
这样就可以在不重启mysql数据库的前提下,
使用新用户了.



接下来使用新建用户登录数据库,

mysql> exit

Bye

C:\WINDOWS\system32> mysql -umyself -p
Enter password: ********

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.21 MySQL Community Server - GPL
Copyright © 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>

没问题,
以新版本的创建用户的命令create 创建新用户可以正常登录数据库,



接下来看看新用户除了能登录数据库还能干点什么,

mysql> use mysql

ERROR 1044 (42000): **Access denied** for user 'myself'@'localhost' to database 'mysql'

可见在没有给新用户赋权的情况下,
是不可以使用数据库的,



所以接下来需要使用赋权命令给新建用户赋权,

mysql> grant all privileges on . to ‘myself’@‘localhost’;

Query OK, 0 rows affected (0.03 sec)

给新建用户myself赋权成功,



使用flush刷新权限,
让新建用户可以工作,

mysql>flush privileges;

Query OK, 0 rows affected (0.03 sec)



接下来使用新建账号myself登录数据库,
进行测试,

C:\WINDOWS\system32> mysql -umyself -p
Enter password: ********

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.21 MySQL Community Server - GPL
Copyright © 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> **select user()**;

±-----------------+
| user() |
±-----------------+
| myself@localhost |
±-----------------+
1 row in set (0.00 sec)

mysql> show databases;

±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
±-------------------+
4 rows in set (0.00 sec)
mysql> use mysql;
Database changed

一切正常,




总结,
在遇到数据库相关错误的时候,
应该先确定自己使用的版本,
尽量使用和自己的数据库版本相对应的命令,
否则极容易出现各种莫名其妙的问题,
最后建议大家在学习和使用数据库的时候,
尽量使用那些成熟稳定的版本,
不要太过于尝鲜使用最新的版本,
因为一但遇到了问题,
在网上连解决方案都找不到,
还是使用那些稳定的版本来学习数据库较不容易走弯路,



最后再强调一遍,
一定要使用新版本的创建用户的命令create 去创建新用户,
不要使用老版本insert into user插入表的这种方式创建用户,
即使用户创建成功了,
也不能用.
october the 20th 2020

  • 22
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值