Codeigniter Can't Connect to External SQL Server Codeigniter Can't Connect to External SQL Server codeigniter codeigniter

Codeigniter Can't Connect to External SQL Server


By default, MySQL will deny connections from outside.

Where is your MySQL server located in? Usually if you're using some web hosting service, they allow you to unlock external connections from they CPanel. Note that most of them require some time to made it really active.

Also, please note mysql_connect does not return an object, but just a resource (connection ID) instead. So if you really want to still using the deprecated (old) MySQL API, please do:

<?php$link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); // note that if the port is different from 3306, you must do: 'server:port'if (!$link) {    die('Could not connect ' . mysql_error());}echo 'Connection successfull';mysql_close($link);

However, it's recommended to use something like MySQLi or PDO, as MySQL extension is deprecated due to a long time not being update, lack of OOP support, lack of new MySQL features support etc etc.

Alternative using MySQLI, from PHP manual:

<?php$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");if (!$link) {    echo "Error: Unable to connect to MySQL." . PHP_EOL;    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;    exit;}echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;mysqli_close($link);


1) Look at /etc/mysql/my.cnf file and make it have these lines:

bind-address = 0.0.0.0# skip-networking

2) run /etc/init.d/mysql restart or something like this to restart mysql to open connection from outside

3) enter mysql console and execute:

GRANT ALL ON database_name.* to 'remote_user'@'app_servers_global_ip' IDENTIFIED BY 'very_hard_password';FLUSH PRIVILEGES;

4) have exact this code in Your config/database.php

$active_group = 'remote';$query_builder = TRUE;$db['remote'] = array(    'dsn'   => '',    'hostname' => 'database_remote_ip_here',    'username' => 'remote_user',    'password' => 'very_hard_password',    'database' => 'database_name',    'dbdriver' => 'mysqli',    'dbprefix' => '',    'pconnect' => FALSE,    'db_debug' => FALSE,    'cache_on' => FALSE,    'cachedir' => '',    'char_set' => 'utf8',    'dbcollat' => 'utf8_general_ci',    'swap_pre' => '',    'encrypt' => FALSE,    'compress' => FALSE,    'stricton' => FALSE,    'failover' => array(),    'save_queries' => FALSE);