Skip to content
Advertisement

Laravel (Lumen) connect to database on another server on same network

I’ve got a Laravel project on a server, and a Laravel Lumen 8 project on another server, they’re both virtual machines and are clones of one another so have the same hardware and OS.

I have a domain, which for the purposes of this Stackoverflow we’ll call foo.com, it goes through Cloudflare, both servers are ipv6 servers and when pinging each other via ssh they can see each other just fine.

The problem I have is with connecting to the MySQL (Maria DB) database from my Lumen project on the other server.

I’ve tried using:

  • The domain of the server where the DB exists
  • The ipv6 domain
  • The VM’s local ipv4 address since both servers exist on the same network this is how they can see each other

I’m testing the connection using Tinker and running:

JavaScript

And I’m unable to connect for some reason, these are the combinations and errors I’ve encountered and would like to know what I’m missing:

Attempt 1

JavaScript

PDOException with message ‘SQLSTATE[HY000] [2002] No route to host

Attempt 2

JavaScript

PDOException with message ‘SQLSTATE[HY000] [2002] Invalid argument’

Attempt 3

JavaScript

PDOException with message ‘SQLSTATE[HY000] [2002] Connection timed out’

How can I get my Lumen project to connect to my Laravel database on another server, I’m pretty sure I’ve tried every combination here.

Advertisement

Answer

To connect MySQL Database on separate server, We have to check the followings:

  1. Two servers can reach via ping or ssh.
  2. MySQL can listen on IP of the dedicated VM or server.
  3. Laravel or Lumen can access the IP address of the server.

In this answer, lets assume the network is 192.168.1.0/24. Lumen Server is Server A with 192.168.1.1. MySQL server is is Server B with 192.168.1.2.

In My configuration, I check Two servers can reach via ping using:

ping -c 4 192.168.1.2

the server must respond like.

JavaScript

For IPv6, use ping 6

ping6 -c 4 abcd:abcd:abcd:abcd:abcd:abcd:abcd:abcd

Result similar to IPv4,

JavaScript

if the result is other than this, please make sure you have connectivity or the same network. Numbers will be changed according to real result. If failed, try ssh by using:

JavaScript

If your server B is not accessible both via ssh or ping, there is some problem with your server connectivity.

  1. If these twos can ping, you can make sure your MySQL Server is listening to IP address of the server. You can check using:

#IPv4

JavaScript

#IPv6

JavaScript

The Result will show like:

JavaScript

Check the Session

JavaScript

if it is listen, MySQL is listening to port. Otherwise, please change the following in mysql config. Mine is /etc/mysql/my.cnf.

JavaScript

CAUTION: editing Configs requires sudo or root permissions

After changing and restarting server, please check with nmap again to make sure your DB Server starts listening. PLEASE MAKE SURE FIREWALL IS OFF FOR TESTING.

To Disable Temporary, sudo service firewalld stop and try again. Please refer to your distro firewall service. Both Lumen and MySQL Servers should disable for testing reason. NOT RECOMMENDED FOR PRODUCTION SERVERS.

If Step 1 and 2 are working well. try to access the server with Laravel Tinker, or mysql client.

JavaScript

if it shows other than socks not found, like access denied, please refer to MySQL or Mariadb Manual.

If it is successful, please set your .env file inside your Laravel/Lumen Project.

JavaScript

Hope this help. Worked in Mariadb.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement