centos安裝LAMP環(huán)境時訪問本地MYSQL數(shù)據(jù)庫錯誤-創(chuàng)新互聯(lián)

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

成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比溫江網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式溫江網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋溫江地區(qū)。費用合理售后完善,10余年實體公司更值得信賴。

錯誤如下:

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

問題描述:

由于將安裝mysql服務(wù)器文件位置修改在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 這個地址進行轉(zhuǎn)換的文件

解決的方法其他有三個

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文件位置。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

名稱欄目:centos安裝LAMP環(huán)境時訪問本地MYSQL數(shù)據(jù)庫錯誤-創(chuàng)新互聯(lián)
網(wǎng)頁網(wǎng)址:http://muchs.cn/article10/psogo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站內(nèi)鏈、網(wǎng)站設(shè)計、定制網(wǎng)站、網(wǎng)站維護、營銷型網(wǎng)站建設(shè)

廣告

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

成都網(wǎng)站建設(shè)公司