Chito Cheng
Chito Cheng

Reputation: 550

Laravel cannot connect to Azure MySQL

I am working on a Laravel product which has to connect to Azure MySql server. I have the following config in .env file:

/.ENV

DB_CONNECTION=mysql
DB_HOST=hostname.mysql.database.azure.com
DB_PORT=3306
DB_DATABASE=dbname
DB_USERNAME=username@hostname
DB_PASSWORD=1234567890

But Laravel cannot connect to the MySQL properly, and give this error

SQLSTATE[HY000] [1045] Access denied for user 'username'@'123.123.123.123' (using password: YES) (SQL: select * from `users`) 

It seems like Laravel has replaced the hostname with server ip in the username. How can I make Laravel not replacing the hostname, and connect to the Azure MySQL properly.

Upvotes: 0

Views: 1607

Answers (2)

John Mellor
John Mellor

Reputation: 2503

Azure requires SSL connections by default. You can turn these off in Azure or use:

'options'   => array(
                \PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false,
                \PDO::MYSQL_ATTR_SSL_KEY => '/certs/client-key.pem',
                \PDO::MYSQL_ATTR_SSL_CERT => '/certs/client-cert.pem',
                \PDO::MYSQL_ATTR_SSL_CA => '/certs/ca.pem',
            ),

Upvotes: 2

Niraj Shah
Niraj Shah

Reputation: 15457

The issue is likely to do with the user permissions on the MySQL server itself. You can try to create a new MySQL User using username@% (which is username with any hostname`.

E.g.

CREATE USER 'newuser'@'%' IDENTIFIED BY 'some-password';
GRANT ALL PRIVILEGES ON your_database . * TO 'newuser'@'%';
FLUSH PRIVILEGES;

Then update your Laravel .env file with the new username and password.

Upvotes: 0

Related Questions