最近在學習mysql資料庫,總結一下:
簡單語句
- 1:為root設定密碼
- mysqladmin -u root password "123.abc"
- 2:現實資料庫:
- show databases;
- 3:use 切換資料庫
- 4:show tables; 顯示表
- 5:/usr/local/mysql/var/ 裡面是資料庫 一個資料庫對應一個檔案
- 夾,一個資料表對應三個檔案 分别是:.frm .MYD .MYI
- 6:顯示資料表結構
- describe 資料庫名.表明;
- 7:建立資料庫
- create database 資料庫名;
- 8:建立資料表
- create table 表名(字段定義。。。。);
- 9:删除資料表
- drop table 資料庫名.表名;
- 10:删除資料庫
- drop database 資料庫名;
- 11:插入資料
- insert into 表名(字段1,字段2。。) values(字段1的值,字
- 段2的值....)
- 12:查詢資料記錄
- select 字段1,字段2.... from 表名 where 條件表達式
- 13:修改資料
- update 表名 set 字段名1=字段值1,字段名2=字段值2 where 條
- 件表達式
- 14:删除資料
- delete from 表名 where 條件表達式
備份mysql(需要以.sql結尾):
- 1:備份一個資料庫:
- mysqldump -u 使用者名 -p 資料庫名 > mysql_bak.sql
- 2:備份資料表:
- mysqldump -u 使用者名 -p 庫名 表名 > mysql_tables.sql
- 3:備份所有的資料庫:
- mysqldump -u 使用者名 -p --all-databases > mysql-all.sql
恢複mysql:
- 1:恢複所有資料庫:
- mysql -u root -p 庫名 < /備份路徑/備份檔案名
- 2:恢複單個資料庫:
- mysql -u root -p 庫名 < /路徑/檔案名
- 3:恢複資料庫的一個表:
- mysql -u root -p mysql < mysql.host-user.sql
- //用到的備份檔案不一樣
授權使用者:
- grant 權限清單 on 資料庫名.表名 to 使用者名@來源位址 indentified by '密碼'
- //權限表 all是所有 select,insert,update,delete
- * 可以代表所有的表
- 使用者名不能比對 但是'' 比對任何使用者, 來源用%比對某個域的所有
- 域名(%。test.com)也可以 192.168.1.0/24
- 最後一段設密碼
- show grants for 使用者名@域名/ip;
show engines 檢視目前資料庫支援的存儲引擎(mysql裡面執行)
- revoke 權限清單 on 資料庫名.表名 from 使用者名@域名/ip;