Codeigniter 3.0 can not connect to local mysql, so please let me know the reason.
Error message is SQLSTATE [HY000] [2002] Connection refused. My Codeigniter is working on Docker.
Specifically on Docker in mac
- php-fpm 7.1
- MySQL 5.7
- Nginx 2.4
Containers are working.
And that MySQL container can be connected from sequel pro or Mac terminal, only from Codeigniter can connect.
Successful connection code from my Mac terminal
mysql -uroot -h 127.0.0.1-P 4306 -ppassword
db_test.php
<?php try { $pdo = new PDO( 'mysql:dbname=******;host=127.0.0.1;charset=utf8', 'root', 'password', [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ] ); } catch (PDOException $e) { header('Content-Type: text/plain; charset=UTF-8', true, 500); exit($e->getMessage()); }
result — SQLSTATE [HY000] [2002] Connection refused.
/config/database.php
}elseif (strpos($_SERVER['SERVER_NAME'],'myname') !== false && ENVIRONMENT != "test"){ // MyLocal Setting $db['default'] = array( 'dsn' => '', 'hostname' => '127.0.0.1', 'port' => '4306', 'username' => 'root', 'password' => 'password', 'database' => '******', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => TRUE, 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE );
docker-compose.yml
version: '2' services: memcached: image: memcached:1.4 data: image: busybox volumes: - ./misc/data:/var/lib/mysql mysql: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: password MYSQL_DATABASE: mysql_database MYSQL_USER: mysql_user MYSQL_PASSWORD: mysql_pw socket: /var/run/mysqld/mysqld.sock ports: - "4306:3306" volumes_from: - data volumes: - ../application/initial.sql:/docker-entrypoint-initdb.d/initial.sql container_name: mysql nginx: build: ./nginx ports: - "80:80" - "443:443" volumes: - ./nginx/default.conf:/etc/nginx/conf.d/default.conf links: - web container_name: nginx environment: - CI_ENV=localhost web: build: ./php-fpm71 # build: ./php-fpm56 <- if you want to use PHP5.6 volumes: - /Users/alberorana/codeigniter/suke10:/var/www/html/suke10 - ./php-fpm71/php.ini:/usr/local/etc/php/conf.d/php.ini links: - mysql environment: DATABASE_HOST: '0.0.0.0' DATABASE_NAME: 'suke10' DATABASE_USER: 'root' DATABASE_PASSWORD: 'password' container_name: web
etc/mysql/mysql.conf.d/mysqld.cnf in MySQL container
[mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql #log-error = /var/log/mysql/error.log # By default we only accept connections from localhost #bind-address = 127.0.0.1 # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0
Advertisement
Answer
Your database connection inside the php container is:
mysql:3306
Since you in your compioser file, “link” the phpcontainer to the databaser container, and a link is essentliation addition of a line in your hosts-file, so mysql will point to the ip in the internal docker-network for you.