mysql用戶管理 經常使用sql語句mysql數據庫備份恢復

13.4 mysql用戶管理
13.5 經常使用sql語句
13.6 mysql數據庫備份恢復
擴展
SQL語句教程 http://www.runoob.com/sql/sql-tutorial.html
什麼是事務?事務的特性有哪些? http://blog.csdn.net/yenange/article/details/7556094
根據binlog恢復指定時間段的數據 https://blog.csdn.net/lilongsy/article/details/74726002
mysql字符集調整 http://xjsunjie.blog.51cto.com/999372/1355013html

使用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
相關視頻 
連接:http://pan.baidu.com/s/1miFpS9M 密碼:86dx 
連接:http://pan.baidu.com/s/1o7GXBBW 密碼:ue2fmysql

mysql用戶管理目錄概要

  • grant all on . to 'user1' identified by 'passwd';
  • grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.180.1' identified by 'passwd';
  • grant all on db1.* to 'user3'@'%' identified by 'passwd';
  • show grants;
  • show grants for user2@192.168.180.1;

mysql用戶管理

  • 場景,爲了安全,新建的站點,建立新的用戶,或者給予使用已有帳戶,給予權限
  • grant all on . to 'user1' identified by 'passwd';
    • grant 表示 受權
    • all 表示全部權限,查看,建立,刪除等等
    • on . to 'user1' identified by 'passwd';
  • 如果登陸到mysql中後,輸錯了字符,並按了回車鍵,直接輸入分號 ; 就會推出, 回到mysql的命令行
  • 退出mysql除了使用 quit 命令,還可使用 exit 命令,還能夠ctrl+d快捷鍵退出
  1. 登陸到mysql 
  2. 建立普通用戶user1,命令:
  • grant all on *.* to 'user1'@'127.0.0.1' identified by '123456a';——>在輸入命令的時候,千萬要注意符號,一旦漏失了符號 ' ',那麼後面就沒法登陸到user1的mysql
    • 'user1'@'127.0.0.1' 指定用戶@指定來源IP (指定用戶能夠寫 % 就是通配,表示全部的IP)若是指定了來源IP,那麼只能經過來源IP登陸
    • 符號*.* 表示全部庫,全部表
      • 第一個 * 表示庫名,能夠寫成mysql.* 那就表示對mysql全部的表
    • identified by 'passwd' 指定user1的mysql密碼
  • grant語句,是不會記錄到命令歷史中的由於不安全
mysql>  grant all on *.* to 'user1'@'127.0.0.1' identified by '123456a';
Query OK, 0 rows affected (0.02 sec)
  • 退出數據庫,並嘗試user1是否能夠登陸
[root@yong-01 ~]# mysql -uuser1 -p'123456a'
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'user1'@'localhost' (using password: YES)
  • 會看到登陸失敗,由於它默認的是sock,須要指定 -h 指定IP,會看到成功登陸到user1的數據庫
[root@yong-01 ~]# mysql -uuser1 -p'123456a' -h127.0.0.1
  1. 受權localhost,受權本地,用sock去鏈接
  2. 從新登陸root,並輸入localhost,建立成功後,並退出
  • grant all on *.* to 'user1'@'localhost' identified by '123456a';
mysql> grant all on *.* to 'user1'@'localhost' identified by '123456a';
Query OK, 0 rows affected (0.00 sec)
  • 這時不加-h 也能夠登陸到user1了,由於如今受權就是針對localhost,localhost就是針對的sock
[root@yong-01 ~]# mysql -uuser1 -p123456a

對具體的權限去受權

  • grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.180.1' identified by 'passwd';
    • 針對SELECT,UPDATE,INSERT,針對 db1這個庫全部的表給用戶user2來源IP,並設定密碼
  • grant all on db1.* to 'user3'@'%' identified by 'passwd';
    • 針對全部的IP去受權
  • show grants; 查看全部的受權
    • 在登陸到某一用戶下,show grants;會查看到當前用戶的權限的
    • 登陸user1用戶的mysql,去查看受權
[root@yong-01 ~]# mysql -uuser1 -p123456a
mysql> show grants;
+-----------------------------------------------------------------------------------------------------------------------+
| Grants for user1@localhost                                                                                            |
+-----------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'user1'@'localhost' IDENTIFIED BY PASSWORD '*B012E8731FF1DF44F3D8B26837708985278C3CED' |
+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
  • show grants for user1@127.0.0.1; 指定用戶去查看受權
    • 登陸root用戶的mysql,而後查看user1用戶的mysql的受權
