centos安裝LAMP環(huán)境時訪問本地MYSQL數(shù)據(jù)庫錯誤

近日在安裝LAMP環(huán)境時訪問本地MySQL數(shù)據(jù)庫錯誤

創(chuàng)新互聯(lián)建站專注于望都網(wǎng)站建設服務及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供望都營銷型網(wǎng)站建設,望都網(wǎng)站制作、望都網(wǎng)頁設計、望都網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務,打造望都網(wǎng)絡公司原創(chuàng)品牌,更為您提供望都網(wǎng)站排名全網(wǎng)營銷落地服務。

錯誤如下:

Warning: mysqli::mysqli(): (HY000/2002): Can't connect to local MySQL server

問題描述:

由于將安裝mysql服務器文件位置修改在home目錄下,造成本地訪問問題。

在網(wǎng)上找了很久終于找到解決問題的方法:

1、先確保在本機上使用命令能登陸,如果也出現(xiàn)提示sock文件位置問題,那么需要修改mysql配置文件(/etc/my.cnf)

增加如下配置:

[mysql]

socket=/home/mysqldata/mysql.sock#這是我的sock文件的位置

2、php使用mysqli無法訪問問題

解決方法:

找到PHP配置文件(/etc/php.ini)找到mysqli.default_socket這個值

修改為mysqli.default_socket =/home/mysqldata/mysql.sock#這是我的sock文件的位置

問題分析(這是我在網(wǎng)上找到的解決方案):

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.

其實就是說UNIX系統(tǒng)需要將localhost 這個地址進行轉換的文件

解決的方法其他有三個

There are a few ways to solve this problem.

  1. You can just use TCP/IP instead of the Unix socket. You would do this by using 127.0.0.1instead of localhost when you connect. The Unix socket might by faster and safer to use, though.

  2. You can change the socket in php.ini: open the MySQL configuration file my.cnf to find where MySQL creates the socket, and set PHP's mysqli.default_socket to that path. On my system it's /var/run/mysqld/mysqld.sock.

  3. Configure the socket directly in the PHP script when opening the connection. For example:

    $db = new MySQLi('localhost', 'kamil', '***', '', 0, 
                                  '/var/run/mysqld/mysqld.sock')

1、修改連接文件中l(wèi)ocalhost 為127.0.0.1

2、查看my.cnf文件中sock文件的位子,并賦值到php.ini:文件中的mysqli.default_socket去

3、PHP語言直接多一個參數(shù)指向sock文件位置。

分享文章:centos安裝LAMP環(huán)境時訪問本地MYSQL數(shù)據(jù)庫錯誤
鏈接地址:http://bm7419.com/article16/jdsgdg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、網(wǎng)站制作云服務器、用戶體驗做網(wǎng)站、App設計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設