码迷,mamicode.com
首页 > 数据库 > 详细

13.4-13.6 MySQL用户管理,常用语句

时间:2018-05-08 16:42:25      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:mysql用户管理   mysql常用语句   

13.4 MySQL用户管理


1 创建user1用户

mysql> grant all on  *.*  to'user1'@'127.0.0.1' identified by '123456a';

Query OK, 0 rows affected (0.01 sec)


解释:

grant all 所有的权限 

*.*xx库的xx表,如果是mysql库的表可以表示为mysql.*

@'127.0.0.1'授权指定源ip连接。所有ip通配表示为@'%',%表示所有ip


2 利用user1连接MySQL

如果没有指定ip连接,会出现以下报错,因为mysql默认socket连接,而user1授权指定了ip连接。

[root@AliKvn ~]# mysql -uuser1 -p123456a 

Warning: Using a password on the command line interface can be insecure.

ERROR 1045 (28000): Access denied for user 'user'@'localhost' (using password: YES)

正确连接方式,

[root@AliKvn ~]# mysql -uuser1 -p123456a  -h127.0.0.1 

mysql> 


2.1 授权localhost连接

利用root进入mysql修改,

[root@AliKvn ~]# mysql -uroot -paminglinux

mysql>  grant all on  *.*  to'user1'@'localhost' identified by '123456a';

2.2 localhost相当于socket,所以在本机用user1连接mysql的话,直接连接

[root@AliKvn ~]# mysql -uuser1 -p123456a

mysql> 

2.3 还可以针对性去设置用户给予权限

mysql> grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.133.1' identified by 'passwd';


3 查看用户的权限

查看当前用户的权限

mysql> show grants

查看user1的权限

mysql> show grants for user1@'127.0.0.1';

+-----------------------------------------------------------------------------------------------------------------------+

| Grants for user1@127.0.0.1                                                                                            |

+-----------------------------------------------------------------------------------------------------------------------+

| GRANT ALL PRIVILEGES ON *.* TO 'user1'@'127.0.0.1' IDENTIFIED BY PASSWORD '*B012E8731FF1DF44F3D8B26837708985278C3CED' |

+-----------------------------------------------------------------------------------------------------------------------+


3.1 添加user2,并针对指定权限

mysql> grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'172.18.171.157' identified by 'passwd';

Query OK, 0 rows affected (0.00 sec)

mysql> show grants for user2@'172.18.171.157'

    -> ;

+-------------------------------------------------------------------------------------------------------------------+

| Grants for user2@172.18.171.157                                                                                   |

+-------------------------------------------------------------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'user2'@'172.18.171.157' IDENTIFIED BY PASSWORD '*59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0' |

| GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'172.18.171.157'                                               |

+-------------------------------------------------------------------------------------------------------------------+

2 rows in set (0.00 sec)


3.2 在不知道用户密码的时候给予同样权限,例如给予172.18.171.158的user2.

我们复制即可show grants后的信息,更改IP地址172.18.171.158,然后再分别执行即可赋予同样权限。

mysql> GRANT USAGE ON *.* TO 'user2'@'172.18.171.158' IDENTIFIED BY PASSWORD '*59C70DA2F3E3A5BDDF46B68F5C8B8F25762BCCEF0'

    -> ;

Query OK, 0 rows affected (0.00 sec)

mysql> GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'172.18.171.158';

Query OK, 0 rows affected (0.00 sec)

3.3 看158的用户权限信息。

mysql> show grants for user2 @'172.18.171.158'

    -> ;

+-------------------------------------------------------------------------------------------------------------------+

| Grants for user2@172.18.171.158                                                                                   |

+-------------------------------------------------------------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'user2'@'172.18.171.158' IDENTIFIED BY PASSWORD '*59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0' |

| GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'172.18.171.158'                                               |

+-------------------------------------------------------------------------------------------------------------------+

2 rows in set (0.00 sec)



 

13.5 常用sql语句


1 查看MySQL库里的user表有多少行

select count(*) from mysql.user;

mysql> select count(*) from mysql.user;

+----------+

| count(*) |

+----------+

|       11 |

+----------+

1 row in set (0.00 sec)


count(*)表示表中共有多少行,此处是11行。

这个是用MyIsam 的engine,相对InnoDB会比较快


2 查看表里所有的内容;

mysql> select * from mysql.db\G;

