Cant connect to mysql server on mysql errno name or service not known

This tutorial is intended to explain the necessary steps for solving the “ERROR 2003 (HY000): Can’t connect to MySQL server on ‘127.0.0.1’ (111)” which might occur when you try to access the MySQL database server.

Before moving any further, if you are a Linux user who is new to MySQL/MariaDB, then you may consider learning MySQL / MariaDB for Beginners – Part 1 and 20 MySQL (Mysqladmin) Commands for Database Administration in Linux as well.

On the other hand, if you are already a intermediate/experienced MySQL user, you can master these 15 Useful MySQL/MariaDB Performance Tuning and Optimization Tips.

Note: For this tutorial, it is assumed that you have already installed mysql database server.

Coming back to the point of focus, what are some of the possible causes of this error?

  1. Network failure especially if mysql database server is running on remote host.
  2. No mysql server is running on the mentioned host.
  3. Firewall blocking TCP-IP connection or other related reasons.

Below are the essential steps to deal with it.

1. If database server is on a remote machine, then try to test the client-server connectivity using ping command, for instance:

$ ping server_ip_address
Cant connect to mysql server on mysql errno name or service not known
Ping Host Machine

Once there is connectivity, use the ps command below which shows information about a selection of the active processes, together with a pipe and grep command, to check that the mysql daemon is running on your system.

$ ps -Af | grep mysqld

where the option:

  1. -A – activates selection of all processes
  2. -f – enables full format listing
Cant connect to mysql server on mysql errno name or service not known
Check MySQL Process

If there is no output from the previous command, start the mysql service as follows:

$ sudo systemctl start mysql.service
$ sudo systemctl start mariadb.service
OR
# sudo /etc/init.d/mysqld start

After starting mysql service, try to access the database server:

$ mysql -u username -p -h host_address  

2. If you still get the same error, then determine the port (default is 3306) on which the mysql daemon is listening by running the netstat command.

$ netstat -lnp | grep mysql

where the options:

  1. -l – displays listening ports
  2. -n – enables display of numerical addresses
  3. -p – shows PID and name of the program owning the socket
Cant connect to mysql server on mysql errno name or service not known
Find MySQL Port Number

Therefore use the -P option to specify the port you see from the output above while accessing the database server:

$ mysql -u username -p -h host_address -P port

3. If all the above commands run successfully, but you still see the error, open the mysql config file.

$ vi /etc/mysql/my.cnf
OR
$ vi /etc/mysql/mysql.conf.d/mysqld.cnf 

Look for the line below and comment it out using the # character:

bind-address = 127.0.0.1 

Save the file and exit, afterwards restart the mysql service like so:

$ sudo systemctl start mysql.service
$ sudo systemctl start mariadb.service
OR
# sudo /etc/init.d/mysqld start

However, if you have firewallD or Iptables running try to review firewall services and open the mysql port, assuming it is firewall blocking TCP-IP connections to your mysql server.

That’s all! Do you know other methods or have suggestions for solving the MySQL connection error above? Let us know by dropping a comment via the feedback form below.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Cant connect to mysql server on mysql errno name or service not known

We are thankful for your never ending support.

A big part of the content on the Internet is stored on databases for which MySQL is a popular choice. But what to do when suddenly your dynamic content doesn’t load, or when returning to your website you are greeted by a nearly empty white page with message “Error establishing a database connection.” This guide is aimed at helping with troubleshooting MySQL databases on cloud servers, and by following the steps listed here you’ll hopefully be able to restore your database functionality.

Try UpCloud for free! Deploy a server in just 45 seconds

Check that the service is running

If your website cannot connect to your database, it is possible the service is simply not listening. Check your MySQL state, on Ubuntu and Debian systems this can be done with the following command.

sudo service mysql status

CentOS and other Red Hat variants use MySQL as well, but it is named MariaDB instead, so use this command instead.

sudo service mariadb status

The output from the status check on CentOS and Debian will show something along the lines of this example from CentOS below, Debian output would be nearly identical with the exception of different service name.

mariadb.service - MariaDB database server
    Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled)
    Active: active (running) since Wed 2015-08-05 11:53:38 EEST; 3h 23min ago
  Main PID: 2451 (mysqld_safe)
    CGroup: /system.slice/mariadb.service
            ├─2451 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
            └─2609 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql...

The printout is rather verbose, but the important part is usually coloured to stand out better. In green ‘active (running)’ means the service should be running normally if instead, it says ‘active (exited)’ or ‘inactive (dead)’ the process has been stopped or killed.

Ubuntu condenses the same information to a one-liner like an example output underneath.

mysql start/running, process 5897

If your service status says something other than ‘running’, try to restart the process using the same service command as before but with ‘restart’ instead of ‘status’.

sudo service mysql restart

sudo service mariadb restart

Should the database service restart without encountering errors, you can try to connect to it using the command below. Enter the root password when prompted.

mysql -u root -p

