How to Connect to a Remote MySQL Database from Ubuntu Without MySQL Installed Locally?

I’ve primarily worked with a local MySQL database in my Laravel applications. However, I’m now trying to connect to a remote MySQL database for my Laravel app, and I’m encountering the following error: SQLSTATE[HY000] [2002] No route to host.

I have the remote MySQL server’s details:

  • Hostname or IP address.
  • MySQL username.
  • MySQL password.

I am using Ubuntu as my development computer. Could someone guide me to connect my Laravel app to this remote MySQL database? Any help to resolve the error would be greatly appreciated.

Thanks!

The problem mostly occurs because your remote server blocks the access.

  • Check your hostname or IP address
  • Check you have used correct PORT
  • Check the port is not blocked in the firewall

You can test the connection from your command terminal. First, you need MySQL Client in your Ubuntu machine. If you don’t have the MySQL client installed on your Ubuntu machine, you can install it using the following command.

sudo apt-get update
sudo apt-get install mysql-client

To access a remote MySQL server, you’ll typically use the mysql command-line tool with the -h flag to specify the hostname or IP address of the remote server, -u to specify the MySQL user, and -p to prompt for the password.

mysql -h remote-hostname-or-ip -u your-username -p

After running this command, you’ll be prompted to enter the password for your MySQL user. Once you enter the correct password and your remote server allows the connection, you should be connected to the remote MySQL server. You can interact with the remote MySQL database just like you would with a local database.

To exit the MySQL command-line interface, you can simply type:

exit