技术分享图片

这里的*这个是用InnoDB的Engine 会比较慢,所以要慎用


3 查询单个或多个字段的数据

单个

select db from mysql.db;

mysql> select db from mysql.db;

+---------+

| db      |

+---------+

| test    |

| test\_% |

| db1     |

| db1     |

| db1     |

+---------+

5 rows in set (0.00 sec)


多个

select db,user from mysql.db;

mysql> select db,user from mysql.db;

+---------+-------+

| db      | user  |

+---------+-------+

| test    |       |

| test\_% |       |

| db1     | user2 |

| db1     | user2 |

| db1     | user2 |

+---------+-------+

5 rows in set (0.00 sec)



4 模糊查询

mysql>  select * from mysql.db where host like '172.%.%';

可以利用\G整洁显示

mysql> select * from mysql.db where host like '172.%.%'\G;

其中like就是起到了模糊匹配的作用

技术分享图片

5 插入语句

db1.t1是一个空表,利用这个表测试。

mysql> desc db1.t1;

+-------+----------+------+-----+---------+-------+

| Field | Type     | Null | Key | Default | Extra |

+-------+----------+------+-----+---------+-------+

| id    | int(4)   | YES  |     | NULL    |       |

| name  | char(40) | YES  |     | NULL    |       |

+-------+----------+------+-----+---------+-------+

2 rows in set (0.00 sec)


mysql> select * from db1.t1;

Empty set (0.01 sec)


在db1.t1插入数据:两个字段(第一个字段是id,第二个字段是name)

mysql>  insert into db1.t1 values (1, 'abc');

mysql> select * from db1.t1;

+------+------+

| id   | name |

+------+------+

|  1  | abc  |

+------+------+

1 row in set (0.00 sec)

可以看到已被插入2个字段,分别是1和abc.

插入数据的时候要注意,插入字符串最好加上'',数字可以不用加''。

mysql> insert into db1.t1 values (1,'234');

Query OK, 1 row affected (0.00 sec)


mysql> insert into db1.t1 values (1,234);

Query OK, 1 row affected (0.00 sec)


mysql> select * from db1.t1;

+------+------+

| id   | name |

+------+------+

|    1 | abc  |

|    1 | 234  |

|    1 | 234  |

+------+------+

3 rows in set (0.00 sec)


5.1 更改表的某一行的数据

update db1.t1 set name='aaa' where id=1;


mysql> select * from db1.t1;

+------+------+

| id   | name |

+------+------+

|    1 | aaa  |

|    1 | aaa  |

|    1 | aaa  |

+------+------+

3 rows in set (0.01 sec)

更改db1.t1里面匹配id=1 将name改成aaa值

5.2 同样可以匹配name改id

mysql> update db1.t1 set id=2  where name='aaa';

Query OK, 3 rows affected (0.01 sec)

Rows matched: 3  Changed: 3  Warnings: 0


mysql> select * from db1.t1;

+------+------+

| id   | name |

+------+------+

|    2 | aaa  |

|    2 | aaa  |

|    2 | aaa  |

+------+------+

3 rows in set (0.00 sec)


6 清空字段

mysql> delete from db1.t1 where id=2;

mysql> select * from db1.t1;

Empty set (0.00 sec)

6.1  清空表的内容,表结构保留

mysql>truncate table db1.t1; 

mysql> desc db1.t1;

+-------+----------+------+-----+---------+-------+

| Field | Type     | Null | Key | Default | Extra |

+-------+----------+------+-----+---------+-------+

| id    | int(4)   | YES  |     | NULL    |       |

| name  | char(40) | YES  |     | NULL    |       |

+-------+----------+------+-----+---------+-------+

2 rows in set (0.00 sec)


6.2 完全删除表

mysql>drop table db1.t1;

mysql> desc db1.t1;

ERROR 1146 (42S02): Table 'db1.t1' doesn't exist

6.3 干掉数据库

drop database db1;

mysql> drop database db1;

Query OK, 0 rows affected (0.00 sec)


mysql> use db1

ERROR 1049 (42000): Unknown database 'db1'


总结:

尽量在大数据库,表少用*来查看内容。

慎用删除功能。


13.4-13.6 MySQL用户管理,常用语句

标签:mysql用户管理   mysql常用语句   

原文地址:http://blog.51cto.com/13578154/2113971

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!