1. 程式人生 > >13.4 mysql用戶管理 13.5 常用sql語句 13.6 mysql數據庫備份恢復

13.4 mysql用戶管理 13.5 常用sql語句 13.6 mysql數據庫備份恢復

13.4 mysql用戶管理 13.5 常用sql語句 13.6 mysql數據庫備份恢復

- 13.4 mysql用戶管理
- 13.5 常用sql語句
- 13.6 mysql數據庫備份恢復
- 擴展
- 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%E5%B7%A5%E5%85%B7%E5%A4%87%E4%BB%BDmysql%E6%95%B0%E6%8D%AE/#%E4%B8%89%E3%80%81%E5%BC%80%E5%A7%8B%E6%81%A2%E5%A4%8Dmysql


# 13.4 MySQL用戶管理
- 場景,為了安全,新建的站點,創建新的用戶,或者給予使用已有賬戶,給予權限
MySQL創建用戶以及授權
```
mysql> grant all on *.* to ‘user1‘@‘127.0.0.1‘ identified by ‘123456a‘;
Query OK, 0 rows affected (0.01 sec)

mysql> 
```
- grant all on *.* to ‘user1’ identified by ‘passwd’;
- grant all on *.* to ‘user1’@’127.0.0.1’ identified by ‘passwd’;
- grant 授權
- all  (查看,創建,刪除等等)
- ‘user1’@’127.0.0.1’  指定用戶@指定來源IP (指定用戶可以寫%,表示所有的IP)如果指定了來源IP,那麽只能通過來源IP登錄
- *.*  所有庫,所有表
- 命令輸錯,直接輸入“ ; ”分號退出

- 使用用戶user1 登錄mysql 看下
```
[root@localhost ~]# mysql -uuser1 -p123456a
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user ‘user1‘@‘localhost‘ (using password: YES)
[root@localhost ~]# 
```
- 不能登錄,為什麽不行呢,因為默認用的是socket
- 授權一個ip
```
[root@localhost ~]# mysql -uuser1 -p123456a -h127.0.0.1
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> quit
Bye
[root@localhost ~]# 
 
```
- 用socket去連
- 先登錄root,再定義localhost
```
[root@localhost ~]# mysql -uroot -paminglinux
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> 


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

mysql> 
```
- 退出來,打錯了用 分號 ; 退出來,除了用 quit 之外還可以用exit  ctrl d
```
mysql> uit
    -> quit
    -> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘uit
quit‘ at line 1
mysql> quit
Bye
[root@localhost ~]# 
```
- 再來試下user1 登錄,就可以了
```
[root@localhost ~]# mysql -uuser1 -p123456a
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> exit
Bye
[root@localhost ~]# 
```
- 用來查看指定用戶的授權是什麽,show grants; (指定root)
 show grants for [email protected];(指定192.168.202.1)
```
[root@localhost ~]# mysql -uroot -paminglinux
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> show grants;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘localhost‘ IDENTIFIED BY PASSWORD ‘*1836D7557E753782F1509748BD403456701A0D2F‘ WITH GRANT OPTION |
| GRANT PROXY ON ‘‘@‘‘ TO ‘root‘@‘localhost‘ WITH GRANT OPTION                                                                           |
+----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> 


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 ‘*B012E8731FF1DF44F3D8B26837708985278C3CED‘ |
+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> 
```

