2018-3-23 13周5次課 MySQL常用操作(下)

來源:互聯網
上載者:User

標籤:MySQL

13.4 mysql使用者管理


·建立使用者:grant all on *.* to 'user1' identified by 'passwd';

或指定來源ip:grant all on *.* to 'user1'@'ip' identified by 'passwd';


mysql> grant all on *.* to 'user1' identified by '123456';Query OK, 0 rows affected (0.00 sec)mysql> grant all on *.* to 'user2'@'127.0.0.1' identified by '123456';Query OK, 0 rows affected (0.00 sec)


·針對mysql資料庫所有的表有許可權

grant all on mysql.* to 'user1' identified by 'passwd';


·針對所有的ip來授權

grant all on *.* to 'user2'@'%' identified by '123456';


·針對 socket 授權:

mysql> grant all on *.* to 'user1'@'localhost' identified by '123456';Query OK, 0 rows affected (0.00 sec)


·查看登入使用者授權:show grants


·查看指定使用者授權:show grants for 使用者名稱@ip;


·針對部分許可權授權:

grant SELECT,UPDATE,INSERT on db1.* to 'user3'@'192.168.133.1' identified by 'passwd';


·如果想給同一個使用者在不通的ip上授權:

把grants裡的命令複製執行以便,更改ip


13.5 常用sql語句


·查看某個資料庫的某個表有多少行

select count(*) from mysql.user;


·查看某個表的說有內容:

select * from mysql.db\G;

(select count(*) 和 select * 這種操作盡量避免,資料太大的表,比較耗費時間)


·顯示欄位:

select db from mysql.db;

什麼是欄位:這些就是欄位


·顯示多個欄位:

select db,user from mysql.db;


·模糊查詢:

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


·往表裡插入資料:

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



·更改表裡的資料資訊:

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


·根據條件刪除:

delete from db1.t1 where id=3;


·清空表的所有資料(不用逐條刪除):

truncate table db1.t1;

(truncate 和 drop、delete的區別,truncate只清楚表裡的資料,不破壞表的結構)


·徹底刪除表:

drop table db1.t1;


mysql> drop table db1.t1;Query OK, 0 rows affected (0.01 sec)


·徹底刪除資料庫:

drop database db1;


mysql> drop database db1;Query OK, 1 row affected (0.01 sec)



13.6 mysqlDatabase Backup恢複


·備份庫:

mysqldump -u使用者名稱 -p密碼 資料庫 > 備份資料檔案路徑

[[email protected] ~]# mysqldump -uroot -p123456 mysql > /tmp/mysql.sql

(資料太多不列出)


·恢複庫:

mysql -u使用者名稱 -p密碼 資料庫 < 備份資料檔案路徑

[[email protected] ~]# mysql -uroot -p123456 mysql2 < /tmp/mysql.sq


·備份表:

mysqldump -u使用者名稱 -p密碼 資料庫 表名 > 備份資料路徑

[[email protected] ~]# mysqldump -uroot -p123456 mysql user > /tmp/user.sql


·恢複表:

mysqldump -u使用者名稱 -p密碼 資料庫 < 備份資料路徑

[[email protected] ~]# mysqldump -uroot -p123456 mysql < /tmp/user.sql

(恢複表資料,只要寫庫名即可)


·備份所有庫:

mysqldump -u使用者名稱 -p密碼 -A > 備份資料路徑

[[email protected] ~]# mysqldump -uroot -p123456 -A > /tmp/mysql_all.sql


·只備份表結構:

mysqldump -u使用者名稱 -p密碼 -d 資料庫 > 備份資料路徑

[[email protected] ~]# mysqldump -uroot -p123456 -d mysql2 > /tmp/mysql2.sql








2018-3-23 13周5次課 MySQL常用操作(下)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.