1. 程式人生 > >MySQL 建立使用者以及授權,常用的語句,利用mysqldump備份與恢復

MySQL 建立使用者以及授權,常用的語句,利用mysqldump備份與恢復

MySQL建立使用者以及授權,授權資訊檢視

grant語句因為是不安全的,所以在mysql的歷史資訊中看不見,及通過上下鍵翻不會翻到該語句。

[root@test-a ~]# mysql -uroot -p'test111' # root使用者登入
mysql: [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 2
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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 databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| sys                |
| test_db0           |
+--------------------+
6 rows in set (0.10 sec)

mysql> use test_db0;
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> grant all on *.* to 'mysql_user1' identified by 'test111'; -- 授權並建立使用者
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
[root@test-a ~]# mysql -umysql_user1 -p'test111' -- mysql_user1登入
mysql: [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.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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>  CREATE TABLE `test_tb1` ( `id` int(4) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- 建立一個新表test_tb1
Query OK, 0 rows affected (0.01 sec)

mysql> quit
Bye
[root@test-a ~]# mysql -uroot -p'test111' # root登入
mysql: [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.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> grant all on test_db0.test_tb1 to 'mysql_user2' identified by 'test111'; -- 建立mysql_user2使用者並授權test_db0資料庫的test_tb1表所有許可權給test_tb1
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye
[root@test-a ~]# mysql -umysql_user2 -p'test111' # 登入
mysql: [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.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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 test_db0; # 切換到test_db0庫
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;  -- 檢視所有的資料表,沒有test_db0
+--------------------+
| Tables_in_test_db0 |
+--------------------+
| test_tb1           |
+--------------------+
1 row in set (0.00 sec)


[root@test-a ~]# mysql -umysql_user1 -p'test111' # mysql_user2登入
mysql: [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.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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 test_db0; -- 同樣切換到test_db0庫
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; -- 可以檢視到test_tb0和test_tb1表
+--------------------+
| Tables_in_test_db0 |
+--------------------+
| test_tb0           |
| test_tb1           |
+--------------------+
2 rows in set (0.00 sec)

mysql> show grants; -- 檢視當前登入使用者的所有授權
+---------------------------------------------------------------------+
| Grants for root@localhost                                           |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION        |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> show grants for mysql_user2;  -- 檢視mysql_user2的授權
+--------------------------------------------------------------------+
| Grants for mysql_user2@%                                           |
+--------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'mysql_user2'@'%'                            |
| GRANT ALL PRIVILEGES ON `test_db0`.`test_tb1` TO 'mysql_user2'@'%' |
+--------------------------------------------------------------------+

mysql>  grant SELECT,UPDATE,INSERT on  test_db0.* to 'mysql_user4'@'192.168.77.134' identified by 'test111'; -- 給從192.168.77.134登入的mysql_user4使用者授權
mysql> show grants for mysql_user4;  -- 這時會提示需要指定host
ERROR 1141 (42000): There is no such grant defined for user 'mysql_user4' on host '%'

mysql> show grants for mysql_user4@'192.168.77.134';
+--------------------------------------------------------------------------------+
| Grants for [email protected]                                          |
+--------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'mysql_user4'@'192.168.77.134'                           |
| GRANT SELECT, INSERT, UPDATE ON `test_db0`.* TO 'mysql_user4'@'192.168.77.134' |
+--------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

常用SQL語句

mysql> select count(*) from mysql.user; -- 檢視使用者數
+----------+
| count(*) |
+----------+
|        7 |
+----------+
1 row in set (0.30 sec)

mysql> select * from mysql.db\G  -- 查看錶資料

mysql> select db from mysql.db; -- 從db中讀取所有db欄位
+--------------------+
| db                 |
+--------------------+
| test_db0           |
| performance_schema |
| sys                |
+--------------------+
3 rows in set (0.00 sec)

mysql> select db,user from mysql.db; -- 多個欄位查詢
+--------------------+---------------+
| db                 | user          |
+--------------------+---------------+
| test_db0           | mysql_user4   |
| performance_schema | mysql.session |
| sys                | mysql.sys     |
+--------------------+---------------+
3 rows in set (0.03 sec)

mysql> select db,user,host from mysql.db where host like '192.168.%'; -- 模糊匹配查詢
+----------+-------------+----------------+
| db       | user        | host           |
+----------+-------------+----------------+
| test_db0 | mysql_user4 | 192.168.77.134 |
+----------+-------------+----------------+
1 row in set (0.03 sec)


mysql> desc test_tb0; -- 查看錶欄位
+-------+--------+------+-----+---------+-------+
| Field | Type   | Null | Key | Default | Extra |
+-------+--------+------+-----+---------+-------+
| id    | int(4) | YES  |     | NULL    |       |
+-------+--------+------+-----+---------+-------+
1 row in set (0.07 sec)


mysql> insert into test_tb0 values (1); -- 插入資料
Query OK, 1 row affected (0.09 sec)

mysql> select * from test_tb0;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

mysql> update test_tb0 set id=2; -- 把id的所有值改為2
Query OK, 1 row affected (0.08 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from test_tb0;
+------+
| id   |
+------+
|    2 |
+------+
1 row in set (0.00 sec)


mysql> delete from test_tb0 where id=2; -- 刪除資料
Query OK, 1 row affected (0.05 sec)

mysql> select * from test_tb0;
Empty set (0.00 sec)

mysql> insert into test_tb0 values (1);
Query OK, 1 row affected (0.00 sec)

mysql> truncate table test_tb0; -- 清空庫
Query OK, 0 rows affected (0.34 sec)

mysql> desc test_tb0;
+-------+--------+------+-----+---------+-------+
| Field | Type   | Null | Key | Default | Extra |
+-------+--------+------+-----+---------+-------+
| id    | int(4) | YES  |     | NULL    |       |
+-------+--------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> select * from test_tb0;
Empty set (0.00 sec)

mysql> drop table test_tb0; -- 刪除表
Query OK, 0 rows affected (0.09 sec)

mysql> desc test_tb0;
ERROR 1146 (42S02): Table 'test_db0.test_tb0' doesn't exist

備份與恢復

[root@test-a ~]# mysqldump -uroot -p'test111' mysql > /tmp/mysql.sql # 備份mysql庫
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@test-a ~]# mysql -uroot -p'test111' -e 'create database mysql2' # 建立mysql2庫
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@test-a ~]# mysql -uroot -p'test111' mysql2 < /tmp/mysql.sql # 恢復備份到mysql2庫
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@test-a ~]#
[root@test-a ~]# mysql -uroot -p'test111'
mysql: [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 14
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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 databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| mysql2             |
| performance_schema |
| sys                |
| test_db0           |
+--------------------+
7 rows in set (0.00 sec)


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

mysql> select count(*) from mysql2.user;
+----------+
| count(*) |
+----------+
|        7 |
+----------+
1 row in set (0.03 sec)


[root@test-a ~]# mysqldump -uroot -p'test111' mysql2 user > /tmp/mysql2.user.sql # 備份user表
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@test-a ~]# mysql -uroot -p'test111' -e '
> ^C
[root@test-a ~]# mysql -uroot -p'test111' mysql2
mysql: [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 16
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> drop table user; --刪除user表
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
+---------------------------+
| Tables_in_mysql2          |
+---------------------------+
| columns_priv              |
| db                        |
| engine_cost               |
| event                     |
| func                      |
| general_log               |
| gtid_executed             |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| server_cost               |
| 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 |
+---------------------------+
30 rows in set (0.00 sec)

mysql> quit
Bye
[root@test-a ~]# mysql -uroot -p'test111' mysql2 < /tmp/mysql2.user.sql #恢復表
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@test-a ~]# mysql -uroot -p'test111' mysql2
mysql: [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 18
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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 tables;
+---------------------------+
| Tables_in_mysql2          |
+---------------------------+
| columns_priv              |
| db                        |
| engine_cost               |
| event                     |
| func                      |
| general_log               |
| gtid_executed             |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| server_cost               |
| 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                      |
+---------------------------+
31 rows in set (0.00 sec)


[root@test-a ~]# mysqldump -uroot -p'test111' -A > /tmp/all.sql # 備份所有庫
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@test-a ~]# grep 'Current Database:' /tmp/all.sql
-- Current Database: `db1`
-- Current Database: `mysql`
-- Current Database: `mysql2`
-- Current Database: `test_db0`

[root@test-a ~]# mysqldump -uroot -p'test111' -d mysql2 > /tmp/mysql2.struct.sql  # -d 只備份表的結構
mysqldump: [Warning] Using a password on the command line interface can be insecure.