mysql常用命令

來源:互聯網
上載者:User

標籤:des   blog   http   io   os   ar   使用   for   sp   

一、啟動與關閉

1.1 Linux下啟動mysql 的命令:

  • a. rpm包安裝:service mysqld start
  • b. 源碼包安裝:/usr/local/mysql/bin/mysqld_safe --user=mysql &

1.2 Linux下重啟mysql 的命令:

  • a. rpm包安裝:service mysqld restart
  • b. 源碼包安裝:
    • 先關閉mysql

      /usr/local/mysql/bin/mysqladmin -uroot -p shutdown

    • 再啟動mysql/usr/local/mysql/bin/mysqld_safe --user=mysql &

1.3 Linux下關閉mysql 的命令:

  • a. rpm包安裝:service mysqld stop
  • b. 源碼包安裝:
    • 方法1、/usr/local/mysql/bin/mysqladmin -uroot -p shutdown
    • 方法2、killall mysqld           //強行終止MySQL資料庫服務,有可能導致表損壞,不建議使用

二、資料庫連接

2.1 串連MySQL

格式:$mysql_dir/bin/mysql [-h主機地址] -u使用者名稱 -p使用者密碼,斷行符號後提示輸入密碼。

2.2 退出MySQL

格式:exit/quit

 

三、修改密碼

3.1 mysqladmin 命令

格式:mysqladmin -u使用者名稱 -p舊密碼 password 新密碼

例1:給root加個密碼123456。首先在終端下進入目錄$mysql_dir/bin,然後鍵入以下命令

./mysqladmin -uroot password ’123456′

註:因為開始時root沒有密碼,所以-p舊密碼一項就可以省略了。

例2:再將root的密碼改為abc123。

./mysqladmin -uroot -p123456 password abc123

3.2 UPDATE user 語句

首先使用root 帳戶登入mysql,然後執行:

UPDATE mysql.user SET password=PASSWORD(‘123456‘) WHERE user=‘root‘;FLUSH PRIVILEGES;

3.3 SET PASSWORD 語句

同樣,首先使用root 帳戶登入mysql,然後執行:

SET PASSWORD FOR ‘root‘@‘localhost‘ = PASSWORD(‘abc123‘);

四、建立使用者與授權

4.1 CREATE USER

CREATE USER user [IDENTIFIED BY [PASSWORD] ‘password‘]    [, user [IDENTIFIED BY [PASSWORD] ‘password‘]] ...

CREATE USER用於建立新的MySQL賬戶。要使用CREATE USER,您必須擁有mysql 資料庫的全域CREATE USER許可權,或擁有INSERT許可權。對於每個賬戶,CREATE USER會在沒有許可權的mysql.user表中建立一個新記錄。如果賬戶已經存在,則出現錯誤。

使用自選的IDENTIFIED BY子句,可以為賬戶指定一個密碼。user值和密碼的給定方法和GRANT語句一樣。特別要注意的是,要在純文字中指定密碼,需忽略PASSWORD關鍵詞。要把密碼指定為由PASSWORD()函數返回的混編值,需包含關鍵字PASSWORD。

例1:建立新使用者david & sandy

mysql> create user ‘david‘@‘localhost‘ identified by ‘password‘;Query OK, 0 rows affected (0.00 sec)mysql>
mysql> create user ‘sandy‘@‘localhost‘ identified by PASSWORD ‘*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19‘;Query OK, 0 rows affected (0.00 sec)mysql>

4.2 使用GRANT語句

最好的方法是使用GRANT語句,因為這樣更精確,錯誤少。從MySQL 3.22.11起提供了GRANT;它的主要用途是來給帳戶授權的,但也可用來建立新帳戶並同時授權。

注意:當mysql 運行於no_auto_create_user 時要提供建立使用者的密碼,否則不能建立新使用者。

格式:

GRANT privileges ON databasename.tablename TO ‘username‘@‘host‘ identified by ‘password‘; 

例1、增加一個使用者test1,密碼為abc,讓他可以在任何主機上登入,並對所有資料庫擁有所有許可權。

首先用以root使用者連入MySQL,然後鍵入以下命令:

grant all privileges on *.* to ‘test1‘@‘%‘ identified  by ‘abc‘;flush privileges;

例2、增加一個使用者test2,密碼為abc,讓他只可以在localhost上登入,並可以對資料庫david進行查詢、插入、修改、刪除的操作。

grant select,insert,update,delete on david.* to ‘test2‘@‘localhost‘ identified by ‘abc‘;flush privileges;

如果不想test2有密碼,可以再打一個命令將密碼消掉。

grant select,insert,update,delete on david.* to ‘test2‘@‘localhost‘ identified by ‘‘;

grant 更多用法,請自行Google+Baidu。

4.3 直接操作MySQL授權表

