命令列操作MySQL的基本命令

2021-05-07 14:41:01 字數 3932 閱讀 8688

一、從命令列登入mysql資料庫伺服器

1、登入使用預設3306埠的mysql

/usr/local/webserver/mysql/bin/mysql -u root -p

2、通過tcp連線管理不同埠的多個mysql(注:mysql4.1以上版本才有此項功能)

/usr/local/webserver/mysql/bin/mysql -u root -p --protocol=tcp --host=localhost --port=3306

3、通過socket套接字管理不同埠的多個mysql

/usr/local/webserver/mysql/bin/mysql -u root -p --socket=/tmp/mysql.sock

4、通過埠和ip管理不同埠的多個mysql

/usr/local/webserver/mysql/bin/mysql -u root -p -p 3306 -h 127.0.0.1

二、資料庫操作sql語句

1、顯示伺服器上當前存在什麼資料庫

show databases;

2、建立名稱為maps的資料庫

create database maps;

3、刪除名稱為maps的資料庫

drop database maps;

4、選擇maps資料庫

use maps;

5、檢視當前主機的運**況

show full processlist;

三、資料庫中表操作sql語句

1、顯示當前資料庫中列表

show tables;

2、建立資料庫表maps_user:儲存引擎為myisam,欄位uid為主鍵、唯一索引。

create table `maps_user` (

`uid` int( 5 ) unsigned not null auto_increment ,

`username` varchar( 20 ) not null ,

`password` char( 32 ) not null ,

`dateline` int(10) not null ,

primary key ( `uid` )

) engine = myisam ;

3、檢視maps_user表結構

describe maps_user; // desc maps_user;

4、從表中檢索資訊

4.1、從maps_user表中檢索所有記錄

select * from maps_user;

4.2、從maps_user表中檢索特定的行:欄位username等於abc,按欄位id降序排列

select * from maps_user where username = 'wan' order by id desc;

4.3、從maps_user表中檢索指定的字段:username和password

select username, password from maps_user;

4.4、從maps_user表中檢索出唯一的不重覆記錄:

select distinct username from maps_user;

5、插入資訊到maps_user表

insert into maps_user (id, username, password, dateline) values ('', 'wan', '123456', '1234231231');

6、更新maps_user表中的指定資訊

update maps_user set password = '123123' where id = 12;

7、批量替換的sql語句(將字串aaa批量替換為bbb的sql語句)

update 表名 set 欄位名 = replace (欄位名, 'aaa', 'bbb');

update maps_user set dateline = replace (dateline, '12231231', '126657333');

8、刪除maps_user表中的指定記錄

delete from maps_user where id = 12;

9、清空maps_user表

delete from maps_user; 或 truncate maps_user;

10、刪除maps_user表

drop table maps_user;

11、更改表結構,將maps_user表username欄位的字段型別改為char(25)

alter table maps_user change username username char(25);

12、匯入.sql檔案到mysql中

/usr/local/webserver/mysql/bin>mysql  -u  使用者名稱 -p  資料庫名  <   ./maps.sql    (將當前目錄下的mysql.sql匯入資料庫) 或:source ./mysql.sql;

例如:/usr/local/webserver/mysql/bin>mysql -u root -p test < ./maps.sql

enter password: ****

13、update多表更新:

mysql語法: update tablename set col_name1=expr1 [, col_name2=expr2 ...] [where where_definition]

mysql示例: update tablea a, table b set a.uid= b.uid, a.username= b.username where a.id=b.id

14、對sql語句的分析(desc/explain)

explain select * from maps_user where username = 'wan' order by id desc;

四、資料庫許可權操作sql語句

1、建立乙個具有root許可權,可從任何ip登入的使用者ctowoo,密碼為123456

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

2、建立乙個具有「資料操作」、「結構操作」許可權,只能從192.168.122.***登入的使用者ctowoo,密碼為123456

grant select , insert , update , delete , file , create , drop , index , alter , create temporary tables , create view , show view , create routine, alter routine, execute on *.* to 'ctowoo'@'192.168.122.%' identified by '123456';

3、建立乙個只擁有「資料操作」許可權,只能從192.168.122.24登入,只能操作maps資料庫的maps_user表的使用者ctowoo,密碼為123456

grant select , insert , update , delete on  maps.maps_user to 'ctowoo'@'192.168.122.24' identified by '123456';

4、建立乙個擁有「資料操作」、「結構操作」許可權,可從任何ip登入,只能操作maps資料庫的使用者ctowoo,密碼為123456

grant select , insert , update , delete , create , drop , index , alter , create temporary tables , create view , show view , create routine, alter routine, execute on rewin.* to 'ctowoo'@'%' identified by '123456';

5、刪除使用者

drop user 'ctowoo'@'%';

MySQL命令列基本命令操作

進入命令模式後,顯示所有資料庫 show databases 選定某個資料庫 use 資料庫名 建立資料庫 create database 資料庫名 刪除資料庫 drop table 資料庫名 顯示資料庫的所有表 show tables 建立表create table 表名 刪除表drop tabl...

mysql基本命令列

update user setpassword password 123456 where user root 修改密碼 flush privileges 重新整理資料庫 show databases 顯示所有資料庫 use dbname 開啟某個資料庫 show tables 顯示資料庫mysql...

dos基本命令列操作

進入某根目錄 碟符 如 d 進入d盤 資料夾下內容瀏覽 dir 單級進入某資料夾 cd 檔名 如 cd 360downloads 多級進入某資料夾 cd 檔案路徑 如 cd 360downloads aaa 單級回退 回退到上一資料夾 cd.回退到根目錄 cd 建立目錄 md 刪除目錄 rd 刪除檔...