[root@yong-01 ~]# mysql -uroot -p111111
mysql> show grants for user1@'127.0.0.1';
+-----------------------------------------------------------------------------------------------------------------------+
| Grants for user1@127.0.0.1                                                                                            |
+-----------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'user1'@'127.0.0.1' IDENTIFIED BY PASSWORD '*B012E8731FF1DF44F3D8B26837708985278C3CED' |
+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

show grants;需求

  • show grants;看的是root
  • 建立一個用戶user2,並作一個受權
  • grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.133.1' identified by 'passwd';
mysql> grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.180.1' identified by 'passwd';
Query OK, 0 rows affected (0.00 sec)
  • 查看user2的受權
  • show grants for user2@'192.168.180.1';
mysql> show grants for user2@'192.168.180.1';
+------------------------------------------------------------------------------------------------------------------+
| Grants for user2@192.168.180.1                                                                                   |
+------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'user2'@'192.168.180.1' IDENTIFIED BY PASSWORD '*59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0' |
| GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'192.168.180.1'                                               |
+------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
  1. 有一種狀況會用到它,好比說,給192.168.180.1作了受權了,但發現一個IP不夠,還有一個192.168.180.2,也就是說user2用戶不只須要在192.168.180.1上登陸,還須要在192.168.180.2上登陸,這時候就須要把受權的命令所有在執行一遍
  2. 這時候就能夠直接把GRANT USAGE ON *.* TO 'user2'@'192.168.180.1' IDENTIFIED BY PASSWORD '*59C70DA2F3E3A5BDF46B68FF5C8B8F25762BCCEF0'; 複製一遍,將其中192.168.180.1改成192.168.180.2 並在語句結尾加上分號 ;
mysql>  GRANT USAGE ON *.* TO 'user2'@'192.168.180.2' IDENTIFIED BY PASSWORD '*59C70DA2F3E3A5BDF46B68FF5C8B8F25762BCCEF0';
Query OK, 0 rows affected (0.00 sec)
  • 而後再將第二行復制GRANT SELECT, INSERT, UPDATE ON db1.* TO 'user2'@'192.168.133.1' 把IP改成192.168.133.2,並加上分號 ;
mysql> GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'192.168.180.2';
Query OK, 0 rows affected (0.00 sec)
  • 這時候在來查看show grants查看192.168.133.2
mysql> show grants for user2@'192.168.180.2';
+------------------------------------------------------------------------------------------------------------------+
| Grants for user2@192.168.180.2                                                                                   |
+------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'user2'@'192.168.180.2' IDENTIFIED BY PASSWORD '*59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0' |
| GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'192.168.180.2'                                               |
+------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
  1. show grants;會看到一樣的密碼,一樣的用戶,惟一改變的就是IP
  2. 在知道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;

經常使用sql語句

  • 增刪改查,就是mysql和其餘關係型數據庫經常使用的select語句操做命令

查詢語句

  1. 首先登陸root下的mysql  mysql -uroot -p111111
  2. 使用db1庫  use db1;
  3. 查看當前庫的全部表show tables;
  4. 查看錶的行數 select count(*) from mysql.user;
  • 庫和表中間有個分割符,就是用點 . 分割
mysql> select count(*) from mysql.user;
+----------+
| count(*) |
+----------+
|       10 |
+----------+
1 row in set (0.00 sec)
  • 就是說user表有10行內容
  • 查看全部的內容 select * from mysql.db;(這樣看起來會很亂) ——>能夠在後面加上\G,如select * from mysql.db\G;
  • 這裏的 * 表示查看全部內容
  • 查看db庫的全部內容 select db from mysql.db; 第一個db是字段
mysql> select db from mysql.db;
+---------+
| db      |
+---------+
| test    |
| test\_% |
| db1     |
| db1     |
+---------+
4 rows in set (0.01 sec)

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

mysql>
  • 模糊查詢 select * from mysql.db where host like '192.168.%';  like 就是模糊匹配

插入語句

  • 查看建立的表
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)
  • 查看db1.t1表的內容,會發現爲空 select * from db1.t1;
  • 插入數據到 insert into db1.t1 values (1, 'abc');
  • 插入1, 'abc'到db1.t1表
  • 再來查詢db1.t1
mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|    1 | abc  |
+------+------+
1 row in set (0.00 sec)
  • 這樣就成功了插入了一條數據,在插入的時候 name 這個字段應該是是一個字符串,字符串須要加上一個單引號 ' ' ,數字能夠不加單引號
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)
  • 這裏沒有作限制,這裏id和name均可以是相同的,同一個字段裏有相同的數字,相同的值 ,也能夠作一些限制,在插入相同的id的時候,就會衝突

update操做

  • 更改db1.t1表 的字符串爲name 的數據 和 字符串爲id 的數據
  • update db1.t1 set name='aaa' where id=1;
