mjnoach
mjnoach

Reputation: 179

Can't connect to mysql server from command prompt [Laravel]

I've updated the .env file with my server database data (not localhost), but I can't connect to it via command prompt.

DB_HOST=vps.sitename.com
DB_DATABASE=name_of_my_database
DB_USERNAME=myusername
DB_PASSWORD=mypassword

When I try to access that database from the command line like so:

mysql -umyusername -pmypassword

I'm getting the following response:

Access denied for user 'mysuername'@'localhost' (using password: YES)

Do you know any solutions for that problem?

Upvotes: 2

Views: 1085

Answers (2)

codewizard
codewizard

Reputation: 406

Adding the credentials in .env file with your server does not mean you can access the DB if localhost is not added/granted access in MySQL, as the mysql command doesn't even use the .env file in the first place.

You would need to GRANT ACCESS to the 'mysuername'@'localhost' in MySQL like this:

GRANT ALL ON *.* TO 'myusername'@'localhost';

then don't forget to flush privileges like:

FLUSH PRIVILEGES

This assumes that you created the 'myusername'@'localhost';

Otherwise, you need to do the following:

CREATE USER 'myusername'@'localhost' IDENTIFIED BY 'mypass';
GRANT ALL ON *.* TO 'myusername'@'localhost';
FLUSH PRIVILEGES

And then don't forget to bind your address in my.cnf in order to access remotely, if applicable, such as:

#Replace xxx with your IP Address 
bind-address        = xxx.xxx.xxx.xxx

This should do it.

Hope everything works out for you.

Upvotes: 0

Bogdan
Bogdan

Reputation: 44566

The mysql command does not use the .env file, only Laravel does. If you don't pass the hostname to the command, it will try to connect to localhost as you can see from the error message. You can pass that to the command via the -h or --host option like so:

mysql -h vps.sitename.com -umyusername -pmypassword

You also need to make sure that the MySQL server on vps.sitename.com allows remote connections and that the user myusername is allowed to connect from your IP address.

Upvotes: 1

Related Questions