mysql檢視配置命令大全 Mysql常用命令彙總

2021-10-17 22:02:13 字數 4702 閱讀 5202

一、mysql安裝目錄

資料庫目錄:/var/lib/mysql/

配置檔案:/usr/share/mysql(mysql.server命令及配置檔案)

二、系統管理

連線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。

> 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 '';

刪除使用者

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 ( [,.. ]);

例子:mysql> create table myclass(

> id int(4) not null primary key auto_increment,

> name char(20) not null,

> *** int(4) not null default '0',

> degree double(16,2));

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

例子:mysql> describe myclass

mysql> 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:\mysql\mydb.sql 檔案中。

開啟開始->執行->輸入「cmd」,進入命令列模式。

c:\> mysqldump -h localhost -u root -p mydb >e:\mysql\mydb.sql

然後輸入密碼,等待一會匯出就成功了,可以到目標檔案中檢查是否成功。

例2:將資料庫 mydb 中的 mytable 匯出到 e:\mysql\mytable.sql 檔案中。

c:\> mysqldump -h localhost -u root -p mydb mytable>e:\mysql\mytable.sql

例3:將資料庫 mydb 的結構匯出到 e:\mysql\mydb_stru.sql 檔案中。

c:\> mysqldump -h localhost -u root -p mydb --add-drop-table >e:\mysql\mydb_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 -u root –p < [備份檔案的儲存路徑]

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

您可能感興趣的文章:mysql基礎入門 輕鬆學習mysql命令

mysql常用命令 mysql處理資料庫和表的命令

mysql最基本的命令使用彙總

mysql命令大全(完整版)

mysql檢視表和清空表的常用命令總結

mysqldump命令匯入匯出資料庫方法與例項彙總

mysql匯入匯出命令解析

詳解mysql中alter命令的使用

windows下通過dos命令登入mysql的方法

mysql命令例項彙總

mysql 命令大全 Mysql 命令大全

輸入mysql進入,如同輸入cmd模式 1 使用show語句找出在伺服器上當前存在什麼資料庫.mysql show databases 2 建立乙個資料庫mysqldata mysql create database mysqldata.3 選擇你所建立的資料庫 mysql use mysqldat...

mysql配置命令大全 MySQL常用命令彙總

非互動式超時時間,如 jdbc 程式 show global variables like wait timeout 互動式超時時間,如資料庫工具 show global variables like interactive timeout 連線數配置,太小會導致too many connectio...

網頁mysql命令大全 Mysql命令大全

monica sehgal hal simlai joseph irvine 3 rows in set 0.00 sec 注意 這裡用到concat 函式,用來把字串串接起來。另外,我們還用到以前學到的as給結果列 concat f name,l name 起了個假名。5.1 建立資料表 命令 c...