Home > Net >  PHP Script Connecting to MySQL Docker Container :: What Am I Doing Wrong?
PHP Script Connecting to MySQL Docker Container :: What Am I Doing Wrong?

Time:04-05

I have a MySQL Docker container running, and I need a PHP script to connect to the container to do database stuff. My script isn't working, and I can't tell if its a script thing or a container thing.

First, my system. I'm developing on an Ubuntu 16.04.7 machine, running Docker 20.10.7, should be the latest. My MySQL Container was pulled from Docker Hub (docker pull mysql pulled down Image ID 667ee8fb158e). I spun up the container with this command:

docker run --detach --name=myMYSQL -p 52000:3306  --env="MYSQL_ROOT_PASSWORD=password123" 667ee8fb158e

Note the port mapping here. For reasons too numerous for this post, this mapping is required. It may be what is causing my script errors later, I dunno.

Before I discuss my script, I manually connected to the database and did some admin work:

mysql>
mysql> CREATE USER 'user01'@'localhost';
Query OK, 0 rows affected (0.02 sec)

mysql> SET PASSWORD FOR 'user01'@'localhost' = 'password123';
Query OK, 0 rows affected (0.01 sec)

mysql> create database myDB;
Query OK, 1 row affected (0.05 sec)

mysql> GRANT ALL PRIVILEGES ON myDB.* TO 'user01'@'localhost';
Query OK, 0 rows affected, 1 warning (0.03 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> show grants for 'user01'@'localhost';
 ------------------------------------------------------------------------- 
| Grants for user01@localhost                                             |
 ------------------------------------------------------------------------- 
| GRANT ALL PRIVILEGES ON `myDB`.* TO `user01`@`localhost`                |
 ------------------------------------------------------------------------- 
1 row in set (0.00 sec)

mysql>

Okay, now for the PHP script. After doing an apt-get install for mysql-client-core-5.7 and php-mysql, I used the below script:

<?php
$servername = "127.0.0.1";
$username = "user01";
$password = "password123";
$database = "myDB";
$mysqlPort = "52000"
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database, $mysqlPort, null);
// Check connection
if (!$conn) {
    die("OH NO!!!  Connection has failed, dude: " . mysqli_connect_error());
}
echo "Excellent, connected successfully!!!";
mysqli_close($conn);
?>

(Note how I'm using mysqli_connect() here. According to this post, if I'm using "127.0.0.1" as the $servername, I'm forcing a TCP connection and I don't need to supply a $socket. That's fine with me, as I need a TCP connection here.)

Fowever, the script fails. Here's the command line output:

me@myUbuntu:~$
me@myUbuntu:~$ php phpMySQL.php
PHP Warning:  mysqli_connect(): (HY000/1045): Access denied for user 'user01'@'10.10.10.10' (using password: YES) in /home/me/phpMySQL.php on line 8
OH NO!!!  Connection has failed, dude: Access denied for user 'user01'@'10.10.10.10' (using password: YES)me@myUbuntu:~$

Arrgh. A number of Google searches imply that my script is successfully connecting to the MySQL container, but being denied? Maybe. Sadly, I can't enable the MySQL error logs to confirm. But I can't help wonder if my port-mapping are coming back to bite me and the script is never successfully reaching the container? I wish I knew. Does anyone have any advice? Thank you.

CodePudding user response:

I think your grant should be at 10.10.10.10.

GRANT ALL PRIVILEGES ON `myDB`.* TO `user01`@`10.10.10.10`

Make sure that the user is also @10.10.10.10

  • Related