DB接続ができません
Q&A
解決したいこと
DB接続ができずDBにデータをアップロードできません
該当するソースコード
public static function getConnection()
{
// データベース接続処理する
$mysqli = new mysqli('localhost','root','pass','keijiban_db');
if($mysqli->connect_error){
echo $mysqli->connect_error;
exit();
} else {
$mysqli->set_charset("utf8");
}
return $mysqli;
}
docker.compose.yml
services:
keijiban_application:
build: ./docker/php
container_name: "keijiban_application"
ports:
- "8080:80"
volumes:
- ./application/docroot:/var/www/html
- ./application/private:/var/www/private
- ./docker/php/php.ini:/usr/local/etc/php/conf.d/docker.ini
- ./docker/php/php-fpm.conf:/etc/php-fpm.d/docker.conf
keijiban_db:
build:
context: ./docker/php
dockerfile: Dockerfile
platform: linux/x86_64
image: mysql:5.7
container_name: "keijiban_db"
environment:
MYSQL_DATABASE: "keijiban_db"
MYSQL_USER: "root"
MYSQL_PASSWORD: "pass"
MYSQL_ROOT_PASSWORD: "pass"
ports:
- "3306:3306"
volumes:
- ./docker/mysql/data:/var/lib/mysql
- ./docker/mysql/my.cnf:/etc/mysql/conf.d/my.cnf
- ./docker/mysql/initdb.d:/docker-entrypoint-initdb.d