Mysql常用命令彙總

kboypkb發表於2021-09-09

一、Mysql安裝目錄
資料庫目錄:/var/lib/mysql/
配置檔案:/usr/share/mysql(mysql.server命令及配置檔案)
相關命令:/usr/bin(mysqladmin mysqldump等命令)
啟動指令碼:/etc/init.d/mysql(啟動指令碼檔案mysql的目錄)

二、系統管理
連線MySQL
格式: mysql -h 主機地址 -u使用者名稱 -p使用者密碼
例 1:連線到本機上的MySQL。

複製程式碼 程式碼如下:hadoop@ubuntu:~$ mysql -uroot -pmysql;


例 2:連線到遠端主機上的MYSQL。

複製程式碼 程式碼如下:hadoop@ubuntu:~$ mysql -h 127.0.0.1 -uroot -pmysql;


修改新密碼
在終端輸入:mysql -u使用者名稱 -p密碼,回車進入Mysql。

?

1234 > use mysql;> update user set password=PASSWORD('新密碼') where user='使用者名稱';> flush privileges; #更新許可權> quit; #退出

增加新使用者
格式:grant select on 資料庫.* to 使用者名稱@登入主機 identified by '密碼'
舉例:
例 1:增加一個使用者 test1 密碼為 abc,讓他可以在任何主機上登入,並對所有資料庫有
查詢、插入、修改、刪除的許可權。首先用以 root 使用者連入 MySQL,然後鍵入以下命令:

複製程式碼 程式碼如下:mysql>grant select,insert,update,delete on *.* to root@localhost identified by 'mysql';
或者
複製程式碼 程式碼如下:grant all privileges on *.* to root@localhost identified by 'mysql';
然後重新整理許可權設定:flush privileges;


例 2:如果你不想 root 有密碼運算元據庫“mydb”裡的資料表,可以再打一個命令將密碼消掉。

複製程式碼 程式碼如下:grant select,insert,update,delete on mydb.* to root@localhost identified by '';


刪除使用者

?

12345 hadoop@ubuntu:~$ mysql -u使用者名稱 -p密碼mysql>delete from user where user='使用者名稱' and host='localhost';mysql>flush privileges;//刪除使用者的資料庫mysql>drop database dbname;

三、資料庫操作
顯示所有的資料庫:mysql> show databases;(注意:最後有個 s)

建立資料庫:mysql> create database test;

連線資料庫:mysql> use test;

檢視當前使用的資料庫:mysql> select database();

當前資料庫包含的表資訊:mysql> show tables; (注意:最後有個 s)

刪除資料庫:mysql> drop database test;

四、表操作
備註:操作之前使用“use ”應連線某個資料庫。
建表命令:create table ( [,.. ]);
例子:

?

12345 mysql> create table MyClass(> id int(4) not null primary key auto_increment,> name char(20) not null,> sex int(4) not null default '0',> degree double(16,2));

獲取表結構命令: desc 表名,或者show columns from 表名
例子:

?

123 mysql> describe MyClassmysql> desc MyClass;mysql> show columns from MyClass;

刪除表命令:drop table
例如:刪除表名為 MyClass 的表

複製程式碼 程式碼如下:mysql> drop table MyClass;


插入資料命令:insert into [( [,.. ])] values ( 值 1 )[, ( 值 n )]
例子:

複製程式碼 程式碼如下:mysql> insert into MyClass values(1,'Tom',96.45),(2,'Joan',82.99), (2,'Wang', 96.59);


查詢表中的資料
查詢所有行:mysql> select * from MyClass;

查詢前幾行資料
例如:檢視錶 MyClass 中前 2 行資料

複製程式碼 程式碼如下:mysql> select * from MyClass order by id limit 0,2;
或者
複製程式碼 程式碼如下:mysql> select * from MyClass limit 0,2;


刪除表中資料命令:delete from 表名 where 表示式
例如:刪除表 MyClass 中編號為 1 的記錄

複製程式碼 程式碼如下:mysql> delete from MyClass where id=1;


修改表中資料命令:update 表名 set 欄位=新值,... where 條件

複製程式碼 程式碼如下:mysql> update MyClass set name='Mary' where id=1;


在表中增加欄位命令:alter table 表名 add 欄位 型別 其他;
例如:在表 MyClass 中新增了一個欄位 passtest,型別為 int(4),預設值為 0

複製程式碼 程式碼如下:mysql> alter table MyClass add passtest int(4) default '0'


更改表名命令:rename table 原表名 to 新表名;
例如:在表 MyClass 名字更改為 YouClass

複製程式碼 程式碼如下:mysql> rename table MyClass to YouClass;


更新欄位內容命令:update 表名 set 欄位名 = 新內容
update 表名 set 欄位名 = replace(欄位名, '舊內容', '新內容');
例如:文章前面加入 4 個空格

複製程式碼 程式碼如下:update article set content=concat('    ', content);


五、資料庫匯入匯出
從資料庫匯出資料庫檔案使用“mysqldump”命令,首先進入 DOS 介面,然後進行下面操作。
1)匯出所有資料庫
格式:mysqldump -u [資料庫使用者名稱] -p -A>[備份檔案的儲存路徑]

2)匯出資料和資料結構
格式:mysqldump -u [資料庫使用者名稱] -p [要備份的資料庫名稱]>[備份檔案的儲存路徑]
舉例:
例1:將資料庫 mydb 匯出到 e:MySQLmydb.sql 檔案中。
開啟開始->執行->輸入“cmd”,進入命令列模式。
c:> mysqldump -h localhost -u root -p mydb >e:MySQLmydb.sql
然後輸入密碼,等待一會匯出就成功了,可以到目標檔案中檢查是否成功。

例2:將資料庫 mydb 中的 mytable 匯出到 e:MySQLmytable.sql 檔案中。
c:> mysqldump -h localhost -u root -p mydb mytable>e:MySQLmytable.sql

例3:將資料庫 mydb 的結構匯出到 e:MySQLmydb_stru.sql 檔案中。
c:> mysqldump -h localhost -u root -p mydb --add-drop-table >e:MySQLmydb_stru.sql
備註:-h localhost 可以省略,其一般在虛擬主機上用。

3)只匯出資料不匯出資料結構
格式:
mysqldump -u [資料庫使用者名稱] -p -t [要備份的資料庫名稱]>[備份檔案的儲存路徑]

4)匯出資料庫中的Events
格式:mysqldump -u [資料庫使用者名稱] -p -E [資料庫使用者名稱]>[備份檔案的儲存路徑]

5)匯出資料庫中的儲存過程和函式
格式:mysqldump -u [資料庫使用者名稱] -p -R [資料庫使用者名稱]>[備份檔案的儲存路徑]

從外部檔案匯入資料庫中
1)使用“source”命令
首先進入“mysql”命令控制檯,然後建立資料庫,然後使用該資料庫。最後執行下面操作。
mysql>source [備份檔案的儲存路徑]

2)使用“首先進入“mysql”命令控制檯,然後建立資料庫,然後退出 MySQL,進入 DOS 介面。最後執行下面操作。
mysql -u root –p

以上就是Mysql常用命令彙總,希望對大家的學習有所幫助。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/1916/viewspace-2804641/,如需轉載,請註明出處,否則將追究法律責任。

相關文章