- 再來把user2 做一個授權, 再show grants;
- show grants;看的是root
```
mysql> grant SELECT,UPDATE,INSERT on db1.* to ‘user2‘@‘192.168.202.1‘ identified by ‘passwd‘;  
Query OK, 0 rows affected (0.00 sec)

mysql> 

mysql> show grants;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘localhost‘ IDENTIFIED BY PASSWORD ‘*1836D7557E753782F1509748BD403456701A0D2F‘ WITH GRANT OPTION |
| GRANT PROXY ON ‘‘@‘‘ TO ‘root‘@‘localhost‘ WITH GRANT OPTION                                                                           |
+----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> 

```
- 展示user2的 授權
```
mysql> show grants for user2@‘192.168.202.1‘;
+------------------------------------------------------------------------------------------------------------------+
| Grants for [email protected]                                                                                   |
+------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO ‘user2‘@‘192.168.202.1‘ IDENTIFIED BY PASSWORD ‘*59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0‘ |
| GRANT SELECT, INSERT, UPDATE ON `db1`.* TO ‘user2‘@‘192.168.202.1‘                                               |
+------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> 

mysql> GRANT USAGE ON *.* TO ‘user2‘@‘192.168.202.2‘ IDENTIFIED BY PASSWORD ‘*59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0‘
    -> ;
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT SELECT, INSERT, UPDATE ON `db1`.* TO ‘user2‘@‘192.168.202.2‘;
Query OK, 0 rows affected (0.00 sec)

mysql> 

mysql> show grants for user2@‘192.168.202.2‘;
+------------------------------------------------------------------------------------------------------------------+
| Grants for [email protected]                                                                                   |
+------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO ‘user2‘@‘192.168.202.2‘ IDENTIFIED BY PASSWORD ‘*59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0‘ |
| GRANT SELECT, INSERT, UPDATE ON `db1`.* TO ‘user2‘@‘192.168.202.2‘                                               |
+------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> 
```








# 13.5 常用sql語句
- 先到mysql下
```
[root@localhost ~]# mysql -uroot -paminglinux
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> use db1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> 
```
- 查看表的行數 select count(*) from mysql.user;

```
mysql> select count(*) from mysql.user;
+----------+
| count(*) |
+----------+
|       10 |
+----------+
1 row in set (0.02 sec)

mysql> 
```
- 查看所有的內容
```
mysql> select * from mysql.db;
+---------------+---------+-------+-------------+-------------+-------------+-------------+-------------+-----------+------------+-----------------+------------+------------+-----------------------+------------------+------------------+----------------+---------------------+--------------------+--------------+------------+--------------+
| Host          | Db      | User  | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Create_tmp_table_priv | Lock_tables_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Execute_priv | Event_priv | Trigger_priv |
+---------------+---------+-------+-------------+-------------+-------------+-------------+-------------+-----------+------------+-----------------+------------+------------+-----------------------+------------------+------------------+----------------+---------------------+--------------------+--------------+------------+--------------+
| %             | test    |       | Y           | Y           | Y           | Y           | Y           | Y         | N          | Y               | Y          | Y          | Y                     | Y                | Y                | Y              | Y                   | N                  | N            | Y          | Y            |
| %             | test\_% |       | Y           | Y           | Y           | Y           | Y           | Y         | N          | Y               | Y          | Y          | Y                     | Y                | Y                | Y              | Y                   | N                  | N            | Y          | Y            |
| 192.168.202.1 | db1     | user2 | Y           | Y           | Y           | N           | N           | N         | N          | N               | N          | N          | N                     | N                | N                | N              | N                   | N                  | N            | N          | N            |
| 192.168.202.2 | db1     | user2 | Y           | Y           | Y           | N           | N           | N         | N          | N               | N          | N          | N                     | N                | N                | N              | N                   | N                  | N            | N          | N            |
+---------------+---------+-------+-------------+-------------+-------------+-------------+-------------+-----------+------------+-----------------+------------+------------+-----------------------+------------------+------------------+----------------+---------------------+--------------------+--------------+------------+--------------+
4 rows in set (0.00 sec)

mysql> 
```
- 查看db庫的所有內容 
- 查看表,用戶
```
mysql> select db from mysql.db;
+---------+
| db      |
+---------+
| test    |
| test\_% |
| db1     |
| db1     |
+---------+
4 rows in set (0.00 sec)

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

mysql> 
```

