Mysql使用者管理、常用SQL語句及Mysql的備份與恢復
阿新 • • 發佈:2018-12-06
12月6日任務
13.4 mysql使用者管理
13.5 常用sql語句
13.6 mysql資料庫備份恢復
MySQL使用者管理
建立使用者
- 指定具體ip
# 這裡指定了具體的ip
# *.*:第一個*表示任意的資料庫,第二個*表示任意表
mysql> grant all on *.* to 'test1'@'127.0.0.1' identified by '1234456';
Query OK, 0 rows affected (0.03 sec)
不指定ip無法登入
[[email protected] ~]# mysql -utest1 -p123456
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'test1'@'localhost' (using password: YES)
-h指定ip後成功登入
[[email protected] ~]# mysql -utest1 -p123456 -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 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> quit Bye
- 限制命令
# 對test2使用者只給予SELECT,UPDATE,INSERT許可權
mysql> grant SELECT,UPDATE,INSERT on db1.* to 'test2'@'192.168.65.1' identified by '111';
Query OK, 0 rows affected (0.01 sec)
# 檢視[email protected]'192.168.65.1'的許可權
mysql> show grants for [email protected]'192.168.65.1';
+-----------------------------------------------------------------------------------------------------------------+
| Grants for [email protected] |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test2'@'192.168.65.1' IDENTIFIED BY PASSWORD '*832EB84CB764129D05D498ED9CA7E5CE9B8F83EB' |
| GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'test2'@'192.168.65.1' |
+-----------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
- 任意ip
# %表示任意的意思
mysql> grant all on db1.* to 'test3'@'%' identified by '111';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for test3;
+------------------------------------------------------------------------------------------------------+
| Grants for [email protected]% |
+------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test3'@'%' IDENTIFIED BY PASSWORD '*832EB84CB764129D05D498ED9CA7E5CE9B8F83EB' |
| GRANT ALL PRIVILEGES ON `db1`.* TO 'test3'@'%' |
+------------------------------------------------------------------------------------------------------+
- 同一個使用者指定多個ip
# 這裡以test2為例
# 通過show grants命令檢視建立命令,其內的密碼時加密後的,這裡我們無法使用下列的命令直接建立
# grant SELECT,UPDATE,INSERT on db1.* to 'test2'@'192.168.65.2' identified by '*832EB84CB764129D05D498ED9CA7E5CE9B8F83EB';
# 但是我們可以通過修改顯示出的命令,實現建立目的
mysql> show grants for [email protected]'192.168.65.1';
+-----------------------------------------------------------------------------------------------------------------+
| Grants for [email protected] |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test2'@'192.168.65.1' IDENTIFIED BY PASSWORD '*832EB84CB764129D05D498ED9CA7E5CE9B8F83EB' |
| GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'test2'@'192.168.65.1' |
+-----------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
# 將檢視到的命令進行修改,如下所示
mysql> GRANT USAGE ON *.* TO 'test2'@'192.168.65.2' IDENTIFIED BY PASSWORD '*832EB84CB764129D05D498ED9CA7E5CE9B8F83EB';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'test2'@'192.168.65.2';
Query OK, 0 rows affected (0.00 sec)
# 可以檢視密碼並沒有修改
mysql> show grants for [email protected]'192.168.65.2';
+-----------------------------------------------------------------------------------------------------------------+
| Grants for [email protected] |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test2'@'192.168.65.2' IDENTIFIED BY PASSWORD '*832EB84CB764129D05D498ED9CA7E5CE9B8F83EB' |
| GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'test2'@'192.168.65.2' |
+-----------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
簡單常用SQL語句
- 查看錶內的行數
mysql> select count(*) from mysql.user;
+----------+
| count(*) |
+----------+
| 11 |
+----------+
1 row in set (0.03 sec)
- 獲取表的內容
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.65.1 | db1 | test2 | Y | Y | Y | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N |
| 192.168.65.2 | db1 | test2 | Y | Y | Y | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N |
| % | db1 | test3 | Y | Y | Y | Y | Y | Y | N | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y |
+--------------+---------+-------+-------------+-------------+-------------+-------------+-------------+-----------+------------+-----------------+------------+------------+-----------------------+------------------+------------------+----------------+---------------------+--------------------+--------------+------------+--------------+
5 rows in set (0.00 sec)
# 使用\G代替;,使顯示更規整
mysql> select * from mysql.db\G
- 查看錶的欄位
# 查看錶的某個欄位
mysql> select db from mysql.db;
+---------+
| db |
+---------+
| db1 |
| test |
| test\_% |
| db1 |
| db1 |
+---------+
5 rows in set (0.00 sec)
# 查看錶的多個欄位,欄位間使用,分割
mysql> select db,user from mysql.db;
+---------+-------+
| db | user |
+---------+-------+
| db1 | test3 |
| test | |
| test\_% | |
| db1 | test2 |
| db1 | test2 |
+---------+-------+
5 rows in set (0.00 sec)
- 檢視指定ip/ip範圍的欄位
# like表示模糊匹配,這裡%可以是1-255的任意一個
mysql> select * from mysql.db where host like '192.168.65.%';
+--------------+-----+-------+-------------+-------------+-------------+-------------+-------------+-----------+------------+-----------------+------------+------------+-----------------------+------------------+------------------+----------------+---------------------+--------------------+--------------+------------+--------------+
| 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.65.1 | db1 | test2 | Y | Y | Y | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N |
| 192.168.65.2 | db1 | test2 | 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.65.%'\G
*************************** 1. row ***************************
Host: 192.168.65.1
Db: db1
User: test2
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.65.2
Db: db1
User: test2
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)
mysql> insert into db1.t1 values (1, 'abc');
Query OK, 1 row affected (0.04 sec)
- 插入內容: insert
mysql> select * from db1.t1;
+------+------+
| id | name |
+------+------+
| 1 | abc |
+------+------+
1 row in set (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 |
+------+------+
2 rows in set (0.00 sec)
- 修改內容:update
# 根據id欄位修改name欄位
mysql> update db1.t1 set name='aaa' where id=1;
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from db1.t1;
+------+------+
| id | name |
+------+------+
| 1 | aaa |
| 1 | aaa |
+------+------+
2 rows in set (0.00 sec)
# 根據name欄位修改id欄位
mysql> update db1.t1 set id=8 where name='abc';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from db1.t1;
+------+------+
| id | name |
+------+------+
| 1 | 234 |
| 8 | abc |
+------+------+
2 rows in set (0.00 sec)
- 清空表內資料(表的結果為刪除)
mysql> truncate db1.t1;
Query OK, 0 rows affected (0.01 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.01 sec)
- 刪除表內的某些資料
mysql> delete from db1.t1 where id=2;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from db1.t1 where id=1;
Query OK, 2 rows affected (0.01 sec)
- 刪除操作: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
# 刪除資料庫
mysql> drop database db1;
Query OK, 0 rows affected (0.04 sec)
mysql> use db1;
ERROR 1049 (42000): Unknown database 'db1'
作為運維人員,truncate、drop、delete等刪除命令不能隨意使用,極易造成資料的丟失。
MySQL資料庫的備份與恢復
- 備份庫
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