My setup: docker (php, mysql, nginx and some other)
My Problem: I can not connect to a remote mysql database within the php docker container
php Example:
try
{
$this->live['pdo'] = new PDO(
'mysql:host='.$_ENV['DB_REMOTE_HOST'].';port='.$_ENV['DB_REMOTE_PORT'].';dbname='.$_ENV['DB_REMOTE_DATABASE'],
$_ENV['DB_REMOTE_USERNAME'],
$_ENV['DB_REMOTE_PASSWORD']
);
$this->live['pdo']->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->live['pdo']->exec("SET NAMES utf8");
$this->live['pdo']->exec("SET CHARACTER SET utf8");
}
catch (PDOException $e)
{
echo $e->getMessage();
exit;
}
my.env
DB_REMOTE_HOST = "5.9.xxx.xx"
DB_REMOTE_PORT = 3306
DB_REMOTE_DATABASE = "somedb"
DB_REMOTE_USERNAME = "someuser"
DB_REMOTE_PASSWORD = "xxxxxx"
part of my docker-compose.yml
php:
build:
context: .
dockerfile: ./php/Dockerfile
args:
ENVIRONMENT: ${ENVIRONMENT}
container_name: php
restart: "unless-stopped"
ports:
- "9000"
- "9006"
- "3306:3306"
volumes:
- ./app/myapp.com:/var/www/html:cached
working_dir: /var/www/html
networks:
- my_network
So when i try to run the php example above, i am getting:
SQLSTATE[HY000] [1045] Access denied for user 'someuser'@'85.110.xxx.xx' (using password: YES
85.110.xxx.xx is the acutal ip address i am getting with my macbook on https://whatsmyip.com/
thats wierd for me, because the ip i try to reach is 5.9.xxx.xx . The mysql user, the database and the password are correct i triple checked it. I can even login with sequel. I already tried to make the port 3306 public for the php container, doesn't change anything.
I will be very thankful, if anyone can help me on this.
In MySQL a user is defined by a username AND an IP address from which they connect. There is the exceptional "%" meaning every IP is accepted. So if your MySQL user isn't defined on the server like 'someuser'@'85.110.xxx.xx'
or 'someuser'@'%'
, the server will deny access to you.
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.