我是Docker的新手,一直試圖弄清楚如何使用PHP連接到我的MariaDB容器,但沒有成功。Docker無法通過PHP連接到mariadb
我試圖在計算器和谷歌搜索,但沒有找到任何有用的信息,所以我希望你們能幫助我。
奇怪的是,當我嘗試使用JetBrains公司DataGrip與本地主機,MySQL和根,管理連接到MariaDB的,我可以連接到數據庫,但不與PDO。
我真的希望你能幫助我,感謝你的時間。
這裏有下列項目文件:
這是我的搬運工,compose.yml文件
version: "3.1"
services:
nginx:
image: nginx:alpine
container_name: nginx
volumes:
- ./config/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "80:80"
links:
- php
php:
image: php:7.1-fpm
container_name: php
links:
- mariadb:mysql
volumes:
- ./public:/public
ports:
- "9000:9000"
mariadb:
image: mariadb:10.1
container_name: database
environment:
MYSQL_ROOT_PASSWORD: admin
ports:
- "3306:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
links:
- mariadb
ports:
- 8183:80
environment:
PMA_HOST: mariadb
PMA_USER: root
PMA_PASSWORD: admin
PMA_ARBITRARY: 1
我的nginx.conf文件:
server {
listen 80 default;
client_max_body_size 108M;
access_log /var/log/nginx/application.access.log;
root /public;
index index.php;
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
include fastcgi_params;
}
}
我的index.php文件
<?php
$servername = "localhost";
$username = "root";
$password = "admin";
$database = "mysql";
try {
$conn = new PDO("sqlite:host=".$servername.";dbname=" . $database, $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = $conn->prepare("SELECT * FROM testDB");
$sql->execute();
while($result = $sql->fetch(PDO::FETCH_ASSOC)){
$result['myTestData'];
}
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
也許你應該在你的DSN – Bart
sqlite'改變''給我mysql'已經試過了,我得到找不到消息驅動程序。 – user3626278
所以你需要安裝/啓用pdo mysql驅動 – Bart