- 模糊查詢。like 模糊匹配
```
mysql> select * from mysql.db where host like ‘192.168.%‘;
+---------------+-----+-------+-------------+-------------+-------------+-------------+-------------+-----------+------------+-----------------+------------+------------+-----------------------+------------------+------------------+----------------+---------------------+--------------------+--------------+------------+--------------+
| Host          | Db  | User  | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Create_tmp_table_priv | Lock_tables_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Execute_priv | Event_priv | Trigger_priv |
+---------------+-----+-------+-------------+-------------+-------------+-------------+-------------+-----------+------------+-----------------+------------+------------+-----------------------+------------------+------------------+----------------+---------------------+--------------------+--------------+------------+--------------+
| 192.168.202.1 | db1 | user2 | Y           | Y           | Y           | N           | N           | N         | N          | N               | N          | N          | N                     | N                | N                | N              | N                   | N                  | N            | N          | N            |
| 192.168.202.2 | db1 | user2 | Y           | Y           | Y           | N           | N           | N         | N          | N               | N          | N          | N                     | N                | N                | N              | N                   | N                  | N            | N          | N            |
+---------------+-----+-------+-------------+-------------+-------------+-------------+-------------+-----------+------------+-----------------+------------+------------+-----------------------+------------------+------------------+----------------+---------------------+--------------------+--------------+------------+--------------+
2 rows in set (0.00 sec)

mysql> select * from mysql.db where host like ‘192.168.%‘\G;   // \G是按照豎行排行
*************************** 1. row ***************************
                 Host: 192.168.202.1
                   Db: db1
                 User: user2
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: N
          Create_priv: N
            Drop_priv: N
           Grant_priv: N
      References_priv: N
           Index_priv: N
           Alter_priv: N
Create_tmp_table_priv: N
     Lock_tables_priv: N
     Create_view_priv: N
       Show_view_priv: N
  Create_routine_priv: N
   Alter_routine_priv: N
         Execute_priv: N
           Event_priv: N
         Trigger_priv: N
*************************** 2. row ***************************
                 Host: 192.168.202.2
                   Db: db1
                 User: user2
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: N
          Create_priv: N
            Drop_priv: N
           Grant_priv: N
      References_priv: N
           Index_priv: N
           Alter_priv: N
Create_tmp_table_priv: N
     Lock_tables_priv: N
     Create_view_priv: N
       Show_view_priv: N
  Create_routine_priv: N
   Alter_routine_priv: N
         Execute_priv: N
           Event_priv: N
         Trigger_priv: N
2 rows in set (0.00 sec)

ERROR: 
No query specified

mysql> 
```

-  插入1, ‘abc’到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.01 sec)

mysql> select * from db1.t1;
Empty set (0.09 sec)

mysql> 

mysql> select * from db1.ti;
ERROR 1146 (42S02): Table ‘db1.ti‘ doesn‘t exist
mysql> select * from db1.t1;
Empty set (0.09 sec)

mysql> insert into db1.t1 values (1, ‘abc‘);
Query OK, 1 row affected (0.01 sec)

mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|    1 | abc  |
+------+------+
1 row in set (0.00 sec)

mysql> 


mysql> insert into db1.t1 values (1, ‘234‘);
Query OK, 1 row affected (0.01 sec)

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

mysql> 

mysql> insert into db1.t1 values (1, ‘234‘);
Query OK, 1 row affected (0.02 sec)

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

mysql> 

