使用Docker从PHP连接到MySQL服务器 [英] Connect to MySQL server from php with Docker

查看:1135
本文介绍了使用Docker从PHP连接到MySQL服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Docker的新手.我正在Docker环境中开发PHP应用程序,并且需要使用MySql Server/数据库来持久化数据.根据文档,我运行以下命令来创建MySql服务器映像:

I am new to Docker. I am developing a PHP application in Docker environment, and I need to use a MySql Server/database to persist data. Following documentation, I run this command to create the MySql server image:

docker run -v /var/lib/mysql mariadb

它下载了它,但是我也遇到了这个错误:

It downloaded it, but I also go this error with it:

错误:数据库未初始化,并且未指定密码选项.您需要指定MYSQL_ROOT_PASSWORD,MYSQL_ALLOW_EMPTY_PASSWORD和MYSQL_RANDOM_ROOT_PASSWORD中的一个

error: database is uninitialized and password option is not specified You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD

如何设置MYSQL_ALLOW_EMPTY_PASSWORD选项?

How do I set the MYSQL_ALLOW_EMPTY_PASSWORD option?

然后我将如何在该映像上创建数据库,然后从我的PHP代码(在另一映像上)对其进行读/写操作?

And then how would I create a database on this image, and then read/write to it from my PHP code (which is on a different image)?

推荐答案

创建容器

运行此命令:docker run --name some-mariadb -e MYSQL_ROOT_PASSWORD=mysecretpw -v mysql:/var/lib/mysql -d mariadb:latest

由此,您将使用mariadb创建docker容器,并将数据存储在mysql文件夹中.在命令中,您有 -e MYSQL_ROOT_PASSWORD = my-secret-pw .这会将您的根密码设置为 my-secret-pw .当您连接到它时,您的用户是 root ,密码是 my-secret-pw .

With this, you are creating your docker container with mariadb, and storing data on mysql folder. In the command, you have -e MYSQL_ROOT_PASSWORD=my-secret-pw. This set your root password as my-secret-pw. When you connect to it, your user is root, and password is my-secret-pw.

查找地址

docker inspect some-mariadb-使用此命令,您将找到有关容器的所有信息.您需要您的IP地址.

docker inspect some-mariadb - With this command, you will find all information about your container. You need your IP address.

提示:docker inspect some-mariadb | grep IPAddress您可以更轻松地找到IP地址.

Hint: docker inspect some-mariadb | grep IPAddress you make easier to find your IP Address.

正在连接

现在,它是PHP的一部分.运行下面的PHP,但检查您的主机地址,在我的情况下为 172.17.0.2 (

Now, it is the PHP part. Run the PHP below, but check your host address, that in my case was 172.17.0.2 (from PHP documentation.):

<?php
$link = mysqli_connect("172.17.0.2", "root", "my-secret-pw");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made!" . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);

如果工作正常,您将阅读:

And if works fine, you will read:

A proper connection to MySQL was made!
Host information: 172.17.0.2 via TCP/IP

Ps .:要创建数据库,请像在没有docker的数据库中那样运行所需的命令.相同(CREATE DATABASE mydb).

Ps.: For database creation, run the required commands as you would make in a database without the docker. It is the same (CREATE DATABASE mydb).

这只是开始.我建议您研究 docker撰写.掌握了它之后,您会学到很多关于Docker优势的信息.

This is just for starting. I recommend you to study docker compose. When you get it, will you learn a lot about docker advantages.

这篇关于使用Docker从PHP连接到MySQL服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