zoukankan      html  css  js  c++  java
  • mysqli使用localhost问题

    <?php
    $mysqli = new mysqli('localhost', 'root', '123456', 'mysql');
    if ($mysqli->connect_error) {
        die('Connect Error (' . $mysqli->connect_errno . ') '
                . $mysqli->connect_error);
    };
    
    echo 'ok';

    • 如果上面连接地址为'localhost'就会报错,如下:

      Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /mnt/www/cglevi/publichtml/mysql.php on line 2 Connect Error (2002) No such file or directory

    • 将'localhost'修改为'127.0.0.1'之后链接正常

    • 将代码:

      $con = mysqli_connect("localhost","root","123456","mysql");

      修改为:

      $con = mysqli_connect("127.0.0.1","root","123456","mysql");


    查看了hosts没有问题,如下:

    127.0.0.1 localhost
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    /etc/hosts (END) 
    

    查看mysql状态,没有问题,如下:

    mysql> status;
    --------------
    mysql  Ver 14.14 Distrib 5.6.10, for Linux (x86_64) using  EditLine wrapper
    
    Connection id:      860
    Current database:   
    Current user:       root@localhost
    SSL:            Not in use
    Current pager:      stdout
    Using outfile:      ''
    Using delimiter:    ;
    Server version:     5.6.10 MySQL Community Server (GPL)
    Protocol version:   10
    Connection:     Localhost via UNIX socket
    Server characterset:    latin1
    Db     characterset:    latin1
    Client characterset:    utf8
    Conn.  characterset:    utf8
    UNIX socket:        /var/lib/mysql/mysql.sock
    Uptime:         13 hours 13 min 50 sec
    
    Threads: 1  Questions: 11900  Slow queries: 0  Opens: 100  Flush tables: 1  Open tables: 80  Queries per second avg: 0.249
    --------------

    开始的回答有点不严谨,估计也没有解决问题,修改了答案:

    问题出现的原因:

    当主机填写为localhost时MySQL会采用 unix domain socket连接,当主机填写为127.0.0.1时MySQL会采用TCP/IP的方式连接。使用Unix socket的连接比TCP/IP的连接更加快速与安全。这是MySQL连接的特性,可以参考官方文档的说明4.2.2. Connecting to the MySQL Server

    On Unix, MySQL programs treat the host name localhost specially, in a way that is 
    likely different from what you expect compared to other network-based programs. 
    For connections to localhost, MySQL programs attempt to connect to the local server 
    by using a Unix socket file. This occurs even if a --port or -P option is given to 
    specify a port number. To ensure that the client makes a TCP/IP connection to the 
    local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP 
    address or name of the local server. You can also specify the connection protocol 
    explicitly, even for localhost, by using the --protocol=TCP option.
    

    这个问题有以下几种解决方法:

    1. 使用TCP/IP代替Unix socket。即在连接的时候将localhost换成127.0.0.1。
    2. 修改MySQL的配置文件my.cnf,指定mysql.socket的位置:

      /var/lib/mysql/mysql.sock (你的mysql.socket路径)。

    3. 直接在php建立连接的时候指定my.socket的位置(官方文档:mysqli_connect)。比如:

      $db = new MySQLi('localhost', 'root', 'root', 'my_db', '3306', '/var/run/mysqld/mysqld.sock')

    其实答案写的不是很严谨,不过既然被采纳了,就多说一点。 通常意义上localhost127.0.0.1是等价的,只是mysql在处理这个名词的问题上有一些不同,是根据不同的地址来采取的不同的通信手段。

    原因呢,我猜大概是为了本地应用能获得更好的性能。而且localhost这个地址在mysql中也不会做匹配。即user@'%'不能匹配到user@'localhost'


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    04:求整数的和与均值
    03:均值
    02:财务管理
    C8-3 三角形还是长方形? (100/100 分数)
    C8-2 圆的周长和面积 (100/100 分数)
    C8-1 复数加减乘除 (100/100 分数)
    C7-3 用类实现a+b (100/100 分数)
    C7-2 多继承 (100/100 分数)
    C7-1 账户类(100/100)
    数组第k小数
  • 原文地址:https://www.cnblogs.com/zsw-1993/p/4879329.html
Copyright © 2011-2022 走看看