綠色資源網:您身邊最放心的安全(quán)下載站! 最新軟件|熱門排行|軟件(jiàn)分類|軟件專題|廠商大(dà)全

綠色資源網

技術教程
您的位置:首頁數據庫類MySQL → 常用的MySQL數據庫命令大全

常用(yòng)的MySQL數據庫(kù)命(mìng)令大全

我要評論(lùn) 2011/12/25 15:05:47 來源:綠色資源(yuán)網 編輯:ynaad.com [ ] 評論:0 點擊:297次

常用的MySQL命令大全

一、連接MySQL
格(gé)式: mysql -h主機地(dì)址 -u用戶名 -p用戶密碼
1、例(lì)1:連接到本機(jī)上的(de)MYSQL。
首先(xiān)在打開(kāi)DOS窗口,然後進入(rù)目錄 mysqlbin,再鍵入(rù)命令mysql -uroot -p,回車後提示你輸密(mì)碼,如果剛安裝好MYSQL,超級用戶root是沒(méi)有密碼的,故直接回車即可進入到MYSQL中了,MYSQL的提示符是: mysql>。
2、例2:連接到遠程主機上的MYSQL。假設遠程主機的IP為:110.110.110.110,用戶名為root,密碼為abcd123。則鍵入以下命令:
mysql -h110.110.110.110 -uroot -pabcd123
(注(zhù):u與root可以不用加空格,其它也一樣)
3、退出MYSQL命令: exit (回車)。

修改mYSQL 管理(lǐ)員密碼

二、修改密碼
格式:mysqladmin -u用戶名 -p舊密碼 password 新密碼
1、例1:給root加個(gè)密碼ab12。首先在DOS下進入目錄mysqlbin,然後鍵入(rù)以下命令:
mysqladmin -uroot -password ab12
注:因為開始時root沒有(yǒu)密碼,所以-p舊密碼一項就可以(yǐ)省略(luè)了。
2、例2:再將root的密碼改為djg345。
mysqladmin -uroot -pab12 password djg345
 

增加新用

三、增(zēng)加新用戶。(注意:和上麵不同,下麵的因(yīn)為是MySQL環境中的命令,所以後麵(miàn)都帶一個分號作為命令結束符)
格式:grant select on 數據庫.* to 用戶名@登錄(lù)主機 identified by \"密碼\"
例1、增加一個(gè)用戶test1密碼為abc,讓他可以在(zài)任何主機上(shàng)登錄,並對所有數據庫有查詢、插(chā)入、修改、刪除(chú)的權限。首先用以root用戶連入MySQL,然(rán)後鍵入(rù)以下命令:
grant select,insert,update,
delete on *.* to test2@localhost identified by \"abc\";
如果你不想test2有密碼,可以再打一個命令(lìng)將密碼消掉。
grant select,insert,update,delete on mydb
.* to test2@localhost identified by \"\";
在上麵講了登錄、增加(jiā)用戶、密碼更改等問題。下(xià)麵我們來看看MySQL中有(yǒu)關數(shù)據(jù)庫方麵的操作(zuò)。注意:你必須首先登錄到MySQL中,以下操作都是在MySQL的提示符下進行的,而(ér)且每個命令以分號結束。
1、MySQL常用命令
create database name; 創建數據庫
use databasename; 選擇(zé)數據庫
drop database name 直接刪除數據庫,不提醒
show tables; 顯(xiǎn)示(shì)表
describe tablename; 表的(de)詳細描述
select 中加上distinct去除重複字段(duàn)
mysqladmin drop database name 刪除數據庫前,有提示。
顯示當前mysql版本和當前日期
select version(),current_date;
2、修改mysql中(zhōng)root的密碼:
shell>mysql -u root -p
mysql> update user set password=password(”xueok654123″) where user=’root’;
mysql> flush privileges //刷新數據庫
mysql>use dbname; 打開數據庫:
mysql>show databases; 顯示所有數據庫
mysql>show tables; 顯(xiǎn)示數據庫mysql中(zhōng)所有的表:先use mysql;然後
mysql>describe user; 顯示表mysql數據庫中user表的列(liè)信(xìn)息(xī));
3、grant
創建一個可以從任何地方連(lián)接服務器的一個完全的超級用戶,但是必須(xū)使用一個口令something做(zuò)這個
mysql> grant all privileges on *.* to user@localhost identified by ’something’ with
增加新用戶
格(gé)式:grant select on 數據庫.* to 用戶名@登錄主機 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;
創建(jiàn)一個用戶custom在特(tè)定(dìng)客戶(hù)端(duān)it363.com登錄,可(kě)訪問特定數據庫fangchandb
mysql >grant select, insert, update, delete, create,drop on fangchandb.* to custom@ it363.com identified by ‘ passwd’
重命名表:
mysql > alter table t1 rename t2;
4、mysqldump

