mysql(MySQL客戶端連接工具)


在MySQL提供的工具中,DBA使用最頻繁的莫過於mysql。這里的mysql不是指MySQL服務,也不是mysql數據庫,而是連接數據庫的客戶端工具。類似於Oracle的sqlplus。

語法: 
mysql [options][database]

options是mysql的可用選項,一次可以寫一個或者多個,甚至可以不寫。database表示連接的數據庫,一次只能寫一個或者不寫,如果不寫,在登錄數據庫后還需要使用use dbname選擇數據庫。

mysql的選項通常有兩種表達方式:

  1. -+選項單詞的縮寫+選項值**;
  2. --+選項的完整單詞+=+選項的實際值;
例如:
mysql -uroot -ppassword 
mysql --user=root --password=password 

1.連接選項

-u,--user=name 指定連接的用戶名 
-p,--password=password 指定連接密碼 
-h,--host=name 指定服務器IP或域名 
-P,--port=3308 指定連接端口

在默認情況下,如果這些選項都不寫,mysql將會使用'用戶'@'localhost'和空密碼連接本機的3306端口。空用戶會在mysql安裝完畢后自動生成,這也就是僅使用mysql命令就能連到數據庫的原因。

如果客戶端和服務器在用一台機器上,通常不需要指定-h選項,否則需要指定mysql服務所在的IP或主機名。如果不指定端口,默認連接到3306端口。示例如下: 

# mysql -h 10.10.200.202 -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 75520
Server version: 5.6.28 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> 

2.指定客戶端字符集

--default-character-set=charset-name為服務器的字符集選項。該選項可以配置在my.cnf的[mysqld]組中,同樣也可以作為客戶端字符集選項,也可以配置在[mysql]組中。在使用mysql命令登錄數據庫時,使用--default-character-set選項可以指定客戶端的字符集。例如,如果未使用--default-character-set選項登錄數據庫時:

# mysql -h 10.10.200.202 -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 75520
Server version: 5.6.28 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show variables like 'chara%';
+--------------------------+-----------------------------------------+
| Variable_name            | Value                                   |
+--------------------------+-----------------------------------------+
| character_set_client     | latin1                                  |
| character_set_connection | latin1                                  |
| character_set_database   | utf8                                    |
| character_set_filesystem | binary                                  |
| character_set_results    | latin1                                  |
| character_set_server     | utf8                                    |
| character_set_system     | utf8                                    |
| character_sets_dir       | /usr/local/mysql-5.6.28/share/charsets/ |
+--------------------------+-----------------------------------------+

當使用--default-character-set選項登錄數據庫時:

# mysql -h 10.10.200.202 -uroot -p --default-character-set=utf8
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 75542
Server version: 5.6.28 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show variables like 'chara%';
+--------------------------+-----------------------------------------+
| Variable_name            | Value                                   |
+--------------------------+-----------------------------------------+
| character_set_client     | utf8                                    |
| character_set_connection | utf8                                    |
| character_set_database   | utf8                                    |
| character_set_filesystem | binary                                  |
| character_set_results    | utf8                                    |
| character_set_server     | utf8                                    |
| character_set_system     | utf8                                    |
| character_sets_dir       | /usr/local/mysql-5.6.28/share/charsets/ |
+--------------------------+-----------------------------------------+
8 rows in set (0.01 sec)

3.執行選項

-e,--execute=name 執行sql語句並退出 
此選項可以直接在客戶端執行SQL語句,而不用連接到MySQL數據庫后再執行,對於一些腳本的執行,使用此法較為便利。可以使用此種方法連續執行多條SQL語句,語句之間用分號(;)分隔例如:

# mysql -uroot -p mysql -e "select host,user from user"
Enter password: 
+--------------------+--------+
| host               | user   |
+--------------------+--------+
| 10.10.200.201      | root   |
| 10.10.200.201      | zabbix |
| 127.0.0.1          | root   |
| ::1                | root   |
| localhost          |        |
| localhost          | root   |
| localhost          | zabbix |
| tcxx-ops-mysql-202 |        |
| tcxx-ops-mysql-202 | root   |
+--------------------+--------+

4.格式化選項

-E,--vertical 將輸出按字段順序垂直顯示 
-s,--silent 去掉SQL輸出結果中的線條框

# mysql -uroot -p mysql -e "select host,user from user" -E
Enter password: 
*************************** 1. row ***************************
host: 127.0.0.1
user: root
*************************** 2. row ***************************
host: ::1
user: root
*************************** 3. row ***************************
host: localhost
user: 
*************************** 4. row ***************************
host: localhost
user: root
*************************** 5. row ***************************
host: test-server
user: 
*************************** 6. row ***************************
host: test-server
user: root

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM