對於想要從事或愛好mysql相關工作的童鞋們,有必要掌握在命令行下對mysql實現一些簡單的操作。本文從描述了如何登錄到mysql數據庫服務器,如何在mysql提示符下發布命令,創建數據庫,以及執行一些簡單的DML操作。
1、連接到與退出mysql
為了連接mysql數據庫服務器,當調用mysql時,通常需要提供一個MySQL用戶名並且很可能需要一個密碼。如果服務器 運行在登錄服務器之外的其它機器上,還需要指定主機名。聯系管理員以找出進行連接所使用的參數 (即,連接的主機 、用戶名和使用的密碼)。知道正確的參數後,可以按照以下方式進行連接: shell> mysql -h host -u user -p mysql> select version(),current_date; +---------------------------------------+--------------+ | version() | current_date | +---------------------------------------+--------------+ | 5.6.17-enterprise-commercial-advanced | 2014-04-28 | +---------------------------------------+--------------+ 1 row in set (0.03 sec) --在允許匿名登錄到本地服務器的情下可以直接在shell提示符下直接輸入mysql即可實現登錄 mysql> #提示符告訴你mysql准備為你輸入命令。 shell> mysql --輸入分號表示結束命令輸入並執行該命令 --成功地連接後,可以在mysql>提示下輸入QUIT (或\q ,exit)隨時退出 mysql> QUIT Bye --在Unix中,也可以按control-D鍵斷開服務器。
mysql執行命令可分為非交互與交互模式
a) 非交互模式
非交互模式,也叫批模式,也就是將想要運行的命令放在一個文件中,然後告訴mysql從文件讀取它的輸入。
通常用於返回數據量較大,以及批量管理,執行特殊腳本運行的情形。
shell> mysql <query.sql
[root@linux1 ~]# more query.sql
show databases;
use cnfo
select * from tb_tmp;
[root@linux1 ~]# mysql -u root -pmysql <query.sql
Warning: Using a password on the command line interface can be insecure.
Database
information_schema
cnfo
mysql
performance_schema
test
name sex birth
Jack F 2014-04-28
John M 2013-04-28
--也可以使用下面的2種方式來執行批
mysql > source /<dir>/filename
mysql > \./<dir>/finename
--如下面的演示
[root@linux1 ~]# mysql -u root -pmysql
mysql> source query.sql
+--------------------+
| Database |
+--------------------+
| information_schema |
| cnfo |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
+------+------+------------+
| name | sex | birth |
+------+------+------------+
| Jack | F | 2014-04-28 |
| John | M | 2013-04-28 |
+------+------+------------+
2 rows in set (0.00 sec)
也可以在shell模式下直接執行SQL,如下面的方法:
-e or --execution=option
shell>mysql -e "SQL cmd1;SQL cmd2;.."
shell>mysql --execute="SQL cmd1;SQL cmd2;.."
b) 交互模式
交互模式就是直接在mysql提示符下發布命令並執行命令。
如下操作,不區分大小寫,輸入回車後會得到命令執行的結果,即為交互模式。
mysql> SELECT VERSION(), CURRENT_DATE;
mysql> select version(), current_date;
mysql> SeLeCt vErSiOn(), current_DATE;
--簡單計算
mysql> select power(2,3),(5-1)*4;
+------------+---------+
| power(2,3) | (5-1)*4 |
+------------+---------+
| 8 | 16 |
+------------+---------+
1 row in set (0.00 sec)
--分號分割多行
mysql> select version();select current_date;
+---------------------------------------+
| version() |
+---------------------------------------+
| 5.6.17-enterprise-commercial-advanced |
+---------------------------------------+
1 row in set (0.01 sec)
+--------------+
| current_date |
+--------------+
| 2014-04-28 |
+--------------+
1 row in set (0.00 sec)
--換行輸入命令
--注,可以輸入空行
mysql> select user(),
-> current_date;
+----------------+--------------+
| user() | current_date |
+----------------+--------------+
| root@localhost | 2014-04-28 |
+----------------+--------------+
1 row in set (0.00 sec)
--取消執行當前命令
mysql> select current_date()\c
4、mysql常用提示符的含義
5、日常操作
--創建數據庫
mysql> create database cnfo;
Query OK, 1 row affected (0.00 sec)
--切換數據庫
mysql> use cnfo
Database changed
--查看當前數據庫
mysql> select database();
+------------+
| database() |
+------------+
| cnfo |
+------------+
1 row in set (0.00 sec)
--啟動mysql時連接到指定數據庫
[root@linux1 ~]# mysql -u root -p cnfo
Enter password:
mysql> select database();
+------------+
| database() |
+------------+
| cnfo |
+------------+
1 row in set (0.01 sec)
--在當前庫創建表
mysql> create table tb_tmp(name varchar(20),
-> sex char(1),birth date);
Query OK, 0 rows affected (0.09 sec)
--顯示當前庫所有的表
mysql> show tables;
+----------------+
| Tables_in_cnfo |
+----------------+
| tb_tmp |
+----------------+
1 row in set (0.00 sec)
--查看表的定義信息
mysql> desc tb_tmp
-> ;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | date | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.02 sec)
-- Author : Leshami
-- Blog : http://blog.csdn.net/leshami
--為表插入記錄
mysql> insert into tb_tmp values('Jcack','F','20140428');
Query OK, 1 row affected (0.08 sec)
mysql> insert into tb_tmp values('John','M','20130428');
Query OK, 1 row affected (0.02 sec)
--查看表上的記錄
mysql> select * from tb_tmp;
+-------+------+------------+
| name | sex | birth |
+-------+------+------------+
| Jcack | F | 2014-04-28 |
| John | M | 2013-04-28 |
+-------+------+------------+
2 rows in set (0.00 sec)
--更新表上的記錄
mysql> update tb_tmp set name='Jack' where name='Jcack';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
--過濾查詢
mysql> select * from tb_tmp where sex='F';
+------+------+------------+
| name | sex | birth |
+------+------+------------+
| Jack | F | 2014-04-28 |
+------+------+------------+
1 row in set (0.00 sec)