简体   繁体   中英

Laravel remote DB connection issue on MAMP Pro OSX

I have an existing project on Laravel. I want to make a connection to remote DB server so our team can test with the same set of data .

.ENV File

APP_NAME=Laravel
APP_ENV=live
APP_KEY=base64:FE9rhqH/+5UHVJCfoYS6xHNsU9U1DJaPkUDmRSv6JxI=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://laravel.test/

DB_CONNECTION=mysql
DB_HOST=104.1XX.XX.XX
DB_PORT=3306
DB_DATABASE=My_db_name
DB_USERNAME=db_username
DB_PASSWORD=db_password
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_LIFETIME=120
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=

database.php

'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '104.1XX.XX.XX'),
        'port' => env('DB_PORT', ''),
        'database' => env('DB_DATABASE', 'My_db_name'),
        'username' => env('DB_USERNAME', 'db_usernamel'),
        'password' => env('DB_PASSWORD', 'db_password'),
        'unix_socket' => env('DB_SOCKET', '/Applications/MAMP/tmp/mysql/mysql.sock'),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

Error shows it is trying to connect to localhost

Illuminate\Database\QueryException thrown with message "SQLSTATE[HY000] [1045] Access denied for user 'db_username'@'localhost' (using password: YES) (SQL: select `users`.* from `followers` inner join `users` on `followers`.`follower_id` = `users`.`id` inner join `countries` on `users`.`country_id` = `countries`.`id` where (`followers`.`user_id` is null))"

Expected Behaviour: should connect to remote DB

/Applications/MAMP/Library/bin/mysql -u db_username -h 104.1XX.XX.XX -p

Able to make the connection using the above string in terminal

Laravel Framework 5.8.37

PHP 7.3.11 (cli)

Mamp Pro - 5.7

Any help will be appreciated .

As per your error its not the problem of Laravel project. Its the problem of your database. You cannot access the database outside of the server where it is deployed.

Possible problems are as follows:

1. Database user configured to access only for localhost and not outside

2. Proper database credentials

For 1 you can use the following

% -> stands for anywhere

Basically with the below your giving all the access to the user USERNAME to access from anywhere in the world

GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'@'%' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION; 
FLUSH PRIVILEGES;

Along with the above make sure to do the following

change bind-address in file /etc/mysql/mysql.conf.d/mysqld.conf

Basically it will be 127.0.0.1 or your server ip-address change it to 0.0.0.0

Hope it works for you. Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM