我怎样才能连接到MySQL到PHP的PHP文件?

我的docker-compose.yml文件,我成功运行它。 我可以看到运行所有的过程。 mertingen/php-fpm安装了pdopdo-mysql ;

 version: '2.0' services: web: image: nginx ports: - "8080:80" volumes: - ./code:/var/www/hmtl - ./site.conf:/etc/nginx/conf.d/default.conf links: - php php: image: mertingen/php-fpm volumes: - ./code:/var/www/hmtl - ./site.conf:/etc/nginx/conf.d/site.conf mysql: image: mysql ports: - "3333:3306" environment: - MYSQL_ROOT_PASSWORD=1234567890 

我有一个index.php并将其卷起来;

 <?php $servername = "mysql"; $username = "root"; $password = "1234567890"; try { $conn = new PDO("mysql:host=$servername;", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?> 

Nginx conf在这里;

 server { listen 80; index index.php index.html; server_name localhost; error_log /var/log/nginx/error.log; access_log /var/log/nginx/access.log; root /var/www/hmtl; location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass php:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; } } 

我把http://localhost:8080上的502 Gateway Error Nginx放入Host