0

I have a Codeigniter web app hosted on AWS EC2 (PHP7 and PDO is used to connect to db) and database setup on AWS RDS Mysql. To get unicode support I recreated the database and all tables in utf8mb4 charset. Now my CI app give me a connection timeout. I tried connecting to the new db from localhost and it worked perfectly.

$servername = "*******.*******.us-east-2.rds.amazonaws.com";
$username = "*****";
$password = "*****";

try {
    $conn = new 
    PDO("mysql:host=$servername;dbname=*****;charset=utf8mb4;", $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();
}

Here is my CI database.php

$db['default'] = array(
    'dsn'  => $_SERVER['DSN'],
    'hostname' => $_SERVER['DB_HOST'],
    'username' => $_SERVER['DB_USER'],
    'password' => $_SERVER['DB_PASSWORD'],
    'database' => $_SERVER['DB_NAME'],
    'dbdriver' => 'pdo',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8mb4',
    'dbcollat' => 'utf8mb4_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);
  • And by changing to standard 3-byte UTF-8 (utf8) the connection works ok? – Martin Aug 08 '18 at 16:34
  • nope even I keep the code same with utf8 it doesn't connect. Only changing the db to utf8mb4 made application connection timeout – Upul Dissanayake Aug 08 '18 at 16:42
  • But it connects without any issue with my mysqlworkbench and even from localhost – Upul Dissanayake Aug 08 '18 at 16:43
  • Your reply is confusing. If you are NOT using `utf8mb4` and the connection is NOT successful then `charset=utf8mb4` is ***NOT*** to blame. Please clarify, – Martin Aug 08 '18 at 16:45
  • Check your connection details - address, IP, username, password and user priveleges. etc. – Martin Aug 08 '18 at 16:46
  • Try also: REMOVE `charset=...` and see if that PDO connection works ok? – Martin Aug 08 '18 at 16:47
  • Yeah I am not saying the code have an issue. Changing the db charset from latin to utf8mb4 made application to timeout when connecting to mysql db. I will try now removing the charset completely from the "DSN" – Upul Dissanayake Aug 08 '18 at 16:53
  • Same issue even I completely remove the charset from "DSN" string – Upul Dissanayake Aug 08 '18 at 16:59
  • I sorted it out. It was a security group issue like you've said. Security group automatically opened it for my current IP and the server IP was blocked. Now it's working fine. Thanks for your help @Martin – Upul Dissanayake Aug 08 '18 at 17:19
  • Do you have `$config['charset'] = "UTF-8";`? (Note two spelling differences -- this is talking to the outside world, not MySQL.) – Rick James Aug 24 '18 at 15:29
  • @RickJames I sorted out the issue. It was my IP was blocked in AWS. Now it's working fine :) – Upul Dissanayake Aug 31 '18 at 11:08

0 Answers0