天天看點

mysql使用者管理、常用sql語句、mysql資料庫備份恢複

mysql使用者管理

1、新增使用者user1,并設定密碼為123456

mysql> grant all on *.* to 'user1'@'127.0.0.1' identified by '123456';

#建立user1使用者并授予其所有權限“*.*”(通配符)

#第一個*:表示所有的資料庫

#第二個*:表示所有的表

#127.0.0.1表示來源IP,指的隻有這個IP可以連接配接;'%':代表所有的ip

#identified by 設定密碼

2、對user1使用者進行授權管理

mysql> grant SELECT,UPDATE,INSERT on test.* to 'user2'@'192.168.3.74' identified by '123456';

建立user2使用者,并指定test的資料庫,權限:select、update、insert

3、建立user3,對所有的IP,所有的權限

mysql> grant all on test.* to 'user3'@'%' identified by '123456';

4、查詢使用者權限

mysql> show grants for user2@'192.168.3.74';

+-----------------------------------------------------------------------------------------------------------------+

| Grants for [email protected]                                                                                   |

| GRANT USAGE ON *.* TO 'user2'@'192.168.3.74' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |  #這一行僅僅建立使用者

| GRANT SELECT, INSERT, UPDATE ON `test`.* TO 'user2'@'192.168.3.74'  #這一行是授權user2使用者的select、insert、update                                             |

5、更改權限

mysql>  GRANT SELECT, INSERT, UPDATE ON `test`.* TO 'user2'@'192.168.3.74';

常用sql語句

增删改查:

1、檢視指定的資料庫表行數

select count(*) from mysql.user;

2、查詢 192.168.%的資訊

mysql> select * from mysql.db where host like '192.168.%'\G;

3、create table t1(`id` int(4),`name` char(40));

4、insert into db1.t1 values(1,'abc');

5、清空表

truncate db1.t1;

6、删除使用者

delete from user where User='user1' and Host='127.0.0.1';

7、删除資料

delete from db1.t1 where id=1;

8、更改資料

update db1.t1 set name='aaa' where id=1;

mysql資料庫備份恢複

備份指定庫:

[root@centos7 ~]# mysqldump -uroot -p123456 test > /tmp/testbak.sql

備份所有庫:

[root@centos7 ~]# mysqldump -uroot -p123456 -A > /tmp/mysql_all.sql

恢複指定的庫:

[root@centos7 ~]# mysql -uroot -p123456 mysql < /tmp/mysqlbak.sql

恢複全部庫:

[root@centos7 ~]# mysql -uroot -p123456  < /tmp/testbak.sql

備份表:

[root@test ~]# mysql -uroot -p123456 mysql user > /tmp/user.sql

隻備份表結構:

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

恢複表:

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

本文轉自方向對了,就不怕路遠了!51CTO部落格,原文連結:http://blog.51cto.com/jacksoner/1982777 ,如需轉載請自行聯系原作者