一、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
這樣既可備份;