13.4-13.6 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连接。

[[email protected] ~]# 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)

正确连接方式,

[[email protected] ~]# mysql -uuser1 -p123456a  -h127.0.0.1

mysql>

2.1 授权localhost连接

利用root进入mysql修改,

[[email protected] ~]# mysql -uroot -paminglinux

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

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

[[email protected] ~]# 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 [email protected]'127.0.0.1';

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

| Grants for [email protected]                                                                                            |

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

| 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 [email protected]'172.18.171.157'

-> ;

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

| Grants for [email protected]                                                                                   |

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

| 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 [email protected]                                                                                   |

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

| 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'

总结:

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

慎用删除功能。

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

时间: 2024-07-29 15:07:29

13.4-13.6 MySQL用户管理,常用语句的相关文章

mysql用户管理, 常用sql语句,mysql数据库备份恢复

mysql用户管理 新创建一个指定IP的用户,可以访问mysql的某些库某些表. 所有库的所有表,如果想指定访问某个库某些表,只需要修改名称user1 指定用户名br/>@后面的ip是指定ip,%表示所有的ipindentified by 后面是用户的密码验证用用户user1登录也可以指定localhost,登录时不输入本机ip地址即可访问查看授权,用于授权给新用户,新权限: 常用sql 语句 查看库表的行数搜索:select count() from mysql.user;搜索:select

13.4 mysql用户管理 13.5 常用sql语句 13.6 mysql数据库备份恢复

13.4 mysql用户管理 grant all on . to 'user1' identified by 'passwd';mysql> grant all on . to 'user1' identified by 'passwd';Query OK, 0 rows affected (0.01 sec) grant SELECT,UPDATE,INSERT on db1. to 'user2'@'192.168.15.132' identified by 'passwd';mysql>

mysql用户管理、常用语句、数据分备份恢复

mysql用户管理 创建用户并授权 指定登录ip 使用root用户登录录创建授权新用户:mysql> grant all on . to 'user1'@'127.0.0.1' identified by '123456':// all 所有操作(增删查改)// 第一个 通配所有库名,第二个通配所有表名// user1 为用户名// 127.0.0.1 指定登录ip,可用通配符%表示所有ip.// '123456' 为user1用户的登录密码Query OK, 0 rows affected (

MySQL用户管理、常用sql语句、数据库备份

13.4 MySQL用户管理 创建用户并授权 指定登录IP [[email protected] ~]# mysql -uroot -pEnter password: Welcome to the MySQL monitor.mysql> grant all on . to 'user1'@'127.0.0.1' identified by '123456';#创建user1用户并授予其所有权限"."(通配符)#第一个表示db_name:第二个表示tb_name#同时指定其来源I

2.MySQL用户管理,常用SQL语句,MySQL数据库备份与恢复

[toc] MySQL用户管理,重用SQL语句,MySQL数据库备份与恢复 一.MySQL用户管理 1.创建一个普通用户并授权 首先启动mysql,然后进入 [[email protected] ~]# /etc/init.d/mysqld start Starting MySQL... SUCCESS! [[email protected] ~]# mysql -uroot -pxavilinux Warning: Using a password on the command line in

mysql用户管理、常用sql语句、mysql数据库备份恢复

mysql用户管理 1.新增用户user1,并设置密码为123456 mysql> grant all on *.* to 'user1'@'127.0.0.1' identified by '123456'; #创建user1用户并授予其所有权限"*.*"(通配符) #第一个*:表示所有的数据库 #第二个*:表示所有的表 #127.0.0.1表示来源IP,指的只有这个IP可以连接:'%':代表所有的ip #identified by 设置密码 2.对user1用户进行授权管理

MySQL用户管理、sql常用语句、mysql备份与恢复

MySQL用户管理 创建用户 grant all on *.* to 'user1'@'localhost' identified by '123456'; grant all on db1.* to 'user2'@'%' identified by '123456'; //创建user2用户,所有ip都能登录,指定权限为db1库下的所有表: flush privileges; 刷新授权 .:表示所有库和表:user1:用户名:localhost:登录ip,默认localhost为本机登录ip

MySQL常用操作(2)MySQL用户管理、常用sql语句、 MySQL数据库备份恢复

                MySQL用户管理 创建一个普通用户并且授权 1.grant all on *.* to 'user1' identified by 'passwd'; grant all on *.* to 'user1' identified by '123456'; (创建user1用户,all表示所有权限(读.写,增.删.改.查等):*.*,前面的*表示所有的数据库,后面的*表示所有的表:identified by后面跟密码,要用单引号''引起来) grant all o

53.mysql用户管理、常用sql语句、mysql数据库备份恢复

一..mysql用户管理 grant all on *.* to 'user1'@'127.0.0.1' identified by 'passwd'; //创建以127.0.0.1访问的用户user1,密码为passwd,对所有库的所有表拥有所有权限 grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.127.1' identified by 'passwd'; //创建以192.168.133.1访问的user2用户,密码为pass