近日在安裝LAMP環境時訪問本地MySQL數據庫錯誤
創新互聯建站專注于望都網站建設服務及定制,我們擁有豐富的企業做網站經驗。 熱誠為您提供望都營銷型網站建設,望都網站制作、望都網頁設計、望都網站官網定制、微信小程序開發服務,打造望都網絡公司原創品牌,更為您提供望都網站排名全網營銷落地服務。
錯誤如下:
Warning: mysqli::mysqli(): (HY000/2002): Can't connect to local MySQL server
問題描述:
由于將安裝mysql服務器文件位置修改在home目錄下,造成本地訪問問題。
在網上找了很久終于找到解決問題的方法:
1、先確保在本機上使用命令能登陸,如果也出現提示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文件的位置
問題分析(這是我在網上找到的解決方案):
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系統需要將localhost 這個地址進行轉換的文件
解決的方法其他有三個
There are a few ways to solve this problem.
You can just use TCP/IP instead of the Unix socket. You would do this by using 127.0.0.1
instead of localhost
when you connect. The Unix socket might by faster and safer to use, though.
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
.
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、修改連接文件中localhost 為127.0.0.1
2、查看my.cnf文件中sock
文件的位子,并賦值到
php.ini
:文件中的mysqli.default_socket去
3、PHP語言直接多一個參數指向sock文件位置。
分享文章:centos安裝LAMP環境時訪問本地MYSQL數據庫錯誤
鏈接地址:http://m.2m8n56k.cn/article16/jdsgdg.html
成都網站建設公司_創新互聯,為您提供軟件開發、網站制作、云服務器、用戶體驗、做網站、App設計
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