Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fatima1's avatar

connect website to other database server

hi all i have server A that contain database. server B that contain website.

i want to connect B to database in A.

how to do that?

0 likes
13 replies
realrandyallen's avatar

Just change the settings in your .env file

DB_CONNECTION=mysql
DB_HOST=
DB_PORT=3306
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=

Assuming you're using mysql, mysql will not allow the root user remote access so you will either need to grant root access to connect remotely or (better) create a new user with necessary permissions and connect to your db server with that user.

zion's avatar

Is the user you created allowed to make a remote connection?

zion's avatar

How did you create the mysql user?

You can update the user with: GRANT ALL ON *.* TO 'USERNAMEHERE'@'%';

This does grant full access to everything, you may want to limit it. Also, instead of %, consider using the ip-address of the application's server.

Have you checked the link provided by @realrandyallen ?

fatima1's avatar

@REALRANDYALLEN - this is for connection from local to server. here i have two online servers in which i want to access database

Dalma's avatar

There are generally three things that come into play when you have a database server on a different host from your website application.

  1. Are you able to access the machine that is running the database. You may have issues where physical or logical firewalls sit between your servers and you need someone with network security knowledge to open these firewalls.
  2. Does you database server allow for remote connections? The previous comments regarding the bind-address is used to configure your mysql software to allow for remote connections.
  3. Does the user account that you are using for the database connection have remote access granted.

Once you have assessed and solved these three levels of potential problems you should be able to make this work.

fatima1's avatar

@DALMA - Thank you for your replay. But I think I’m confused in this step, so I will create an API for all information that I want from database and place them in laravel

Please or to participate in this conversation.