Docker MySQL:创建新用户 [英] Docker MySQL: create new user
问题描述
在 mysql docker hub页面中,有关于如何使用以下方法创建用户的参考:
In the mysql docker hub page there's a reference on how to create users with:
MYSQL_USER, MYSQL_PASSWORD
但是如何在docker-compose.yml文件中指定这些参数?
But how can you specify those parameters on the docker-compose.yml file?
到目前为止,我有:
mysql:
image: mysql:5.7
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: R00t+
另一个问题;如何从容器外部连接到mysql主机?在容器内,我可以使用以下方式进行连接:
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 );
推荐答案
关于密码,您要设置的所有参数与您设置的root密码完全相同:
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
要像使用主机一样在容器外部与mysql连接,请使用 localhost
,因为您在此行中将端口从容器重定向到主机-"3306:3306"
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 );
我假设您是在启动了Docker容器的笔记本电脑上运行它的.
I assume you are running it on your laptop where you have docker containers started.
这篇关于Docker MySQL:创建新用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!