chuplu
chuplu

Reputation: 164

Connecting to remote mysql works in terminal, not via PHP PDO script

I am facing a weird problem here we have a server A where the app files are stored and B server with database

Tried to connect via command prompt from server A to B using the command

mysql -h xx.xx.xx.xx -u root -p password - and it worked 

NOw i tried to create a php script in server A to connect to server B the command is

$this->db=new PDO('mysql:host=xx.xx.xx.xx;dbname=databasename','root','password');

Connection failed: SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'xx.xx.xx.xx' (13) Fatal error: Uncaught exception 'Exception' with message 'SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'xx.xx.xx.xx' (13)'

Unable to find a solution on this.

Can any help on this?

thank you

Upvotes: 0

Views: 3591

Answers (3)

chuplu
chuplu

Reputation: 164

I got it working by running a command in the database server :)

setsebool httpd_can_network_connect_db=1

thanks for the replies yycdev

Upvotes: 4

don magug
don magug

Reputation: 322

I don't know much about that, but try to place the port in back of the script:

$this->db=new PDO('mysql:host=xx.xx.xx.xx;dbname=databasename','root','password',3306);

Upvotes: 0

devfunkd
devfunkd

Reputation: 3234

Try specifying the port in your connection string and ensure the database server is set to allow remote connections and the port is open on your firewall (both of these I suspect are already done as you are able to connect via the terminal but it never hurts to verify and check things).

Change your PDO connection and add the port=3306 or if you're using MAMP use port 8889

$this->db=new PDO('mysql:host=xx.xx.xx.xx;port=3306;dbname=databasename','root','password');

Another thing to check is if – SELinux is blocking network connections. Login as root and run

setsebool -P httpd_can_network_connect=1

Upvotes: 1

Related Questions