作为程序员一定要保持良好的睡眠,才能好编程

mysql新增用户并赋予对shop数据库的管理

发布时间:2016-05-16


部署网站,每一个网站都有一个自己的数据库,并只能对自己的数据库有权限,那么应该怎么办?


1、新建一个数据库 shop

2、新建用户名 w_user

3、授权w_user有对shop访问的权限


打开mysql服务器

mysql -h *.*.*.* -u root -p ******  回车


登录成功:

r4.png

这是使用超级管理员登录的。


第一步:新建数据库    shop

 

create database shop default charset utf8;

r1.png


第二步:新建用户    w_user

use mysql;

insert into user(Host,User,Password)values('%','w_user',password('123456'));


r2.png


新建用户:w_user

密码为:123456


解释说明:

Host 这里指定的是 % 就是可以远程访问。 如果不想远程访问,设置为 localhost 或127.0.0.1 都行,也可以指定只有一个IP可以去访问。

 

mysql5.5只填写 Host  User  Password 这三个选项即可,


 而mysql5.6新增了三个比填项,因此插入数据库的时候,需要把这三个字段也添加上,看代码:

ssl_cipher,

x509_issuer,

x509_subject


insert into user(Host,User,Password,ssl_cipher,x509_issuer,x509_subject)values("%","web_song",password('xv1@YTpQ66'),'','','');


并给web_song授权 可以访问这几个数据库:

   grant all privileges on chinameat.* to 'web_song';
   grant all privileges on cimie_en.* to 'web_song';
   grant all privileges on newcimie.* to 'web_song';
   grant all privileges on peidui.* to 'web_song';
   grant all privileges on qscccma.* to 'web_song';


可以使用show create table user 查看数据创建的结构:

  `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',
  `User` char(16) COLLATE utf8_bin NOT NULL DEFAULT '',
  `Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '',
  `ssl_cipher` blob NOT NULL,
  `x509_issuer` blob NOT NULL,
  `x509_subject` blob NOT NULL,


第三步:授权

grant 权限1,权限2,…权限n on 数据库名称.表名称 to 用户名@用户地址 identified by ‘连接口令’;


给w_user授权操作shop数据库的所有权限。

grant all privileges on shop.* to 'w_user';

flush privileges;

问题:

mysql> grant all privileges on database.* to 'muser';
ERROR 1133 (42000): Can't find any matching row in the user table
mysql> flush privileges;
要是出现了红色的字,需要把flush privileges; 执行一下,然后再分配权限


r3.png   






授权w_user 可以操作shop库中的article表,可以增、删,改,查,以及删除表,创建表,并只能192.168.1.105这台机器登录

grant select,insert,update,delete,create,drop on shop.article to w_user@192.168.1.105


对所有的数据库的操作的权限

grant all privileges on *.* to w_user;    


第四步:查看权限


使用新建立的用户名去登录


r5.png


登录成功了


不要本地测试,本地测试不能登录成功 ,是因为我们把w_user 这个用户的Host改成了% ,只能远程访问。


------------------------------------------------

问题汇总:

授权时,出现Can't find any matching row in the user table  这时,需要更新权限一下就好了。

flush privileges;

然后再执行.

mysql> grant all privileges on chinameat.* to w_user;
ERROR 1133 (42000): Can't find any matching row in the user table
mysql> grant all privileges on chinameat.* to 'w_user'@'%';
ERROR 1133 (42000): Can't find any matching row in the user table
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on chinameat.* to 'w_user'@'%';
Query OK, 0 rows affected (0.01 sec)






mysql>grant select,insert,update,delete,create,drop on vtdc.employee to joe@10.163.225.87 identified by ‘123′;
给来自10.163.225.87的用户joe分配可对数据库vtdc的employee表进行select,insert,update,delete,create,drop等操作的权限,并设定口令为123。

mysql>grant all privileges on vtdc.* to joe@10.163.225.87 identified by ‘123′;
给来自10.163.225.87的用户joe分配可对数据库vtdc所有表进行所有操作的权限,并设定口令为123。

mysql>grant all privileges on *.* to joe@10.163.225.87 identified by ‘123′;
给来自10.163.225.87的用户joe分配可对所有数据库的所有表进行所有操作的权限,并设定口令为123。