zoukankan      html  css  js  c++  java
  • 转载:连接MySQL数据库的两种方式介绍

     

     mysql有两种连接方式,常用的一般是tcp

    mysql -h(ip) -uroot -pxxx #常用的
    mysql -S /tmp/mysqld.sock

     mysql 采用unix socket连接方式,比用tcp的方式更快,但只适用于mysql和应用同在一台PC上。如果不在同一台pc上,就没有办法连接了。而且我们可以把socket文件放在/dev/shm (内存)。/etc/mysql/my.cnf里面应该可以看到sock的配置条目,

    转载:http://www.ayhacker.net/ayblog/read.php?77

    连接MySQL操作是连接进程和MySQL数据库实例进行通信。从开发的角度来说,本质上是进程通信,常用的进程通信方式有管道、命名管道、命名字、TCP/IP套接字、Unix域名套接字

    TCP/IP连接:

    TCP/IP套接字连接方式是MySQL在任何平台都提供的一种连接方式,也是网络中使用最多的一种方式。这种方式在TCP/IP连接上建立一个基于网络的连接请求,一般情况下客户端在一台服务器上,而MySQL实例在另外一台服务器上,这两台机器通过TCP/IP网络连接

    1. [sql] view plaincopyprint?  
    2. mysql> use mysql;    
    3. Readingtable information for completion of table and column names    
    4. Youcan turn off this feature to get a quicker startup with -A    
    5. Databasechanged    
    6. mysql>select user,host,password from user;    
    7. +------+-------------------+-------------------------------------------+     
    8. |user | host              | password                                  |    
    9. +------+-------------------+-------------------------------------------+     
    10. |root | localhost         |*23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |    
    11. |root | server.sxkeji.com |                                           |    
    12. |root | 127.0.0.1         |                                           |    
    13. |      | localhost         |                                           |    
    14. |      | server.sxkeji.com |                                           |    
    15. | wu  | %                 |*00A51F3F48415C7D4E8908980D443C29C69B60C9|    
    16. +------+-------------------+-------------------------------------------+     
    17. 6rows in set (0.01 sec)    
    18. mysql>   

    首先远程连接的客户端连接的用户有权限才可以被连接,我们查看到了wu这个用户允许任何机器远程连接

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

    //显示连接成功

    Unix域套接字连接:

    在Linux和Unix环境下,还可以使用Unix域套接字连接。Unix域套接字其实不是网络协议,所以只能使用MySQL客户端和数据库实例在同一台服务器上的情况下使用。可以在配置文件中指定套接字文件路径,如-socket=/tmp/mysql.sock。当数据库启动之后使用如下方法查找套接字

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

    然后就可以通过套接字的方式连接了

    1. [sql] view plaincopyprint?  
    2. # mysql -uwu -S /var/lib/mysql/mysql.sock    
    3.     
    4. Welcometo the MySQL monitor.  Commands end with; or \g.    
    5.     
    6. YourMySQL connection id is 18    
    7.     
    8. Serverversion: 5.1.52 Source distribution    
    9.     
    10.     
    11. Copyright(c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.    
    12.     
    13. Thissoftware comes with ABSOLUTELY NO WARRANTY. This is free software,    
    14.     
    15. andyou are welcome to modify and redistribute it under the GPL v2 license    
    16.     
    17.     
    18. Type'help;' or '\h' for help. Type '\c' to clear the current input statement.    
    19.     
    20.      
    21. mysql>  
  • 相关阅读:
    Python PyInstaller安装和使用教程(搬来的,嘻嘻)
    python 注册码永久性方法
    网上搬来的常用工具
    Pycharm控制台窗口怎样可以显示不同程序的运行结果
    php时间:获取上一个月,本月天数,下一个月
    thinkphp5 --接口实例
    浅谈 PHP 与手机 APP 开发
    php 对象的调用和引入
    php 超全局变量(整理)
    php 全局变量和超全局变量
  • 原文地址:https://www.cnblogs.com/persist/p/3014113.html
Copyright © 2011-2022 走看看