mysql> update db1.t1 set name='aaa' where id=1;
Query OK, 2 rows affected (0.01 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)

delete操做

  • 刪除db1.t1表 的數據 和 字符串爲id 的數據
  • delete from db1.t1 where id=1;
mysql> delete from db1.t1 where id=1;
Query OK, 2 rows affected (0.01 sec)

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

truncate清空一個表

  • 清空表數據 truncate table db1.t1;
    • 即便表的數據清空了,但表的字段依舊存在的
mysql> truncate table db1.t1;
Query OK, 0 rows affected (0.02 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 會清空表的數據並清除表的框架
  • drop 會把表的框架也丟掉 drop table db1.t1;
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;

總結

  • 在使用mysql的時候,少用 * 這樣的操做,由於如果一個表裏面的內容不少,select count(*)這樣操做就會很耗時,浪費資源
  • 數據庫中經常使用引擎是myisam和innodb,默認mysql庫裏面都是使用的myisam引擎
    • 特色:myisam引擎,能自動去統計有多少行
      • 在select count(*)查看錶的時候會很快
      • use mysql;
      • show create table user\G;
    • 特色:innodb引擎,不會自動統計行數,每次去查詢,每次去統計行數,就會很耗時
      • use db1
      • show create table t1;
    • 因此select count(*)這種操做盡可能減小,會耗費太多資源

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

mysql數據庫備份恢復

備份庫

  1. 在執行mysqldump -uroot -p111111 mysql的時候會看到不少信息,屏幕上顯示的這些就是備份的數據
  2. 備份mysql庫文件 mysqlbak.sql文件就是mysql的備份庫文件
[root@yong-01 ~]# mysqldump -uroot -p111111 mysql > /tmp/mysqlbak.sql
Warning: Using a password on the command line interface can be insecure.
  1. 咱們能夠經過mysqlbak.sql來恢復數據庫,還能夠恢復到另一個數據庫裏面去
  2. 建立一個新的庫mysql2
[root@yong-01 ~]# mysql -uroot -p111111 -e "create database mysql2"
Warning: Using a password on the command line interface can be insecure.
  • 恢復庫 mysql -uroot -p111111 mysql < /tmp/mysqlbak.sql 
[root@yong-01 ~]# mysql -uroot -p111111 mysql < /tmp/mysqlbak.sql 
Warning: Using a password on the command line interface can be insecure.
  • 進入到數據庫裏面,在後面加一個mysql2 就會進入到mysql2數據庫裏面
  • mysql -uroot -p111111 mysql2
[root@yong-01 ~]# mysql -uroot -p111111 mysql2
  • 查看數據庫
mysql> select database();
+------------+
| database() |
+------------+
| mysql2     |
+------------+
1 row in set (0.00 sec)

備份表

  • 針對庫裏面的某一個表去作備份,只須要在 庫後面 加上 表名字 便可備份
    • 先庫 在表,中間是空格
    • 備份表 mysqldump -uroot -p111111 mysql user > /tmp/user.sql
  • 能看到備份的時候,庫存在的話,先把庫drop掉,而後建立庫,表存在的話,先把表drop掉,而後建立表,而後在一步一步的插入每一行數據
[root@yong-01 ~]# mysqldump -uroot -p111111 mysql user >/tmp/user.sql
Warning: Using a password on the command line interface can be insecure.
  • 恢復表的時候,只須要寫庫的名字,不須要去寫表的名字
    • 恢復表 mysql -uroot -p111111 mysql < /tmp/user.sql
  • 恢復mysql2庫裏面的表
[root@yong-01 ~]# mysql -uroot -p111111 mysql2 < /tmp/user.sql 
Warning: Using a password on the command line interface can be insecure.

備份全部的庫

  • 備份全部庫 mysqldump -uroot -p111111 -A >/tmp/mysql_all.sql
    • -A 表示all全部的意思
[root@yong-01 ~]# mysqldump -uroot -p111111 -A >/tmp/mysql_all.sql
Warning: Using a password on the command line interface can be insecure.
  • 只備份表結構 mysqldump -uroot -p111111 -d mysql > /tmp/mysql.sql
    • 不須要表的數據,只須要表的語句
  • 備份mysql2的表結構
[root@yong-01 ~]#  mysqldump -uroot -p111111 -d mysql > /tmp/mysql.sql
Warning: Using a password on the command line interface can be insecure.

示例

  • 兩個機器的庫備份,一個庫備份到另外一臺機器上
  • 解決:
    • 首先兩臺機器可以通訊
    • 而後mysqldump -h 遠程mysql-ip -uuser-ppassword dbname > /本地backup.sql
    • 這樣便可備份
相關文章
相關標籤/搜索