除了GRANT,你可以直接用INSERT語句建立相同的賬戶,然後使用FLUSH PRIVILEGES告訴伺服器重載授權表。

例1:建立使用者test3,讓其具有同4.2 例2中test2使用者同樣的許可權。

mysql> insert into mysql.user (Host, User, Password, Select_priv, Insert_priv, Update_priv, Delete_priv) values(‘localhost‘, ‘test3‘, PASSWORD(‘password‘),‘Y‘,‘Y‘,‘Y‘,‘Y‘);Query OK, 1 row affected, 3 warnings (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)mysql> 
mysql> select Host, User, Password, Select_priv, Insert_priv, Update_priv, Delete_priv from mysql.user where User=‘test3‘;+-----------+-------+-------------------------------------------+-------------+-------------+-------------+-------------+| Host      | User  | Password                                  | Select_priv | Insert_priv | Update_priv | Delete_priv |+-----------+-------+-------------------------------------------+-------------+-------------+-------------+-------------+| localhost | test3 | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 | Y           | Y           | Y           | Y           | +-----------+-------+-------------------------------------------+-------------+-------------+-------------+-------------+1 row in set (0.00 sec)mysql> 

當用INSERT建立賬戶時,使用FLUSH PRIVILEGES 的原因是告訴伺服器重讀授權表。否則,只有重啟伺服器後更改才會生效。使用 GRANT,則不需要使用FLUSH PRIVILEGES。

用INSERT語句時,使用PASSWORD()函數是為了加密密碼。GRANT語句自動加密密碼,因此不需要PASSWORD()。

‘Y‘ 值啟用賬戶許可權。

五、庫操作

必須首先登入到mysql 中,有關操作都是在mysql 的提示符下進行,而且每個命令以分號結束。

5.1 建立資料庫

命令:create database <資料庫名>;

例1:建立一個名為test 的資料庫

mysql> create database test;Query OK, 1 row affected (0.00 sec)mysql>

5.2 顯示所有的資料庫

命令:show databases;(注意:最後有個s)

mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema | | david              | | mysql              | | test               | +--------------------+4 rows in set (0.00 sec)mysql> 

5.3 刪除資料庫

命令:drop database <資料庫名>;

例2:刪除名為test 的資料庫

mysql> drop database test;Query OK, 0 rows affected (0.00 sec)mysql> 

5.4 串連資料庫

命令: use <資料庫名>;

例3:串連david 資料庫

mysql> use david;Database changedmysql> 

5.5 查看當前使用的資料庫

命令:select database();

mysql> select database();+------------+| database() |+------------+| david      | +------------+1 row in set (0.00 sec)mysql> 

5.6 當前資料庫包含的表資訊

命令:show tables;(注意:最後有個s)

mysql> show tables;+-----------------+| Tables_in_david |+-----------------+| emp             | +-----------------+1 row in set (0.00 sec)mysql> 

六、表操作

操作之前應串連某個資料庫。

6.1 建立表

create table <表名> (<欄位名1> <類型1> [,..<欄位名n> <類型n>]);create table tablename (col1 type1 [not null] [primary key],col2 type2 [not null],..);
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));Query OK, 0 rows affected (0.04 sec)mysql> 

補充:根據已有的表建立新表。

create table tab_new like tab_old; (只有表結構)

create table tab_new as select * from tab_old; (既包含表結構,又包含表資料)

只包含表結構:

mysql> create table myclass2 like myclass;Query OK, 0 rows affected (0.00 sec)mysql> 

既包含表結構,又包含表資料:

mysql> insert into myclass values(1, ‘david‘, 1, 20130417.16);Query OK, 1 row affected (0.02 sec)mysql> mysql> mysql> create table myclass3 as select * from myclass;Query OK, 1 row affected (0.07 sec)Records: 1  Duplicates: 0  Warnings: 0mysql> select * from myclass3;+----+-------+-----+-------------+| id | name  | sex | degree      |+----+-------+-----+-------------+|  1 | david |   1 | 20130417.16 | +----+-------+-----+-------------+1 row in set (0.02 sec)mysql> 

6.2 擷取表結構

命令:

desc 表名;orshow columns from 表名;

擷取myclass & myclass2 表結構

mysql> desc myclass;+--------+--------------+------+-----+---------+----------------+| Field  | Type         | Null | Key | Default | Extra          |+--------+--------------+------+-----+---------+----------------+| id     | int(4)       | NO   | PRI | NULL    | auto_increment | | name   | char(20)     | NO   |     |         |                | | sex    | int(4)       | NO   |     | 0       |                | | degree | double(16,2) | YES  |     | NULL    |                | +--------+--------------+------+-----+---------+----------------+4 rows in set (0.00 sec)mysql> show columns from myclass2;+--------+--------------+------+-----+---------+----------------+| Field  | Type         | Null | Key | Default | Extra          |+--------+--------------+------+-----+---------+----------------+| id     | int(4)       | NO   | PRI | NULL    | auto_increment | | name   | char(20)     | NO   |     |         |                | | sex    | int(4)       | NO   |     | 0       |                | | degree | double(16,2) | YES  |     | NULL    |                | +--------+--------------+------+-----+---------+----------------+4 rows in set (0.00 sec)mysql> 

