mysql 常用命令
時(shí)間:2023-07-04 19:48:01 | 來(lái)源:網(wǎng)站運(yùn)營(yíng)
時(shí)間:2023-07-04 19:48:01 來(lái)源:網(wǎng)站運(yùn)營(yíng)
mysql 常用命令:一、連接MySQL
格式: mysql -h主機(jī)地址 -u用戶名 -p用戶密碼
1、例1:連接到本機(jī)上的MYSQL。
首先在打開(kāi)DOS窗口,然后進(jìn)入目錄 mysqlbin,再鍵入命令mysql -uroot -p,回車后提示你輸密碼,如果剛安裝好MYSQL,超級(jí)用戶root是沒(méi)有密碼的,故直接回車即可進(jìn)入到MYSQL中了,MYSQL的提示符是: mysql>。
2、例2:連接到遠(yuǎn)程主機(jī)上的MYSQL。假設(shè)遠(yuǎn)程主機(jī)的IP為:192.168.1.12,用戶名為root,密碼為abcd123。則鍵入以下命令:
mysql -h 192.168.1.12 -uroot -pabcd123
(注:u與root可以不用加空格,其它也一樣)
3、退出MYSQL命令: exit (回車)。
二、修改密碼
格式:mysqladmin -u用戶名 -p舊密碼 password 新密碼
1、例1:給root加個(gè)密碼ab12。首先在DOS下進(jìn)入目錄mysqlbin,然后鍵入以下命令:
mysqladmin -uroot -password ab12
注:因?yàn)殚_(kāi)始時(shí)root沒(méi)有密碼,所以-p舊密碼一項(xiàng)就可以省略了。
2、例2:再將root的密碼改為djg345。
mysqladmin -uroot -pab12 password djg345
三、增加新用戶。(注意:和上面不同,下面的因?yàn)槭荕ySQL環(huán)境中的命令,所以后面都帶一個(gè)分號(hào)作為命令結(jié)束符)
格式:grant select on 數(shù)據(jù)庫(kù).* to 用戶名@登錄主機(jī) identified by /"密碼/"
例1、增加一個(gè)用戶test1密碼為abc,讓他可以在任何主機(jī)上登錄,并對(duì)所有數(shù)據(jù)庫(kù)有查詢、插入、修改、刪除的權(quán)限。首先用以root用戶連入MySQL,然后鍵入以下命令:
grant select,insert,update,
delete on *.* to test2@localhost identified by /"abc/";
如果你不想test2有密碼,可以再打一個(gè)命令將密碼消掉。
grant select,insert,update,delete on mydb
.* to test2@localhost identified by /"/";
在上面講了登錄、增加用戶、密碼更改等問(wèn)題。下面我們來(lái)看看MySQL中有關(guān)數(shù)據(jù)庫(kù)方面的操作。注意:你必須首先登錄到MySQL中,以下操作都是在MySQL的提示符下進(jìn)行的,而且每個(gè)命令以分號(hào)結(jié)束。
1、MySQL常用命令
create database name; 創(chuàng)建數(shù)據(jù)庫(kù)
use databasename; 選擇數(shù)據(jù)庫(kù)
drop database name 直接刪除數(shù)據(jù)庫(kù),不提醒
show tables; 顯示表
describe tablename; 表的詳細(xì)描述
select 中加上distinct去除重復(fù)字段
mysqladmin drop database name 刪除數(shù)據(jù)庫(kù)前,有提示。
顯示當(dāng)前mysql版本和當(dāng)前日期
select version(),current_date;
2、修改mysql中root的密碼:
shell>mysql -u root -p
mysql> update user set password=password(”xueok654123″) where user=’root’;
mysql> flush privileges //刷新數(shù)據(jù)庫(kù)
mysql>use dbname; 打開(kāi)數(shù)據(jù)庫(kù):
mysql>show databases; 顯示所有數(shù)據(jù)庫(kù)
mysql>show tables; 顯示數(shù)據(jù)庫(kù)mysql中所有的表:先use mysql;然后
mysql>describe user; 顯示表mysql數(shù)據(jù)庫(kù)中user表的列信息);
3、grant
創(chuàng)建一個(gè)可以從任何地方連接服務(wù)器的一個(gè)完全的超級(jí)用戶,但是必須使用一個(gè)口令something做這個(gè)
mysql> grant all privileges on *.* to user@localhost identified by ’something’ with
增加新用戶
格式:grant select on 數(shù)據(jù)庫(kù).* to 用戶名@登錄主機(jī) identified by “密碼”
GRANT ALL PRIVILEGES ON *.* TO monty@localhost IDENTIFIED BY ’something’ WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO monty@”%” IDENTIFIED BY ’something’ WITH GRANT OPTION;
刪除授權(quán):
mysql> revoke all privileges on *.* from root@”%”;
mysql> delete from user where user=”root” and host=”%”;
mysql> flush privileges;
創(chuàng)建一個(gè)用戶custom在特定客戶端
http://it363.com登錄,可訪問(wèn)特定數(shù)據(jù)庫(kù)fangchandb
mysql >grant select, insert, update, delete, create,drop on fangchandb.* to custom@ 滄州網(wǎng)站建設(shè),滄州網(wǎng)絡(luò)公司,滄州網(wǎng)站制作,滄州網(wǎng)絡(luò)推廣,滄州做網(wǎng)站公司,滄州手機(jī)網(wǎng)站-滄州佳恩網(wǎng)絡(luò)科技有限公司 identified by ‘ passwd’
重命名表:
mysql > alter table t1 rename t2;
4、mysqldump
備份數(shù)據(jù)庫(kù)
shell> mysqldump -h host -u root -p dbname >dbname_backup.sql
恢復(fù)數(shù)據(jù)庫(kù)
shell> mysqladmin -h myhost -u root -p create dbname
shell> mysqldump -h host -u root -p dbname < dbname_backup.sql
如果只想卸出建表指令,則命令如下:
shell> mysqladmin -u root -p -d databasename > a.sql
如果只想卸出插入數(shù)據(jù)的sql命令,而不需要建表命令,則命令如下:
shell> mysqladmin -u root -p -t databasename > a.sql
那么如果我只想要數(shù)據(jù),而不想要什么sql命令時(shí),應(yīng)該如何操作呢?
mysqldump -T./ phptest driver
其中,只有指定了-T參數(shù)才可以卸出純文本文件,表示卸出數(shù)據(jù)的目錄,./表示當(dāng)前目錄,即與mysqldump同一目錄。如果不指定driver 表,則將卸出整個(gè)數(shù)據(jù)庫(kù)的數(shù)據(jù)。每個(gè)表會(huì)生成兩個(gè)文件,一個(gè)為.sql文件,包含建表執(zhí)行。另一個(gè)為.txt文件,只包含數(shù)據(jù),且沒(méi)有sql指令。
5、可將查詢存儲(chǔ)在一個(gè)文件中并告訴mysql從文件中讀取查詢而不是等待鍵盤輸入??衫猛鈿こ绦蜴I入重定向?qū)嵱贸绦騺?lái)完成這項(xiàng)工作。例如,如果在文件my_file.sql 中存放有查
詢,可如下執(zhí)行這些查詢:
例如,如果您想將建表語(yǔ)句提前寫在sql.txt中:
mysql > mysql -h myhost -u root -p database < sql.txt
1、安裝環(huán)境:
Windows XP
Mysql 4.0.17 從 下次就需要用mysql -uroot -proot才可以登陸
在遠(yuǎn)程或本機(jī)可以使用 mysql -h 172.5.1.183 -uroot 登陸,這個(gè)根據(jù)第二行的策略確定
權(quán)限修改生效:
1)net stop mysql
net start mysql
2)c:/mysql/bin/mysqladmin flush-privileges
3)登陸mysql后,用flush privileges語(yǔ)句
6、創(chuàng)建數(shù)據(jù)庫(kù)staffer
create database staffer;
7、下面的語(yǔ)句在mysql環(huán)境在執(zhí)行
顯示用戶擁有權(quán)限的數(shù)據(jù)庫(kù) show databases;
切換到staffer數(shù)據(jù)庫(kù) use staffer;
顯示當(dāng)前數(shù)據(jù)庫(kù)中有權(quán)限的表 show tables;
顯示表staffer的結(jié)構(gòu) desc staffer;
8、創(chuàng)建測(cè)試環(huán)境
1)創(chuàng)建數(shù)據(jù)庫(kù)staffer
mysql> create database staffer
2)創(chuàng)建表staffer,department,position,depart_pos
create table s_position
(
id int not null auto_increment,
name varchar(20) not null default '經(jīng)理', #設(shè)定默認(rèn)值
description varchar(100),
primary key PK_positon (id) #設(shè)定主鍵
);
create table department
(
id int not null auto_increment,
name varchar(20) not null default '系統(tǒng)部', #設(shè)定默認(rèn)值
description varchar(100),
primary key PK_department (id) #設(shè)定主鍵
);
create table depart_pos
(
department_id int not null,
position_id int not null,
primary key PK_depart_pos (department_id,position_id) #設(shè)定復(fù)和主鍵
);
create table staffer
(
id int not null auto_increment primary key, #設(shè)定主鍵
name varchar(20) not null default '無(wú)名氏', #設(shè)定默認(rèn)值
department_id int not null,
position_id int not null,
unique (department_id,position_id) #設(shè)定唯一值
);
3)刪除
mysql>
drop table depart_pos;
drop table department;
drop table s_position;
drop table staffer;
drop database staffer;
9、修改結(jié)構(gòu)
mysql>
#表position增加列test
alter table position add(test char(10));
#表position修改列test
alter table position modify test char(20) not null;
#表position修改列test默認(rèn)值
alter table position alter test set default 'system';
#表position去掉test默認(rèn)值
alter table position alter test drop default;
#表position去掉列test
alter table position drop column test;
#表depart_pos刪除主鍵
alter table depart_pos drop primary key;
#表depart_pos增加主鍵
alter table depart_pos add primary key PK_depart_pos (department_id,position_id);
10、操作數(shù)據(jù)
#插入表department
insert into department(name,description) values('系統(tǒng)部','系統(tǒng)部');
insert into department(name,description) values('公關(guān)部','公關(guān)部');
insert into department(name,description) values('客服部','客服部');
insert into department(name,description) values('財(cái)務(wù)部','財(cái)務(wù)部');
insert into department(name,description) values('測(cè)試部','測(cè)試部');
#插入表s_position
insert into s_position(name,description) values('總監(jiān)','總監(jiān)');
insert into s_position(name,description) values('經(jīng)理','經(jīng)理');
insert into s_position(name,description) values('普通員工','普通員工');
#插入表depart_pos
insert into depart_pos(department_id,position_id)
select a.id department_id,b.id postion_id
from department a,s_position b;
#插入表staffer
insert into staffer(name,department_id,position_id) values('陳達(dá)治',1,1);
insert into staffer(name,department_id,position_id) values('李文賓',1,2);
insert into staffer(name,department_id,position_id) values('馬佳',1,3);
insert into staffer(name,department_id,position_id) values('亢志強(qiáng)',5,1);
insert into staffer(name,department_id,position_id) values('楊玉茹',4,1);
11、查詢及刪除操作
#顯示系統(tǒng)部的人員和職位
select a.name,b.name department_name,c.name position_name
from staffer a,department b,s_position c
where a.department_id=b.id and a.position_id=c.id and b.name='系統(tǒng)部';
#顯示系統(tǒng)部的人數(shù)
select count(*) from staffer a,department b
where a.department_id=b.id and b.name='系統(tǒng)部'
#顯示各部門的人數(shù)
select count(*) cou,b.name
from staffer a,department b
where a.department_id=b.id
group by b.name;
#刪除客服部
delete from department where name='客服部';
#將財(cái)務(wù)部修改為財(cái)務(wù)一部
update department set name='財(cái)務(wù)一部' where name='財(cái)務(wù)部';
12、備份和恢復(fù)
備份數(shù)據(jù)庫(kù)staffer
c:/mysql/bin/mysqldump -uroot -proot staffer>e:/staffer.sql
得到的staffer.sql是一個(gè)sql腳本,不包括建庫(kù)的語(yǔ)句,所以你需要手工
創(chuàng)建數(shù)據(jù)庫(kù)才可以導(dǎo)入
恢復(fù)數(shù)據(jù)庫(kù)staffer,需要?jiǎng)?chuàng)建一個(gè)空庫(kù)staffer
c:/mysql/bin/mysql -uroot -proot staffer<staffer.sql
如果不希望后來(lái)手工創(chuàng)建staffer,可以
c:/mysql/bin/mysqldump -uroot -proot --databases staffer>e:/staffer.sql
mysql -uroot -proot >e:/staffer.sql
但這樣的話系統(tǒng)種就不能存在staffer庫(kù),且無(wú)法導(dǎo)入其他名字的數(shù)據(jù)庫(kù),
當(dāng)然你可以手工修改staffer.sql文件
13、從文本向數(shù)據(jù)庫(kù)導(dǎo)入數(shù)據(jù)
1)使用工具c:/mysql/bin/mysqlimport
這個(gè)工具的作用是將文件導(dǎo)入到和去掉文件擴(kuò)展名名字相同的表里,如
staffer.txt,staffer都是導(dǎo)入到staffer表中
常用選項(xiàng)及功能如下
-d or --delete 新數(shù)據(jù)導(dǎo)入數(shù)據(jù)表中之前刪除數(shù)據(jù)數(shù)據(jù)表中的所有信息
-f or --force 不管是否遇到錯(cuò)誤,mysqlimport將強(qiáng)制繼續(xù)插入數(shù)據(jù)
-i or --ignore mysqlimport跳過(guò)或者忽略那些有相同唯一
關(guān)鍵字的行, 導(dǎo)入文件中的數(shù)據(jù)將被忽略。
-l or -lock-tables 數(shù)據(jù)被插入之前鎖住表,這樣就防止了,
你在更新數(shù)據(jù)庫(kù)時(shí),用戶的查詢和更新受到影響。
-r or -replace 這個(gè)選項(xiàng)與-i選項(xiàng)的作用相反;此選項(xiàng)將替代
表中有相同唯一關(guān)鍵字的記錄。
--fields-enclosed- by= char
指定文本文件中數(shù)據(jù)的記錄時(shí)以什么括起的, 很多情況下
數(shù)據(jù)以雙引號(hào)括起。 默認(rèn)的情況下數(shù)據(jù)是沒(méi)有被字符括起的。
--fields-terminated- by=char
指定各個(gè)數(shù)據(jù)的值之間的分隔符,在句號(hào)分隔的文件中,
分隔符是句號(hào)。您可以用此選項(xiàng)指定數(shù)據(jù)之間的分隔符。
默認(rèn)的分隔符是跳格符(Tab)
--lines-terminated- by=str
此選項(xiàng)指定文本文件中行與行之間數(shù)據(jù)的分隔字符串
或者字符。 默認(rèn)的情況下mysqlimport以newline為行分隔符。
您可以選擇用一個(gè)字符串來(lái)替代一個(gè)單個(gè)的字符:
一個(gè)新行或者一個(gè)回車。
mysqlimport命令常用的選項(xiàng)還有-v 顯示版本(version),
-p 提示輸入密碼(password)等。
這個(gè)工具有個(gè)問(wèn)題,無(wú)法忽略某些列,這樣對(duì)我們的數(shù)據(jù)導(dǎo)入有很大的麻煩,雖然
可以手工設(shè)置這個(gè)字段,但會(huì)出現(xiàn)莫名其妙的結(jié)果,我們做一個(gè)簡(jiǎn)單的示例
我們定義如下的depart_no.txt,保存在e盤,間隔為制表符/t
10 10
11 11
12 24
執(zhí)行如下命令
c:/mysql/bin/mysqlimport -uroot -proot staffer e:/depart_pos.txt
在這里沒(méi)有使用列的包圍符號(hào),分割采用默認(rèn)的/t,因?yàn)椴捎脛e的符號(hào)會(huì)有問(wèn)題,
不知道是不是windows的原因
2)Load Data INFILE file_name into table_name(column1_name,column2_name)
這個(gè)命令在mysql>提示符下使用,優(yōu)點(diǎn)是可以指定列導(dǎo)入,示例如下
c:/mysql/bin/mysql -uroot -proot staffer
mysql>load data infile "e:/depart_no.txt" into depart_no(department_id,position_id);
這兩個(gè)工具在Windows下使用都有問(wèn)題,不知道是Windows的原因還是中文的問(wèn)題,
而且不指定的列它產(chǎn)生了空值,這顯然不是我們想要的,所以謹(jǐn)慎使用這些工具
進(jìn)入MySQL:mysql -uuser -ppassword --port=3307
1:使用SHOW語(yǔ)句找出在服務(wù)器上當(dāng)前存在什么數(shù)據(jù)庫(kù):
mysql> SHOW DATABASES;
2:2、創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)MYSQLDATA
mysql> Create DATABASE MYSQLDATA;
3:選擇你所創(chuàng)建的數(shù)據(jù)庫(kù)
mysql> USE MYSQLDATA; (按回車鍵出現(xiàn)Database changed 時(shí)說(shuō)明操作成功!)
4:查看現(xiàn)在的數(shù)據(jù)庫(kù)中存在什么表
mysql> SHOW TABLES;
5:創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)表
mysql> Create TABLE MYTABLE (name VARCHAR(20), sex CHAR(1));
6:顯示表的結(jié)構(gòu):
mysql> DESCRIBE MYTABLE;
7:往表中加入記錄
mysql> insert into MYTABLE values ("hyq","M");
8:用文本方式將數(shù)據(jù)裝入數(shù)據(jù)庫(kù)表中(例如D:/mysql.txt)
mysql> LOAD DATA LOCAL INFILE "D:/mysql.txt" INTO TABLE MYTABLE;
9:導(dǎo)入.sql文件命令(例如D:/mysql.sql)
mysql>use database;
mysql>source d:/mysql.sql;
10:刪除表
mysql>drop TABLE MYTABLE;
11:清空表
mysql>delete from MYTABLE;
12:更新表中數(shù)據(jù)
mysql>update MYTABLE set sex="f" where name='hyq';
UPDATE [LOW_PRIORITY] [IGNORE] tbl_name
SET col_name1=expr1 [, col_name2=expr2 ...]
[WHERE where_definition]
[ORDER BY ...]
[LIMIT rows]
or
UPDATE [LOW_PRIORITY] [IGNORE] tbl_name [, tbl_name ...]
SET col_name1=expr1 [, col_name2=expr2 ...]
[WHERE where_definition]
UPDATE 以新的值更新現(xiàn)存表中行的列。SET 子句指出要修改哪個(gè)列和他們應(yīng)該給定的值。WHERE
子句如果被給出,指定哪個(gè)記錄行應(yīng)該被更新。否則,所有的記錄行被更新。如果 ORDER BY 子句被指定,記錄行將被以指定的次序更新。
如果你指定關(guān)鍵詞 LOW_PRIORITY,UPDATE 的執(zhí)行將被延遲,直到?jīng)]有其它的客戶端正在讀取表。
如果你指定關(guān)鍵詞 IGNORE,該更新語(yǔ)句將不會(huì)異常中止,即使在更新過(guò)程中出現(xiàn)重復(fù)鍵錯(cuò)誤。導(dǎo)致沖突的記錄行將不會(huì)被更新。
如果在一個(gè)表達(dá)式中從 tbl_name 中訪問(wèn)一個(gè)列,UPDATE 使用列的當(dāng)前值。舉例來(lái)說(shuō),下面的語(yǔ)句設(shè)置 age 列值為它的當(dāng)前值加 1 :
mysql> UPDATE persondata SET age=age+1;
UPDATE 賦值是從左到右計(jì)算的。舉例來(lái)說(shuō),下列語(yǔ)句將 age 列設(shè)置為它的兩倍,然后再加 1 :
mysql> UPDATE persondata SET age=age*2, age=age+1;
如果你設(shè)置列為其當(dāng)前的值,MySQL 注意到這點(diǎn),并不更新它。
UPDATE 返回實(shí)際被改變的記錄行數(shù)目。在 MySQL 3.22 或更新的版本中,C API 函數(shù) mysql_info()
返回被匹配并更新的記錄行數(shù)目,以及在 UPDATE 期間發(fā)生的警告的數(shù)目。
在 MySQL 3.23 中,你可以使用 LIMIT # 來(lái)確保只有給定的記錄行數(shù)目被更改。
如果一個(gè) ORDER BY 子句被使用(從 MySQL 4.0.0 開(kāi)始支持),記錄行將以指定的次序被更新。這實(shí)際上只有連同 LIMIT
一起才有用。
從 MySQL 4.0.4 開(kāi)始,你也可以執(zhí)行一個(gè)包含多個(gè)表的 UPDATE 的操作:
UPDATE items,month SET items.price=month.price
WHERE items.id=month.id;
注意:多表 UPDATE 不可以使用 ORDER BY 或 LIMIT。
關(guān)鍵字: mysql
啟動(dòng):net start mySql;
進(jìn)入:mysql -u root -p/mysql -h localhost -u root -p databaseName;
列出數(shù)據(jù)庫(kù):show databases;
選擇數(shù)據(jù)庫(kù):use databaseName;
列出表格:show tables;
顯示表格列的屬性:show columns from tableName;
建立數(shù)據(jù)庫(kù):source fileName.txt;
匹配字符:可以用通配符_代表任何一個(gè)字符,%代表任何字符串;
增加一個(gè)字段:alter table tabelName add column fieldName dateType;
增加多個(gè)字段:alter table tabelName add column fieldName1 dateType,add columns fieldName2 dateType;
多行命令輸入:注意不能將單詞斷開(kāi);當(dāng)插入或更改數(shù)據(jù)時(shí),不能將字段的字符串展開(kāi)到多行里,否則硬回車將被儲(chǔ)存到數(shù)據(jù)中;
增加一個(gè)管理員帳戶:grant all on *.* to user@localhost identified by "password";
每條語(yǔ)句輸入完畢后要在末尾填加分號(hào)';',或者填加'/g'也可以;
查詢時(shí)間:select now();
查詢當(dāng)前用戶:select user();
查詢數(shù)據(jù)庫(kù)版本:select version();
查詢當(dāng)前使用的數(shù)據(jù)庫(kù):select database();
1、刪除student_course數(shù)據(jù)庫(kù)中的students數(shù)據(jù)表:
rm -f student_course/students.*
2、備份數(shù)據(jù)庫(kù):(將數(shù)據(jù)庫(kù)test備份)
mysqldump -u root -p test>c:/test.txt
備份表格:(備份test數(shù)據(jù)庫(kù)下的mytable表格)
mysqldump -u root -p test mytable>c:/test.txt
將備份數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù):(導(dǎo)回test數(shù)據(jù)庫(kù))
mysql -u root -p test
3、創(chuàng)建臨時(shí)表:(建立臨時(shí)表zengchao)
create temporary table zengchao(name varchar(10));
4、創(chuàng)建表是先判斷表是否存在
create table if not exists students(……);
5、從已經(jīng)有的表中復(fù)制表的結(jié)構(gòu)
create table table2 select * from table1 where 1<>1;
6、復(fù)制表
create table table2 select * from table1;
7、對(duì)表重新命名
alter table table1 rename as table2;
8、修改列的類型
alter table table1 modify id int unsigned;//修改列id的類型為int unsigned
alter table table1 change id sid int unsigned;//修改列id的名字為sid,而且把屬性修改為int unsigned
9、創(chuàng)建索引
alter table table1 add index ind_id (id);
create index ind_id on table1 (id);
create unique index ind_id on table1 (id);//建立唯一性索引
10、刪除索引
drop index idx_id on table1;
alter table table1 drop index ind_id;
11、聯(lián)合字符或者多個(gè)列(將列id與":"和列name和"="連接)
select concat(id,':',name,'=') from students;
12、limit(選出10到20條)<第一個(gè)記錄集的編號(hào)是0>
select * from students order by id limit 9,10;