串連MySQL資料庫的兩種方式介紹

來源:互聯網
上載者:User

串連MySQL操作是串連進程和MySQL資料庫執行個體進行通訊。從開發的角度來說,本質上是進程通訊,常用的進程通訊方式有管道、具名管道、命名字、TCP/IP通訊端、Unix網域名稱通訊端

TCP/IP串連:

TCP/IP通訊端串連方式是MySQL在任何平台都提供的一種串連方式,也是網路中使用最多的一種方式。這種方式在TCP/IP串連上建立一個基於網路的串連請求,一般情況下用戶端在一台伺服器上,而MySQL執行個體在另外一台伺服器上,這兩台機器通過TCP/IP網路連接

  1. mysql> use mysql;  
  2.   
  3. Readingtable information for completion of table and column names  
  4.   
  5. Youcan turn off this feature to get a quicker startup with -A  
  6.   
  7.   
  8. Databasechanged  
  9.   
  10. mysql>select user,host,password from user;  
  11.   
  12. +------+-------------------+-------------------------------------------+   
  13.   
  14. |user | host              | password                                  |  
  15.   
  16. +------+-------------------+-------------------------------------------+   
  17.   
  18. |root | localhost         |*23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |  
  19.   
  20. |root | server.sxkeji.com |                                           |  
  21.   
  22. |root | 127.0.0.1         |                                           |  
  23.   
  24. |      | localhost         |                                           |  
  25.   
  26. |      | server.sxkeji.com |                                           |  
  27.   
  28. | wu  | %                 |*00A51F3F48415C7D4E8908980D443C29C69B60C9|  
  29.   
  30. +------+-------------------+-------------------------------------------+   
  31.   
  32. 6rows in set (0.01 sec)  
  33.   
  34.    
  35.   
  36. mysql>  

首先遠端連線的用戶端串連的使用者有許可權才可以被串連,我們查看到了wu這個使用者允許任何機器遠端連線

  1. # mysql -h192.168.0.110 -uwu -p  
  2.   
  3. Enterpassword:  
  4.   
  5. Welcometo the MySQL monitor.  Commands end withor \g.  
  6.   
  7. YourMySQL connection id is 16  
  8.   
  9. Serverversion: 5.1.52 Source distribution  
  10.   
  11.    
  12.   
  13. Copyright(c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.  
  14.   
  15. Thissoftware comes with ABSOLUTELY NO WARRANTY. This is free software,  
  16.   
  17. andyou are welcome to modify and redistribute it under the GPL v2 license  
  18.   
  19.    
  20.   
  21. Type'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  22.   
  23.   
  24. mysql>  

Unix域通訊端串連:

在Linux和Unix環境下,還可以使用Unix域通訊端串連。Unix域通訊端其實不是網路通訊協定,所以只能使用MySQL用戶端和資料庫執行個體在同一台伺服器上的情況下使用。可以在設定檔中指定通訊端檔案路徑,如-socket=/tmp/mysql.sock。當資料庫啟動之後使用如下方法尋找通訊端

  1. mysql>show variables like 'socket'\G  
  2.   
  3. ***************************1. row ***************************  
  4.   
  5. Variable_name:socket  
  6.   
  7.         Value: /var/lib/mysql/mysql.sock  
  8.   
  9. 1row in set (0.00 sec)  
  10.   
  11. mysql>  

然後就可以通過通訊端的方式串連了

  1. # mysql -uwu -S /var/lib/mysql/mysql.sock  
  2.   
  3. Welcometo the MySQL monitor.  Commands end withor \g.  
  4.   
  5. YourMySQL connection id is 18  
  6.   
  7. Serverversion: 5.1.52 Source distribution  
  8.   
  9.   
  10. Copyright(c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.  
  11.   
  12. Thissoftware comes with ABSOLUTELY NO WARRANTY. This is free software,  
  13.   
  14. andyou are welcome to modify and redistribute it under the GPL v2 license  
  15.   
  16.   
  17. Type'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  18.   
  19.    
  20. 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.