mysql创建用户以及授权

默认用户有root超级管理员,要做一个网站,要连接mysql要一个用户名和密码,不可能是root,防止误操作。Mysql服务里面可以跑多个库,所以需要给单独的用户作一些授权,只需要他对某一个数据库或者某个数据库的某个表有权限。

grant all on . to 'user1' identified by 'passwd'; // grant是授权的意思 all全部的
1.mysql> grant all on . to 'user1'@'127.0.0.1' identified by '123'; //授权user1只能通过127这个ip登录mysql(源ip) identified by密码 .前面这个表示库名,后面是表。Ip也可以使用%表示所有的ip,
Query OK, 0 rows affected (0.63 sec)
2.[root@localhost ~]# mysql -uuser1 -p123 -h127.0.0.1 //用户登录。如果授权ip是localhost那么可以不用-h

3.mysql> grant all on db1.* to 'user1'@'192.168.222.%' identified by '1';
4.[root@localhost ~]# mysql -uuser1 -p1 -h192.168.222.51

5.grant SELECT,UPDATE,INSERT on db1. to 'user2'@'192.168.133.1' identified by 'passwd';
6.grant all on db1.
to 'user3'@'%' identified by 'passwd';

7.show grants;//查看当前用户的授权
mysql> show grants; //查看权限必须进入要查询的用户里面
+-------------------------------------------------------------------------------+
| Grants for user1@192.168.222.% |
+-------------------------------------------------------------------------------+
| GRANT USAGE ON . TO 'user1'@'192.168.222.%' IDENTIFIED BY PASSWORD <secret> |
| GRANT ALL PRIVILEGES ON db1.* TO 'user1'@'192.168.222.%' |
+-------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> select user();
+----------------------+
| user() |
+----------------------+
| user1@192.168.222.51 |
+----------------------+
1 row in set (0.00 sec)

8.show grants for user2@192.168.133.1; //查看指定用户的授权
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql> grant SELECT,UPDATE,INSERT on db1.* to 'user3'@'192.168.222.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for user3@'192.168.222.%';
+------------------------------------------------------------------------------------------------------------------+
| Grants for user3@192.168.222.% |
+------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON . TO 'user3'@'192.168.222.%' IDENTIFIED BY PASSWORD '6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
| GRANT SELECT, INSERT, UPDATE ON db1.
TO 'user3'@'192.168.222.%' |
+------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

转载于:https://blog.51cto.com/lsxme/2061417

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值