55:Mysql用户管理|经常使用sql语句|mysql数据库备份恢复

一、Mysql用户管理mysql

场景,为了安全,新建的站点,建立新的用户,或者给已有用户受权,对某个库或者某个表有权限;sql

语法: grant   all  on  *.*   to  'user'@'127.0.0.1'  identified by  'password';数据库

grant 后  表示受权: all 表示全部权限(  inser t   delete  update  select ) ; 安全

on  后   表示给那个库,那个表受权( *.* )表示全部库下面的全部表;bash

to  后  表示给那个用户授予权限;   框架

后  表示来源IP,只有来源IP是这个时才容许访问;ide

by 后   表示密码;ui

注释:在mysql界面下,若是输错了字符,而且按了回车键,只需再次输入分号(;),则会退出到登陆界面; spa

注释:退出mysql的命令:   exit    quit        ctrl+dcode

1:建立普通用户user1;

grant   all  on   *.*   to   'user'@'127.0.0.1'  identified  by '123456';

注释:'user'@'127.0.0.1' 表示指定用户@来源IP(指定IP时能够写%,表示指定全部IP),若是指定来源IP,只能经过来源IP来登陆了;

注释   *.* 表示全部库.全部表(第一个*表示全部库), mysql.*  表示mysql下的全部表;

注释  identified  by   '123456'   表示指定mysql的密码;

注释: grant的语句是不会记录到命令历史里的,由于不安全;

mysql> grant all on *.* to 'user1'@'127.0.0.1' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

2:退出mysql,使用用户user1登陆;

[root@localhost_001 ~]# mysql -uuser1 -p123456
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'user1'@'localhost' (using password: YES)

3:使用user1会看到登陆失败,由于它默认是sock,须要指定-h指定IP来登陆;会看到以user1用户登陆数据库;

[root@localhost_001 ~]# mysql -uuser1  -p123456 -h127.0.0.1
Welcome to the MySQL monitor.  Commands end with ; or \g.
mysql>

注释:那如何不指定-h来登陆,须要给localhost受权本地登陆,而后用sock去链接;

mysql> grant all on *.* to 'user1'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

4:如今不知道-h也能够登陆了,由于如今受权是针对的localhost;

[root@localhost_001 ~]# mysql -uuser1  -p123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
mysql>

二、针对具体的权限去受权;

格式:grant   select   update   insert    on   db.*   to   'user2'@'192.168.149.130'   idenfified by  '123456';

针对来源IP是192.168.149.130的user2用户,给它授予db库的(select  update  intsert)的权限;

注释:show  grants;       查看当前用户的权限(登陆某一个用户的状况下);

1:运行第一条命令,建立user2用户及来源IP192.168.149.130,不给删除的权限;以下;

mysql> grant select,update,insert on db.* to 'user3'@'192.168.149.130' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

2:而后使用来源IP192.168.149.130登陆user3用户,并执行删除操做(以上并无给删除的权限);

mysql> drop table db1.t1;
ERROR 1142 (42000): DROP command denied to user 'user3'@'localhost' for table 't1'

而插入操做就能够执行;
mysql> insert into db1.t1 values (1, 'abc');
Query OK, 1 row affected (0.00 sec)

3:show  grants;   查看当前用户的权限;

[root@localhost_001 ~]# mysql -uuser3  -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
mysql> show grants;
+--------------------------------------------------------------------------------------------+
| Grants for user3@localhost                                                                 |
+--------------------------------------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE ON *.* TO 'user3'@'localhost' IDENTIFIED BY PASSWORD <secret> |
+--------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

注释:  show   grants  for  user3@'192.168.148.130';      查看指定用户user3的权限;

mysql> show grants for user3@'192.168.149.130';
+-------------------------------------------------------------------------------------------------------------------------------------+
| Grants for user3@192.168.149.130                                                                                                    |
+-------------------------------------------------------------------------------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE ON *.* TO 'user3'@'192.168.149.130' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
+-------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

有一种状况,好比用user3给192.168.149.130作了受权,发现不够,还须要给192.168.149.132作受权,也就是说user3不只要使用192.168.149,130链接,也须要使用192.168.149,132链接,这时候须要把受权的命令都执行一遍;

这时候就能够经过show  grants  for  user3@'192.168.149.130'把指定用户额权限打印出来,而后直接复制在运行(修改IP地址)参数便可;

mysql> GRANT SELECT, INSERT, UPDATE ON *.* TO 'user'@'192.168.149.130' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

查看user3及来源IP是192.168.149.132;以下;

mysql> show grants for user3@'192.168.149.132';
+-------------------------------------------------------------------------------------------------------------------------------------+
| Grants for user3@192.168.149.132                                                                                                    |
+-------------------------------------------------------------------------------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE ON *.* TO 'user3'@'192.168.149.132' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
+-------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

注释:在使用show   grant来查看时,除了IP地址不同,其余都相同;

注释:有时候在不知mysql的密码时,在建立用户时也能够这样来指定密码;

经常使用sql语句,增    删  改  查;

select count(*) from mysql.user;
select * from mysql.db;
select db from mysql.db;
select db,user from mysql.db;
select * from mysql.db where host like '192.168.%';
insert into db1.t1 values (1, 'abc');
update db1.t1 set name='aaa' where id=1;
truncate table db1.t1;
drop table db1.t1;
drop database db1;

1:查看数据的全部表;    show  tables;        查看表的行数;     show  count(*)  from      mysql.user;

