MySQL localhost Unable to connect to the database what to do

Source: Internet
Author: User
Tags mysql in socket

If the above method is still not used, you can refer to the following solutions


Problem: A server's PHP program cannot connect to the database through the localhost address, but if set to 127.0.0.1 you can connect normally, and connecting to other database servers is normal. MySQL permissions are set correctly and the database can be connected properly through the MySQL command-line client.

Analysis: This is the case where the typical socket is not set correctly.

There are two ways to connect to a MySQL database: TCP/IP (a generally understood port type) and UNIX sockets (usually called sockets or sock). Most of the time, you can use localhost to represent the local 127.0.0.1, but when the MySQL connection, the two are not mixed, and MySQL in the permissions set in localhost and 127.0.0.1 are set separately. When set to 127.0.0.1, the system connects the database via TCP/IP, and when set to LocalHost, the system connects the database via a socket.

Solution: First look at the local MySQL socket socket file where the view command is:

Mysqld--verbose--help | grep socket

The output shows the location of the socket file, for example: This server displays a

Socket/var/run/mysqld/mysqld.sock

Then modify the PHP configuration file php.ini and corresponding to the good.

Find this item:

Mysql.default_socket =

In general, this one is empty and is changed to:

Mysql.default_socket =/var/run/mysqld/mysqld.sock

Here you should write a query to the file and set it according to your situation. This PHP configuration is modified, if the CLI (command line) or CGI way, immediately effective, if it is fastcgi way, need to restart the fastcgi process.

In Windows system may encounter

Error message: Error 2003 (HY000): Can ' t connect to MySQL server on ' hostxxxxx ' (10061)
Reason: The MYSQLD database service is not started.
Check: In Windows Task Manager, or under Unix/linux Ps-aux | grep MySQL look. Confirm that the service has started.
Processing: Start mysqld Service

Solutions


Method One: net start MySQL (or other service name)

Method Two: Under XP to start MySQL service MySQL installation directory:
"D:mysql" enters the command Line Input window (DOS command input) and runs the "mysqld-u Root" command in the "D:mysqlbin" directory. Writing: D:mysqlbinmysqld-u root (Enter) running a MySQL process in process management, running successfully (you may need to close the command window manually) and then running the "MySQL" command in the "D:mysqlbin" directory: D: Mysqlbin (Enter)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.