恢複、備份數據庫(kù)

備份數據庫
shell> mysqldump -h host -u root -p dbname >dbname_backup.sql
恢複數據庫
shell> mysqladmin -h myhost -u root -p create dbname
shell> mysqldump -h host -u root -p dbname < dbname_backup.sql
如果隻(zhī)想卸出建表指(zhǐ)令(lìng),則命令如(rú)下:
shell> mysqladmin -u root -p -d databasename > a.sql
如果隻(zhī)想卸出插入數據的sql命令,而不需要建表命令(lìng),則命令如下:
shell> mysqladmin -u root -p -t databasename > a.sql
那麽如果我隻想要數據,而不想(xiǎng)要什(shí)麽sql命令(lìng)時,應該如何操作呢?
   mysqldump -T./ phptest driver
其(qí)中,隻有指定了-T參數才可以卸出(chū)純文本(běn)文件,表示卸出數據(jù)的目錄,./表示當前(qián)目錄,即與mysqldump同(tóng)一目錄。如果不指定(dìng)driver 表,則將(jiāng)卸出整個數據庫的數據(jù)。每個(gè)表會(huì)生成兩(liǎng)個文件,一個為.sql文(wén)件,包含建(jiàn)表執行。另一個為.txt文件,隻包含(hán)數據,且(qiě)沒有sql指令。
5、可將查詢存儲在一個文件中並告訴mysql從文件中讀取查詢(xún)而(ér)不是等待鍵盤輸入。可利用外殼程(chéng)序鍵入重定向實用程序來完成這項工作。例如,如果在文件my_file.sql 中存放有查
詢,可如下執行這些查詢:
例如,如果您想將建表語句(jù)提前寫在sql.txt中:
mysql > mysql -h myhost -u root -p database < sql.txt
1、安裝環境:
Windows XP
Mysql 4.0.17 從 下次就需要用(yòng)mysql -uroot -proot才可以登陸
在遠程或(huò)本機可以使用 mysql -h 172.5.1.183 -uroot 登陸,這個根據第二行的策略確定
權限修改生效:
1)net stop mysql
net start mysql
2)c:\mysql\bin\mysqladmin flush-privileges
3)登陸mysql後,用flush privileges語句
6、創建數據(jù)庫staffer
create database staffer;
7、下麵的語句在mysql環境在執行(háng)
顯(xiǎn)示用戶擁有權限的數據庫 show databases;
切(qiē)換到(dào)staffer數據庫 use staffer;
顯示當前數據庫中有權限的表 show tables;
顯示表staffer的結構 desc staffer;
8、創建測(cè)試環境
1)創建數據庫staffer
mysql> create database staffer
2)創建表staffer,department,position,depart_pos
create table s_position
(
id int not null auto_increment,
name varchar(20) not null default '經理', #設定默認值
description varchar(100),
primary key PK_positon (id) #設定(dìng)主鍵
);
create table department
(
id int not null auto_increment,
name varchar(20) not null default '係統部', #設定默認值
description varchar(100),
primary key PK_department (id) #設定主(zhǔ)鍵
);
create table depart_pos
(
department_id int not null,
position_id int not null,
primary key PK_depart_pos (department_id,position_id) #設定(dìng)複和主鍵
);
create table staffer
(
id int not null auto_increment primary key, #設定主(zhǔ)鍵(jiàn)
name varchar(20) not null default '無(wú)名氏', #設定默認值
department_id int not null,
position_id int not null,
unique (department_id,position_id) #設定唯一值
);
3)刪除
mysql>
drop table depart_pos;
drop table department;
drop table s_position;
drop table staffer;
drop database staffer;
9、修改結(jié)構
mysql>
#表position增加列test
alter table position add(test char(10));
#表position修改列test
alter table position modify test char(20) not null;
#表position修改列test默認值
alter table position alter test set default 'system';
#表position去掉test默認值
alter table position alter test drop default;
#表position去掉列(liè)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、操作數據
#插入表department
insert into department(name,description) values('係統部','係(xì)統部');
insert into department(name,description) values('公關(guān)部','公關部');
insert into department(name,description) values('客服部','客服部');
insert into department(name,description) values('財務部','財務(wù)部');
insert into department(name,description)

關鍵詞:MySQL,數據庫,MySQL數據庫命令

閱讀本文後(hòu)您有什麽感想? 已有(yǒu) 人給出評價!

  • 1 歡迎喜歡
  • 1 白癡
  • 1 拜(bài)托
  • 1 哇
  • 1 加油
  • 1 鄙視
免费人欧美成又黄又爽的视频丨一本色道久久88综合日韩精品丨国产专区日韩精品欧美色丨午夜无遮挡男女啪啪视频丨国产欧美日韩综合精品一区二区丨亚洲精品无码不卡在线播HE丨亚洲精品国产精品国自产观看丨日韩国产高清av不卡