mysql> select count(*) from mysql.user;
+----------+
| count(*) |
+----------+
|       18 |
+----------+
1 row in set (0.00 sec)

注释:查看全部的内容 select * from mysql.db;(这样看起来会很乱) ——>能够在后面加上\G,如select * from mysql.db\G;

不建议使用上面命令select  *命令,会比较耗费资源和内存;

myisam引擎,可以自动统计行,select会比较快;

innodb引擎,不会自动统计行数,每次查询每次统计,比较耗费资源;

3:查看db库的全部内容(第一个db是字段);

mysql> select db from mysql.db;
+---------+
| db      |
+---------+
| test    |
| test\_% |
| db      |
| db      |
+---------+
4 rows in set (0.01 sec)

4:查看db字段和user字段;    select   db,user  from  mysql.db;

mysql> select db,user from mysql.db;
+---------+-------+
| db      | user  |
+---------+-------+
| test    |       |
| test\_% |       |
| db      | user2 |
| db      | user2 |
+---------+-------+
4 rows in set (0.00 sec)

注释:查询 select * from mysql.db where host like '192.168.%';  like 就是模糊匹配;

二、插入语句;

1:查看建立的表;  desc   db1.t1;               发现内容为空:select * from 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)

2:插入一条数据;     insert  into   db1.t1  values  (2,'def');

mysql> insert into db1.t1 values (2, 'def');
Query OK, 1 row affected (0.00 sec)
mysql> insert into db1.t1 values (2, 234);
Query OK, 1 row affected (0.00 sec)
mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|    1 | abc  |
|    2 | def  |
|    1 | 234  |
|    1 | 234  |
|    3 | 256  |
|    2 | 234  |
+------+------+
6 rows in set (0.00 sec)

注释:在插入一条数据时,name字段是一个字符串,字符串须要加上一个双引号,而数字则不须要加双引号;

3:update操做

删除db1.t1表中id为1的的数据;       delete   from   db1.t1  where   id=1;

mysql> delete from db1.t1 where id=1;
Query OK, 3 rows affected (0.00 sec)

mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|    2 | def  |
|    3 | 256  |
|    2 | 234  |
+------+------+
3 rows in set (0.00 sec)

truncate   清空一个表;       truncate     db1.t1;

mysql> truncate db1.t1;
Query OK, 0 rows affected (0.18 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(40) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

注释:即便表清空了,可是字段内容仍是在的;

truncate只是清空表的内容,而drop会删除表的内容并把表的框架也删除;

mysql> drop table db1.t1;          #删除表;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from db1.t1;
ERROR 1146 (42S02): Table 'db1.t1' doesn't exist

truncate     db1.t1;                    清空一个表;

drop   tables  db1.t1;                 删除表;

drop   databases   db1;             删除数据库;

注释以上操做尽可能少用,要是数据库没了就玩完了;

四、mysql的数据库的备份和恢复;  mysqldump 

注释:在执行mysqldump   -uroot -pnihao123!  直接回车时候会看到显示好多信息,屏幕上显示的这些就是备份的数据;

备份库;备份mysql的库到/tmp/mysqbak.sql

[root@localhost_001 ~]# mysqldump -uroot -pnihao123! mysql > /tmp/mysqlbak.sql
Warning: Using a password on the command line interface can be insecure.

恢复库;能够先手动建立一个mysql2库用来恢复;

[root@localhost_001 ~]# mysql -uroot -pnihao123! -e "create database mysql2"
Warning: Using a password on the command line interface can be insecure.
[root@localhost_001 ~]# mysql -uroot -pnihao123! mysql2 < /tmp/mysqlbak.sql 
Warning: Using a password on the command line interface can be insecure.

注释:在后面加一个mysql2 就会进入到mysql2数据库里面;

[root@localhost_001 ~]# mysql -uroot -pnihao123! mysql2

查看当前所在的库;   select   database();

mysql> select database();
+------------+
| database() |
+------------+
| mysql2     |
+------------+
1 row in set (0.00 sec)

备份表:针对数据库里的一个表备份,只须要在库后面加上表的名字便可备份;

注释:先库再表,中间是空格;

备份的时候,库存在的话,先把库drop掉,而后建立库,表存在的话,先把表drop掉,而后建立表,而后在一步一步的插入每一行数据;

备份表:    mysqldump  -uroot -pnihao123!   mysql  user  > /tmp/user.sql

[root@localhost_001 ~]# mysqldump -uroot -pnihao123! mysql user >/tmp/user.sql
Warning: Using a password on the command line interface can be insecure.

恢复表:   mysql   -uroot   -pnihao123!   mysql2  <  /tmp/user.sql

[root@localhost_001 ~]# mysql -uroot -pnihao123! mysql2 < /tmp/user.sql 
Warning: Using a password on the command line interface can be insecure.

备份全部库;   -A  是全部库的意思;

[root@localhost_001 ~]# mysqldump -uroot -pnihao123 -A > /tmp/mysql_all.sql
Warning: Using a password on the command line interface can be insecure.

也能够只备份表结构,不会备份数据;   -d

[root@localhost_001 ~]# mysqldump -uroot -pnihao123 -d > /tmp/mysql.sql
Warning: Using a password on the command line interface can be insecure.

示例:两个机器的库备份,一个库备份到另外一台机器;

首先两台机器可以通讯;

而后mysqldump -h远程mysql的IP    -uuser     -ppassword       dbname  >   /本地bakup.sql

这样既可备份;

相关文章
相关标签/搜索