Connecting to the MySQL Server

孤竹星發表於2015-04-16

客戶端程式連線MySql服務

1. 不指定連線引數

因為沒有指定引數,將使用預設值
語法:shell> mysql
預設主機名為:localhost
[root@root ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.6.24 MySQL Community Server (GPL)


Copyright (c) 2000, 2015, 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. 指定主機名和使用者名稱和密碼

語法:
shell> mysql --host=localhost --user=myname --password=mypass mydb
shell> mysql -h localhost -u myname -pmypass mydb

host:主機名  user:登入的使用者 password:密碼 mydb:訪問的資料庫


[root@qingmeng001 ~]# mysql --host=localhost --user=chenwx --password=chenwx mysql
Warning: Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 29
Server version: 5.6.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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> 

[root@qingmeng001 ~]# mysql -h localhost -u chenwx -pchenwx mysql
Warning: Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 30
Server version: 5.6.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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> 

note:使用-p或 --password指定密碼,在-p或-password後不能有空格

3.避免因安全問題,指定-p或-password選項不直接跟密碼

語法:
shell> mysql --host=localhost --user=myname --password mydb
shell> mysql -h localhost -u myname -p mydb

[root@qingmeng001 ~]# mysql --host=localhost --user=chenwx --password mysql
Enter password:    --輸入密碼,不顯示輸入內容及輸入個數
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 31
Server version: 5.6.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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> 

4.指定IP和協議

語法:
shell> mysql --host=127.0.0.1
shell> mysql --protocol=TCP
--protocol={TCP|SOCKET|PIPE|MEMORY}

在一些unix系統中需要強制指定TCP/IP,否則指定的埠號將唄忽略

5. 配置ipv6

--host=::1.

6. 使用TCP/IP連線到遠端伺服器

語法:
shell> mysql --host=remote.example.com
預設埠:3306

7.指定埠選項--port -P

語法:
shell> mysql --host=remote.example.com --port=13306

8. windows使用named-pipe連線

語法:
--pipe
--protocol=PIPE


--protocol                
Value            Connection Protocol                                                    Permissible Operating  Systems
TCP                 TCP/IPconnection to local or remote server                                All
SOCKET            Unix socket file connection to local server Unix                          only
PIPE                Named-pipe connection to local or remote serverWindows         only
MEMORY          Shared-memory connection to local serverWindows                     only

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

相關文章