If you are greeted with “Welcome to the MySQL/MariaDB monitor” the connection was successful and the database service is running. If instead, you get an error like this example below you probably mistyped the password for root user. Try again, or if you are not sure about the root password, log in with another user account you have access to by just replacing root with the other username.

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

If you have your database set up on a separate server from your web host, make sure the two servers can reach each other. You can test the database connection from your web server with the command underneath using the correct username for your installation.

mysql -u <user name> -p -h <database server private IP>

Check the configuration

When MySQL is running but your website still doesn’t load as it should, or if when attempting to connect to your database manually you get an error message like the one below, you should take a look at the service configuration.

ERROR 2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111)

On Debian and Ubuntu servers the configuration file for MySQL is usually saved at /etc/mysql/. It’s also possible to have user-specific settings stored at /home/<user>/.my.cnf, which would override the global configurations. Check if any user level overrides have been set. It is commonly advised to have separate usernames for different web applications, so check at least those relevant to your page loading issues. You can open the global configuration file with first of the following two commands below, and the user-specific with the latter by replacing the <user> with a database username.

sudo nano /etc/mysql/my.cnf

sudo nano /home/<user>/.my.cnf

By scrolling down past [client] and [mysqld_safe] settings you’ll find something like the example here.

[mysqld]
#
# * Basic Settings
#
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address            = 127.0.0.1

With CentOS and other Red Hats, the primary configuration file is stored at the slightly different location, open it for inspection with

sudo vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

The lines here to pay close attention to are ‘socket’, ‘datadir’ and ‘bind-address’. The parameters in the example above are in their default values, and in most cases, your configuration would look the same. Make sure the settings point to the correct directories so that MySQL can actually find the required files. The easiest way to check the ‘datadir’ is to use this command below

sudo ls -l /var/lib/mysql/

The output will list all files in that directory, it should contain at least the following plus any databases you have created.

drwx------ 2 mysql root      4096 Aug  5 12:23 mysql
drwx------ 2 mysql mysql     4096 Aug  5 12:29 performance_schema

If the data directory or socket has been moved and MySQL doesn’t know where they are, fix the configuration file to point to the correct directories. You can search for the folders with the following command.

sudo find / -name performance_schema  && sudo find / -name mysql.sock

The third parameter you’ll need to check is the bind-address, this is only really relevant if your database needs to be accessed remotely. In Debian and Ubuntu installations the bind is by default set to the loopback address, which prevents database calls from outside the localhost. CentOS doesn’t have the same parameter unless manually set. For any setup where your web service is on a different server to the database, this bind-address should be set to the server’s own private IP.

Check the error logs

If the configuration seems correct and the service is running, but your website still doesn’t load as it should, try checking the logs for any hints to as what might be the cause.

Debian and Ubuntu servers store error logs to /var/log/mysql/error.log. You can read through the logs with ‘less’, but this might not be very convenient as the log includes more than just critical errors. Instead, search the logs using ‘grep’.

sudo grep -i error /var/log/mysql/error.log

Should you not be able to find anything within the most recent logs, check the archived ones as well. To do this, use ‘zgrep’ with otherwise the same command as regular ‘grep’

sudo zgrep -i error /var/log/mysql/error.log.1.gz

Since the database under CentOS is named MariaDB instead of MySQL, the logs are also saved under a different name. You can search the logs with the following command.

sudo grep -i error /var/log/mariadb/mariadb.log

Debian systems also report MySQL events to /var/log/syslog, to filter out everything else, use ‘grep’ with two keywords separated by .* to express ‘and’ like in the command below.

sudo grep -i -E 'mysql.*error' /var/log/syslog

If you are having difficulties finding anything helpful, try different keywords such as ‘start’ to see when the service was last restarted, or ‘failed’ to find any less critical problems that might not be reported as errors.

Ask for help

Optimistically by now your database should be up and running again, but in case you’ve encountered a more persistent error, feel free to ask for help. Contact our support team and try to explain the problem to the best of your ability, also include the steps you’ve taken with their results while troubleshooting the issue. This will help the team in helping you with the problem.

Why is MySQL not connecting to server?

Here are some reasons the Can't connect to local MySQL server error might occur: mysqld is not running on the local host. Check your operating system's process list to ensure the mysqld process is present. You're running a MySQL server on Windows with many TCP/IP connections to it.

How do I fix MySQL host is not allowed to connect to this server?

To fix this, add a user that allows your host. To allow any host, use the wildcard symbol %. You can add a user from the command line or with a UI client (such as MySQL Workbench or phpMyAdmin).

How do I fix MySQL server error?

Some permanent solutions are:.
Determine what is wrong with your DNS server and fix it..
Specify IP addresses rather than host names in the MySQL grant tables..
Put an entry for the client machine name in /etc/hosts on Unix or \windows\hosts on Windows..
Start mysqld with the skip_name_resolve system variable enabled..

How do I find my MySQL server name?

By default your MySQL host is localhost. You can find it in Hosting → Manage → MySQL databases section: If you are setting up a Remote MySQL connection, the host will be different and you will need to check it in the hPanel.