```
-  更改db1.t1表 的字符串為name 的數據 和 字符串為id 的數據
```
mysql> update db1.t1 set name=‘aaa‘ where id=1;
Query OK, 3 rows affected (0.01 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|    1 | aaa  |
|    1 | aaa  |
|    1 | aaa  |
+------+------+
3 rows in set (0.00 sec)

mysql> 


mysql> update db1.t1 set id=2 where name=‘aaa‘;
Query OK, 3 rows affected (0.00 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)

mysql> 
```
- 刪除t1表
```
mysql> delete from db1.t1 where id=2;
Query OK, 3 rows affected (0.00 sec)

mysql> select * from db1.t1;
Empty set (0.00 sec)

mysql> 

```
- 重新插入一個ti表
```
mysql> insert into db1.t1 values (1, ‘234‘);
Query OK, 1 row affected (0.00 sec)

mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|    1 | 234  |
+------+------+
1 row in set (0.00 sec)

mysql> 
```
-  truncate db1.t1; 僅僅是清除表裏的數據,清空數據
```
mysql> truncate db1.t1;
Query OK, 0 rows affected (0.28 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)

mysql> 
```
-  drop table t1;  連表帶殼全部清除  丟掉表
```
mysql> drop table t1;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from db1.t1;
ERROR 1146 (42S02): Table ‘db1.t1‘ doesn‘t exist
mysql> 
```
-  drop database db1; 把數據庫給幹掉了
```
mysql> drop database db1;
Query OK, 0 rows affected (0.00 sec)

mysql> 
```
- myisam引擎的庫的好處是,能自動去統計行數

盡量少用 * 這樣操作,如果是大表會很耗時








# 13.6 MySQL數據庫備份恢復
-
```
[root@localhost ~]# mysqldump -uroot -paminglinux mysql



  `query_time` time NOT NULL,
  `lock_time` time NOT NULL,
  `rows_sent` int(11) NOT NULL,
  `rows_examined` int(11) NOT NULL,
  `db` varchar(512) NOT NULL,
  `last_insert_id` int(11) NOT NULL,
  `insert_id` int(11) NOT NULL,
  `server_id` int(10) unsigned NOT NULL,
  `sql_text` mediumtext NOT NULL,
  `thread_id` bigint(21) unsigned NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT=‘Slow log‘;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2017-10-30 23:00:48
[root@localhost ~]# 
```
- 這些都是就是這個mysql庫裏的東西,備份就是把這些東西 重定向到指定的文件中去
```
[root@localhost ~]# mysqldump -uroot -paminglinux mysql > /tmp/mysqlbak.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost ~]# 
```
- mysqlbak.sql就是我們備份的 msyql庫文件
- 創建一個新的mysql2庫
```
[root@localhost ~]# mysql -uroot -paminglinux -e "create database mysql2"
Warning: Using a password on the command line interface can be insecure.
[root@localhost ~]# 
```
- 要給它恢復回去就用這個命令
```
[root@localhost ~]# mysql -uroot -paminglinux mysql2 < /tmp/mysqlbak.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -paminglinux mysql2 
Warning: Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> 


```
- 這個mysql2 和mysql一樣
```
mysql> select database();
+------------+
| database() |
+------------+
| mysql2     |
+------------+
1 row in set (0.00 sec)

mysql> show tables;
+---------------------------+
| Tables_in_mysql2          |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
28 rows in set (0.00 sec)

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
28 rows in set (0.01 sec)

mysql> 
mysql> quit
Bye


```
- 備份 表user
```
[root@localhost ~]# mysqldump -uroot -paminglinux mysql user > /tmp/user.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost ~]# 


[root@localhost ~]# less /tmp/user.sql




--
-- Table structure for table `user`
--

DROP TABLE IF EXISTS `user`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `user` (
  `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT ‘‘,
/tmp/user.sql
```
- 恢復 一個表,恢復mysql2 裏面的user表
```
[root@localhost ~]# mysql -uroot -paminglinux mysql2 < /tmp/user.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost ~]# 
```
- 備份所有庫
```
[root@localhost ~]# mysqldump -uroot -paminglinux -A > /tmp/mysql_all.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost ~]# 
```
- 只備份表結構,不備份數據 mysqldump -uroot -paminglinux -d mysql2 > /tmp/mysql2.sql
```
[root@localhost ~]# mysqldump -uroot -paminglinux -d mysql2 > /tmp/mysql2.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost ~]# less /tmp/mysql2.sql
```
- [x] 總結

- 備份庫  

mysqldump -uroot -p123456 mysql > /tmp/mysql.sql

- 恢復庫   //恢復是,必須保證目錄一致

mysql -uroot -p123456 mysql < /tmp/mysql.sql

- 備份表 

mysqldump -uroot -p123456 mysql user > /tmp/user.sql

- 恢復表 

mysql -uroot -p123456 mysql < /tmp/user.sql

- 備份所有庫 

mysqldump -uroot -p -A >/tmp/123.sql

- 只備份表結構 

mysqldump -uroot -p123456 -d mysql > /tmp/mysql.sql


- 在mysql下進行導入數據

進入到mysql 
切換至需要導入的數據庫,use

執行source 文件所在路徑;







- 擴展
- 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%E5%B7%A5%E5%85%B7%E5%A4%87%E4%BB%BDmysql%E6%95%B0%E6%8D%AE/#%E4%B8%89%E3%80%81%E5%BC%80%E5%A7%8B%E6%81%A2%E5%A4%8Dmysql


13.4 mysql用戶管理 13.5 常用sql語句 13.6 mysql數據庫備份恢復