Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker MySQL: create new user

In the mysql docker hub page there's a reference on how to create users with:

MYSQL_USER, MYSQL_PASSWORD

But how can you specify those parameters on the docker-compose.yml file?

So far I have:

mysql:
  image: mysql:5.7
  ports:
    - "3306:3306"
  environment:
    MYSQL_ROOT_PASSWORD: R00t+

Another question; how can I connect to the mysql host from outside the container? Inside the container I can connect using:

$user = 'root';
$pass = 'R00t+';
$server = 'mysql';

$dbh = new PDO( "mysql:host=$server", $user, $pass );
like image 683
numediaweb Avatar asked Dec 25 '22 01:12

numediaweb


1 Answers

About password you are setting all parameters exactly the same as you set root password which is:

mysql:
  image: mysql:5.7
  ports:
    - "3306:3306"
  environment:
    MYSQL_ROOT_PASSWORD: R00t+
    MYSQL_USER: youruser
    MYSQL_PASSWORD: yourpassword

To connect with mysql outside container just as host use localhost because you redirect ports from container to host machine in this line - "3306:3306"

$user = 'root';
$pass = 'R00t+';
$server = 'localhost';

$dbh = new PDO( "mysql:host=$server", $user, $pass );

I assume you are running it on your laptop where you have docker containers started.

like image 149
nospor Avatar answered Dec 26 '22 15:12

nospor