Codeigniter cannot connect to external SQL Server

I am trying to connect my codeigniter system to an external database. But he shows an error

Database error occurred. Could not connect to the database server using the provided settings. File Name: core / Loader.php Line Number: 346

then I insert this to the end of config / database.php

  echo '<pre>';
  print_r($db['default']);
  echo '</pre>';

  echo 'Connecting to database: ' .$db['default']['database'];
  $dbh=mysql_connect
  (
    $db['default']['hostname'],
    $db['default']['username'],
    $db['default']['password'])
    or die('Cannot connect to the database because: ' . mysql_error());
    mysql_select_db ($db['default']['database']);

    echo '<br />   Connected OK:'  ;
    die( 'file: ' .__FILE__ . ' Line: ' .__LINE__); 

But he shows

A PHP error has occurred Severity: 8192 Message: mysql_connect (): the mysql extension is outdated and will be removed in the future: use mysqli or PDO instead File name: config / database.php Line number: 79

A PHP error has occurred Severity level: warning Message: mysql_connect (): Cannot connect to MySQL server on '167.114.xxx.xxx' (111) File name: config / database.php Line number: 79

, : MySQL '167.114.xxx.xxx' (111)

codeigniter dir ( public_html)

<?php
$servername = "167.114.xxx.xxx";
$username = "myusername";
$password = "dbpass";
$database = "dbname";

// Create connection
$conn = mysql_connect($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>

. ? db config/database.php

+4
2

MySQL .

MySQL? , - -, CPanel. , , .

, , mysql_connect , ( ). , () API MySQL, :

<?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);

- MySQLi PDO, MySQL - , , MySQL .. ..

MySQLI, PHP:

<?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);
0

1) /etc/mysql/my.cnf :

bind-address = 0.0.0.0
# skip-networking

2) /etc/init.d/mysql restart - , mysql,

3) mysql :

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

4), 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
);
0

All Articles