Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8968071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:23:16+00:00 2026-06-15T17:23:16+00:00

I get this error when I try to connect to the mysql database using

  • 0

I get this error when I try to connect to the mysql database using php mysqli class. Using following code:

$db = new MySQLi("localhost","kamil","*****");

password is * for security.

I have created user kamil with all privileges on external ip address and localhost. When I run: select user,host from mysql.user it properly displays those two users.

I did some research and used this benchmark: https://stackoverflow.com/a/2183134/1839439 to see what it connects to. As it turns out it is only able to connect to 127.0.0.1 and 127.0.0.1:3306 which is localhost, however when I supply localhost it throws out this error.

My question is why does it only allow me to connect to DB using localhost ip address and not the name or external ip. Do I need a different host if I want to be able to use mysql on website or if I can use 127.0.0.1?

hosts file

127.0.0.1       localhost
::1             localhost ip6-localhost ip6-loopback
fe00::0         ip6-localnet
ff00::0         ip6-mcastprefix
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters

127.0.1.1       raspberrypi

Mysql user table results for this user:

| kamil            | 109.255.177.28 |
| kamil            | localhost      |
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:23:17+00:00Added an answer on June 15, 2026 at 5:23 pm

    When you use just "localhost" the MySQL client library tries to use a Unix domain socket for the connection instead of a TCP/IP connection. The error is telling you that the socket, called MySQL, cannot be used to make the connection, probably because it does not exist (error number 2).

    From the MySQL Documentation:

    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.

    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.1 instead 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')
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Java, I get this error when attempting to connect to a mysql database:
When I try to connect to my database from PHP I get this error:
when i try to view the databases in mysql i get this error: ERROR
Whenever I try installing anything using gem, I get this error - murtaza@murtaza-dev:~$ sudo
I am using the following code, upload.php, to try to have someone upload a
I try to echo my database (hosted on ipage.com) using php and I get
When trying to connect to mysql I always get this error: java.sql.SQLException: No suitable
I get this error when i try to upload an image: OSError at /upload/
I get this error when I try to use time_ago_in_words : Comparison of String
Working with H2 I get this error when I try to write a row

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.