登入資料庫:/mysql/bin/mysql -u root -p
-u:使用者名
-p:密碼
-h:資料庫連接配接IP或域名,預設localhost的可省
-P:連接配接端口,預設3306的可省略
DatabaseName:資料庫名稱,帶此參數時,登入成功後直接轉到該資料庫下;
輸入登入使用者的密碼(輸入時不顯示出來,輸完回車即可):Enter password:
例:以root使用者,在域名為localhost,端口為3306上登入并直接進入到mysql資料庫
mysql -h localhost -P 3306 -u root -p mysql
查詢時間:select now();
查詢目前使用者:select user();
查詢資料庫版本:select version();
查詢目前使用的資料庫:select database();
查詢目前日期:select current_date();
查詢從庫同步狀态:show slave status\G;
查詢資料庫程序情況:show processlist;
列出資料庫:mysql> show databases;
選擇資料庫:use DatabaseName;
例:mysql> use mysql; 使用mysql資料庫
列出表格:mysql> show tables;
顯示表格列的屬性:show columns from tableName;
例:mysql> show columns from user; 顯示user表格列的屬性
建立資料庫:create database DatabaseName;
例:mysql> create database mytest; 使用mytest資料庫
删除資料庫:drop database if exists DatabaseName;
例:mysql> drop database if exists mytest; 删除mytest資料庫
建立資料表:create table TableName(字段設定清單);
例:mysql> create table user(
`id` tinyint(8) not null auto_increment primary key,
`name` varchar(32) not null default '',
`password` varchar(32) not null default '',
`sex` enum('0', '1') not null default '0')ENGINE=MyISAM;
lock tables user write;
insert user(`name`, `password`, `sex`) values ('testNmae', '1234', '0'),('testNmae', '12345', '1'),
('testNmae', '123456', '0'),('testNmae', '12345678', '1');
unlock tables;
建立了一個表名為user表,含有id,name,password,sex字段;
删除資料表:drop table if exists TableName;
例:mysql> drop table if exists user; 删除user資料表
清空資料表:truncate table TableName;
例:mysql> truncate table user; 清空user資料表
優化資料表:optimize table TableName;
例:mysql> optimize table user; 優化user資料表
修複資料表:repair table TableName;
例:mysql> repair table user; 修複user資料表
分析資料表:analyze table TableName;
例:mysql> analyze table user; 分析user資料表
檢查資料表:check table TableName;
例:mysql> check table user; 檢查user資料表
資料表添加列(字段):alter table TableName add (字段設定清單) [ after ColumnName ];
例:alter table user add `e_mail` varchar(255) not null default '' after id 在user資料表裡插入一個新的字段e_mail位于字段id之後
資料表更改列(字段):alter table TableName change 舊列名 (新字段設定清單) [ after ColumnName ];
例:alter table user change `e_mail` `eMail` varchar(255) not null default '' after id; 在user資料表裡将字段為e_mail修改為eMail并更新位置
資料表删除列(字段):alter tables TableName drop column ColumnName, ColumnName2;
例:alter table user drop column `eMail`; 在user資料表裡将字段為eMail的列删除
導出一個完整資料庫:
登入資料庫: /mysql/bin/mysqldump -u root -p DatabaseName > OutFileName
-P:連接配接端口,預設3306的可省
DatabaseName:要導出的資料庫名稱;
>:輸出到指定檔案
OutFileName:存放導出資料庫的檔案
導出一個資料庫中的指定表(一個或多個):
/mysql/bin/mysqldump -u root -p DatabaseName DataTableName1 DataTableName2 > OutFileName
DataTableName1:要導出的一個表名
DataTableName2:要導出的另一個表名
...有多個表時,以空格形式書寫即可導出多個
grant 權限1,權限2,…權限n on 資料庫名稱.表名稱 to 使用者名@使用者位址 identified by ‘連接配接密碼’;
控制到庫級的指令示範:
grant all privileges on mytest.* to shangcheng@'localhost' identified by '123456';
使用者名為:shangcheng
庫名:shangcheng
密碼:123456
重新整理系統權限表:mysql> flush privileges;
修改使用者密碼:set password for 'user'@'host' = PASSWORD('newpassword');
例:set password for 'root'@'localhost' = PASSWORD('123456'); 将登入方式為localhost的root使用者修改密碼為123456