We have a dedicated server with proxmox on OVH. The idea is to connect the containers locally but also trough internet. So far I have 2 containers.
I added a bridge network for local IP and that is working since I am able to ping the containers from each other.
Also added bind-address=192.168.1.3
to my.cnf.
1 container is running apache + php 7.2 (192.168.1.3)
The other container is running MySQL. (192.168.1.2)
My MySQL keeps saying SQLSTATE[HY000] [2002] Connection timed out
Here is my php code:
<?php
/**
* Configuration for database connection
*
*/
$host = "192.168.1.2";
$username = "root";
$password = "root";
$dbname = "test";
$dsn = "mysql:host=$host;dbname=$dbname";
$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
try
{
$connection = new PDO("mysql:host=$host", $username, $password, $options);
$sql = file_get_contents("data/init.sql");
$connection->exec($sql);
echo "Database and table users created successfully.";
}
catch(PDOException $error)
{
echo $sql . "<br>" . $error->getMessage();
}
From my understanding the code is correct so it must be something with my mysql configuration.
I'm sure that is something really simple but I'm losing to much time with this.
After searching around for a while. Found out that I had to create a new user and grant permissions to it
mysql> CREATE USER 'monty'@'localhost' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'localhost'
-> WITH GRANT OPTION;
mysql> CREATE USER 'monty'@'%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'%'
-> WITH GRANT OPTION;
Also on my server config /etc/mysql/mariadb.conf.d/50-server.conf
I had to comment the
bind-address = 192.168.1.3
into
#bind-address = 192.168.1.3
Then restart mysql server and change my mysql details on my php code with username 'monty' and password 'some_pass'