##mysql用户管理
创建用户
创建一个user1,受所有权限,密码是123456,只能在127.0.0.1机器下登录。给与的权限是所有(grant all),所有的数据库中所有的表(*.*)
mysql> grant all on *.* to 'user1'@'127.0.0.1' identified by '123456';
Query OK, 0 rows affected (0.45 sec)
创建user2用户,密码为000000,并授予其针对db1库SELECT,UPDATE,INSERT权限
MySQL > grant SELECT,UPDATE,INSERT on db1.* to'user2'@'192.168.1.11' identified by '000000';
创建user3,密码为000000,并针对所有IP授予其db1库所有权限
MySQL > grant all on db1.* to 'user3'@'%' identified by '000000';
当然,也可以使用create user ‘test’@’%’ identified by ‘123456’; 创建用户并授权。
grant all 代表所有的权限
*.* 第一个*代表库名。后面.*代表库中的表名
% 通配所有的IP
[root@localhost ~]# mysql -uuser1 -p000000
ERROR 1045 (28000): Access denied for user 'user1'@'localhost' (using password: YES)
[root@localhost ~]# mysql -uuser1 -p000000 -h127.0.0.1
因为默认使用sock通信,所以使用-h指定主机
查看用户的授权信息
1. 查看当前用户的授权信息
MySQL > show grants;
2. 查看指定用户的授权信息。root用户可用
MySQL > show grants for [email protected];
mysql> show grants for user1@'127.0.0.1';
+-----------------------------------------------------------------------------------------------------------------------+
| Grants for [email protected] |
+-----------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'user1'@'127.0.0.1' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.02 sec)
##常用sql语句
sql语句,对数据库操作无外乎增,删,改,查。
查找mysql库user表中的行数
mysql> select count(*) from mysql.user;
+----------+
| count(*) |
+----------+
| 7 |
+----------+
1 row in set (0.07 sec)
查询mysql库db表中的所有内容
select * from mysql.db\G;
查询mysql库db表中的db字段内容
mysql> select db from mysql.db;
+---------+
| db |
+---------+
| test |
| test\_% |
+---------+
2 rows in set (0.10 sec)
查询mysql库db表中的db和user字段内容
mysql> select db,user from mysql.db;
+---------+------+
| db | user |
+---------+------+
| test | |
| test\_% | |
+---------+------+
2 rows in set (0.00 sec)
来自ip模糊查询mysql库中的db表的所有的内容
select * from mysql.db where host like '192.168.%';
db1库t1表中插入数据(1 , ‘abc’)
mysql> insert into db1.t1 values (1, 'abc');
Query OK, 1 row affected (0.17 sec)
mysql> select * from db1.t1;
+------+------+
| id | name |
+------+------+
| 1 | abc |
+------+------+
1 row in set (0.04 sec)
db1库t1表中更新数据
mysql> update db1.t1 set name='aaa' where id=1;
Query OK, 1 row affected (0.15 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from db1.t1;
+------+------+
| id | name |
+------+------+
| 1 | aaa |
+------+------+
1 row in set (0.00 sec)
删除表中某条数据delete from db1.t1 where id=1;
mysql> select * from db1.t1;
+------+------+
| id | name |
+------+------+
| 1 | aaa |
| 2 | abc |
+------+------+
2 rows in set (0.00 sec)
mysql> delete from db1.t1 where id=1;
Query OK, 1 row affected (0.03 sec)
mysql> select * from db1.t1;
+------+------+
| id | name |
+------+------+
| 2 | abc |
+------+------+
1 row in set (0.00 sec)
清空表中的所有内容,表结构保留truncate table db1.t1;
mysql> select * from db1.t1;
+------+------+
| id | name |
+------+------+
| 2 | abc |
+------+------+
1 row in set (0.00 sec)
mysql> truncate db1.t1;
Query OK, 0 rows affected (0.24 sec)
mysql> select * from db1.t1;
Empty set (0.00 sec)
mysql> desc db1.t1;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(4) | YES | | NULL | |
| name | char(20) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
删除库表,表结构同时删除
drop table db1.t1;
drop database db1;
对运维来讲,使用删除(drop, truncate)命令,一定要慎重,在小心都不过分。一旦删除错了某表,字段,那就损失惨重!
##Mysql数据库备份和恢复
是在linux下操作的
备份库
mysqldump -uroot -p000000 库名 > /tmp/mysql.sql
将mysql库备份为/tmp/mysql.sql
[root@shuai-01 ~]# mysqldump -uroot -p111111 mysql > /tmp/mysql.sql
恢复库 mysql -uroot -p000000 mysql < /tmp/mysql.sql
[root@shuai-01 ~]# mysql -uroot -p111111 mysql < /tmp/mysql.sql
备份表 mysqldump -uroot -p000000 mysql user > /tmp/user.sql 库后面空格加上表名
恢复表 mysql -uroot -p000000 mysql < /tmp/user.sql
只用写库名就行了。
备份所有库 mysqldump -uroot -p000000 -A >/tmp/123.sql
只备份表结构 mysqldump -uroot -p000000 -d mysql > /tmp/mysql.sql
备份用mysqldump , 恢复用mysql
mysqldump针对比较小的数据进行备份比较合适。
扩展
SQL语句教程 http://blog.51cto.com/zt/206
什么是事务?事务的特性有哪些? http://blog.csdn.net/yenange/article/details/7556094
根据binlog恢复指定时间段的数据 http://www.centoscn.com/mysql/2015/0204/4630.html
mysql字符集调整 http://xjsunjie.blog.51cto.com/999372/1355013
使用xtrabackup备份innodb引擎的数据库 innobackupex 备份 Xtrabackup 增量备份
http://zhangguangzhi.top/2017/08/23/innobackex工具备份mysql数据/#三、开始恢复mysql
相关视频
链接:http://pan.baidu.com/s/1miFpS9M 密码:86dx
链接:http://pan.baidu.com/s/1o7GXBBW 密码:ue2f