0

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yes! You are completely right, thank you! In hindsight, I see what the trouble was; the Docker container thinks my PHP script is coming from another host... which, in a way, it is. Once I recreated the user01 with the '10.10.10.10' IP address, everything worked fine. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.