6.3 刪除表

命令:drop table <表名>;

例:刪除表名為 myclass3 的表

mysql> drop table myclass3;Query OK, 0 rows affected (0.00 sec)mysql> show tables;+-----------------+| Tables_in_david |+-----------------+| emp             | | myclass         | | myclass2        | +-----------------+3 rows in set (0.00 sec)mysql> 

6.4 更改表名

命令:rename table 原表名 to 新表名;

例:將表 myclass2 名字更改為 myclass4

mysql> rename table myclass2 to myclass4;Query OK, 0 rows affected (0.02 sec)mysql> show tables;+-----------------+| Tables_in_david |+-----------------+| emp             | | myclass         | | myclass4        | +-----------------+3 rows in set (0.00 sec)mysql> 

6.5 在表中增加欄位

命令:alter table 表名 add 欄位 類型 其他;

例:在表 myclass 中添加了一個欄位passtest,類型為int(4),預設值為0。

mysql> alter table myclass add passtest int(4) default ‘0‘;Query OK, 1 row affected (0.04 sec)Records: 1  Duplicates: 0  Warnings: 0mysql> desc myclass;+----------+--------------+------+-----+---------+----------------+| Field    | Type         | Null | Key | Default | Extra          |+----------+--------------+------+-----+---------+----------------+| id       | int(4)       | NO   | PRI | NULL    | auto_increment | | name     | char(20)     | NO   |     |         |                | | sex      | int(4)       | NO   |     | 0       |                | | degree   | double(16,2) | YES  |     | NULL    |                | | passtest | int(4)       | YES  |     | 0       |                | +----------+--------------+------+-----+---------+----------------+5 rows in set (0.00 sec)mysql> 

七、資料操作

7.1 插入資料

命令:insert into <表名> [( <欄位名1>[,..<欄位名n > ])] values ( 值1 )[, ( 值n )];

例:向 myclass 表中插入以下記錄,留空的表示使用預設值。

mysql> insert into myclass (id, name, sex, degree, passtest) values(1, ‘david‘, 1, 80.56, 78);     Query OK, 1 row affected (0.00 sec)mysql> insert into myclass values(2, ‘sandy‘, 0, 100, 90);Query OK, 1 row affected (0.00 sec)mysql> insert into myclass (id, name, sex, degree) values(3, ‘renee‘, 0, 90.34);Query OK, 1 row affected (0.00 sec)mysql> insert into myclass (id, name) values(4, ‘china‘);Query OK, 1 row affected (0.00 sec)mysql>

7.2 查詢表中的資料

a. 查詢所有行

命令:select <欄位1,欄位2,...> from < 表名 > where < 運算式 >;

例1:查看錶 myclass 中所有資料

mysql> select * from myclass;+----+-------+-----+--------+----------+| id | name  | sex | degree | passtest |+----+-------+-----+--------+----------+|  1 | david |   1 |  80.56 |       78 | |  2 | sandy |   0 | 100.00 |       90 | |  3 | renee |   0 |  90.34 |        0 | |  4 | china |   0 |   NULL |        0 | +----+-------+-----+--------+----------+4 rows in set (0.00 sec)mysql> 

例2:查詢表 david 相關資訊

mysql> select * from myclass where name=‘david‘;+----+-------+-----+--------+----------+| id | name  | sex | degree | passtest |+----+-------+-----+--------+----------+|  1 | david |   1 |  80.56 |       78 | +----+-------+-----+--------+----------+1 row in set (0.00 sec)mysql> 

b. 查詢前幾行資料

例如:查看錶 myclass 中前2行資料

mysql> select * from myclass limit 2;+----+-------+-----+--------+----------+| id | name  | sex | degree | passtest |+----+-------+-----+--------+----------+|  1 | david |   1 |  80.56 |       78 | |  2 | sandy |   0 | 100.00 |       90 | +----+-------+-----+--------+----------+2 rows in set (0.00 sec)mysql>

或者:

mysql> select * from myclass order by id limit 2;+----+-------+-----+--------+----------+| id | name  | sex | degree | passtest |+----+-------+-----+--------+----------+|  1 | david |   1 |  80.56 |       78 | |  2 | sandy |   0 | 100.00 |       90 | +----+-------+-----+--------+----------+2 rows in set (0.01 sec)mysql> 

7.3 刪除表中的資料

命令:delete from 表名 where 運算式;

