백엔드 개발 PHP 튜토리얼 php没法使用localhost连接mysql服务器

php没法使用localhost连接mysql服务器

Jun 13, 2016 am 10:46 AM
client file mysql socket the

php无法使用localhost连接mysql服务器
在redhat下安装了mysql,在mysql控制台下,可以使用localhost连接,但是在php中连接时,却不行,而使用127.0.0.1能连上,查看了hosts文件,发现有127.0.0.1和localhost的对应

哪位大侠知道是怎么回事

------解决方案--------------------
是不是你在建站点的时候设置了站点为http://127.0.0.1了呢?
------解决方案--------------------
看一看你的mysql是怎么配置的,
检查mysql启动后/usr/local/mysql/var/mysql.sock是否存在。
------解决方案--------------------
Description
A frequent error message received when using the mysql command line utility is: Can 't connect to local MySQL server through socket '/tmp/mysql.sock ' While this error message can be frustrating, the solution is simple.

Directions
When connecting to a MySQL server located on the local system, the mysql client connects thorugh a local file called a socket instead of connecting to the localhost loopback address 127.0.0.1. For the mysql client, the default location of this socket file is /tmp/mysql.sock. However, for a variety of reasons, many MySQL installations place this socket file somewhere else like /var/lib/mysql/mysql.sock.

While it is possible to make this work by specifying the socket file directly in the mysql client command

mysql --socket=/var/lib/mysql/mysql.sock ...


it is painful to type this in every time. If you must do so this way (because you don 't have permissions to the file in the solution below), you could create an alias in your shell to make this work

(like alias mysql= "mysql --socket=/var/lib/mysql/mysql.sock " depending on your shell).

To make your life easier, you can make a simple change to the MySQL configuration file /etc/my.cnf that will permanently set the socket file used by the mysql client. After making a backup copy of /etc/my.cnf, open it in your favorite editor. The file is divided into sections such as

[mysqld]
datadir=/usr/local/mysql/data
socket=/var/lib/mysql/mysql.sock

[mysql.server]
user=mysql
basedir=/usr/local/mysql

If there is not currently a section called [client], add one at the bottom of the file and copy the socket= line under the [mysqld] section such as:

[client]
socket=/var/lib/mysql/mysql.sock

If there is already a [client] section in the my.cnf file, add or edit the socket line as appropriate. You won 't need to restart your server or any other processes. Subsequent uses of the mysql client will use the proper socket file.

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

뜨거운 기사 태그

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

PHP의 빅데이터 구조 처리 능력 PHP의 빅데이터 구조 처리 능력 May 08, 2024 am 10:24 AM

PHP의 빅데이터 구조 처리 능력

PHP에서 MySQL 쿼리 성능을 최적화하는 방법은 무엇입니까? PHP에서 MySQL 쿼리 성능을 최적화하는 방법은 무엇입니까? Jun 03, 2024 pm 08:11 PM

PHP에서 MySQL 쿼리 성능을 최적화하는 방법은 무엇입니까?

PHP에서 MySQL 백업 및 복원을 사용하는 방법은 무엇입니까? PHP에서 MySQL 백업 및 복원을 사용하는 방법은 무엇입니까? Jun 03, 2024 pm 12:19 PM

PHP에서 MySQL 백업 및 복원을 사용하는 방법은 무엇입니까?

PHP를 사용하여 MySQL 테이블에 데이터를 삽입하는 방법은 무엇입니까? PHP를 사용하여 MySQL 테이블에 데이터를 삽입하는 방법은 무엇입니까? Jun 02, 2024 pm 02:26 PM

PHP를 사용하여 MySQL 테이블에 데이터를 삽입하는 방법은 무엇입니까?

데이터베이스에서 Java 열거 유형의 애플리케이션 시나리오는 무엇입니까? 데이터베이스에서 Java 열거 유형의 애플리케이션 시나리오는 무엇입니까? May 05, 2024 am 09:06 AM

데이터베이스에서 Java 열거 유형의 애플리케이션 시나리오는 무엇입니까?

MySQL 8.4에서 mysql_native_password가 로드되지 않음 오류를 수정하는 방법 MySQL 8.4에서 mysql_native_password가 로드되지 않음 오류를 수정하는 방법 Dec 09, 2024 am 11:42 AM

MySQL 8.4에서 mysql_native_password가 로드되지 않음 오류를 수정하는 방법

PHP에서 MySQL 저장 프로시저를 사용하는 방법은 무엇입니까? PHP에서 MySQL 저장 프로시저를 사용하는 방법은 무엇입니까? Jun 02, 2024 pm 02:13 PM

PHP에서 MySQL 저장 프로시저를 사용하는 방법은 무엇입니까?

PHP를 사용하여 MySQL 테이블을 만드는 방법은 무엇입니까? PHP를 사용하여 MySQL 테이블을 만드는 방법은 무엇입니까? Jun 04, 2024 pm 01:57 PM

PHP를 사용하여 MySQL 테이블을 만드는 방법은 무엇입니까?

See all articles