例如:刪除表 myclass 中編號為4的記錄

mysql> delete from myclass where id=4;Query OK, 1 row affected (0.00 sec)mysql> select * from myclass;+----+-------+-----+--------+----------+| id | name  | sex | degree | passtest |+----+-------+-----+--------+----------+|  1 | david |   1 |  80.56 |       78 | |  2 | sandy |   0 | 100.00 |       90 | |  3 | renee |   0 |  90.34 |        0 | +----+-------+-----+--------+----------+3 rows in set (0.00 sec)mysql> 

7.4 修改表中的資料

update 表名 set 欄位 = 新值,… where 條件;

例:修改 myclass 表中編號為1的記錄,將degree 值改成89.99

mysql> update myclass set degree=89.99 where id=1;Query OK, 1 row affected (0.00 sec)Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from myclass;+----+-------+-----+--------+----------+| id | name  | sex | degree | passtest |+----+-------+-----+--------+----------+|  1 | david |   1 |  89.99 |       78 | |  2 | sandy |   0 | 100.00 |       90 | |  3 | renee |   0 |  90.34 |        0 | +----+-------+-----+--------+----------+3 rows in set (0.00 sec)mysql> 

八、資料的匯入匯出

8.1 匯出整個資料庫

命令:mysqldump -u 使用者名稱 -p 資料庫名 > 匯出的檔案名稱

[[email protected] bin]# ./mysqldump -uroot -p david > /tmp/david/david.sqlEnter password: [[email protected] bin]# ll /tmp/david/total 4-rw-r--r-- 1 root root 2764 Apr 17 17:13 david.sql[[email protected] bin]# 

8.2 匯出一個表

命令:mysqldump -u 使用者名稱 -p 資料庫名 表名> 匯出的檔案名稱

[[email protected] bin]# ./mysqldump -uroot -p david myclass > /tmp/david/david_myclass.sqlEnter password: [[email protected] bin]# ll /tmp/david/total 8-rw-r--r-- 1 root root 1854 Apr 17 17:16 david_myclass.sql-rw-r--r-- 1 root root 2764 Apr 17 17:13 david.sql[[email protected] bin]# 

8.3 匯出一個資料庫結構

命令:mysqldump -u root -p -d --add-drop-table test > test_db.sql

-d 沒有資料 --add-drop-table 在每個create 語句之前增加一個drop table

[[email protected] bin]# ./mysqldump -uroot -p -d --add-drop-table david > /tmp/david/david_db.sqlEnter password: [[email protected] bin]# 

8.4 匯入資料庫

a. 常用source 命令

進入mysql 資料庫控制台

# ./mysql -uroot -p

mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema | | mysql              | +--------------------+2 rows in set (0.00 sec)mysql> create database sandy;Query OK, 1 row affected (0.00 sec)mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema | | mysql              | | sandy              | +--------------------+3 rows in set (0.00 sec)mysql> use sandyDatabase changedmysql> source /tmp/david/david.sqlQuery OK, 0 rows affected (0.00 sec)...Query OK, 2 rows affected (0.00 sec)Records: 2  Duplicates: 0  Warnings: 0...Query OK, 3 rows affected (0.00 sec)Records: 3  Duplicates: 0  Warnings: 0...Query OK, 0 rows affected (0.00 sec)mysql> show tables;+-----------------+| Tables_in_sandy |+-----------------+| emp             | | myclass         | | myclass4        | +-----------------+3 rows in set (0.00 sec)mysql> select * from emp;+------+-------+| id   | name  |+------+-------+|    1 | david | |    2 | sandy | +------+-------+2 rows in set (0.00 sec)mysql> 

b. 使用mysql 命令

先建立要匯入的資料庫 renee。

mysql> create database renee;Query OK, 1 row affected (0.00 sec)mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema | | mysql              | | renee              | | sandy              | +--------------------+4 rows in set (0.00 sec)mysql> 

匯入資料

[[email protected] bin]# ./mysql -uroot -p -D renee < /tmp/david/david_myclass.sql Enter password: [[email protected] bin]#

查看資料

mysql> use renee;Database changedmysql> show tables;+-----------------+| Tables_in_renee |+-----------------+| myclass         | +-----------------+1 row in set (0.00 sec)mysql> select * from myclass;+----+-------+-----+--------+----------+| id | name  | sex | degree | passtest |+----+-------+-----+--------+----------+|  1 | david |   1 |  89.99 |       78 | |  2 | sandy |   0 | 100.00 |       90 | |  3 | nancy |   0 |  90.34 |        0 | +----+-------+-----+--------+----------+3 rows in set (0.00 sec)mysql> 

更多匯入匯出命令,請自行Google+Baidu。

 

原地址:http://www.cnblogs.com/mchina/archive/2013/04/19/2571244.html

mysql常用命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.