Codeigniter Can';t连接到外部SQL Server


Codeigniter Can't Connect to External SQL Server

我正在尝试将我的代码点火器框架连接到外部数据库。但它显示错误

发生数据库错误无法连接到数据库服务器使用提供的设置。文件名:core/Loader.php行号:346

然后,我将这个插入到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__); 

但它显示

遇到PHP错误严重性:8192消息:mysql_connect():mysql扩展已弃用,将将来删除:使用mysqli或PDO文件名:config/database.php线路编号:79

遇到PHP错误严重性:警告消息:mysql_connect():无法连接到上的mysql服务器'167.114.xxx.xxx'(111)文件名:config/database.php线路编号:79

无法连接到数据库,因为:无法连接到MySQL服务器关于'167.114.xxx.xxx'(111)

然后我尝试在codeigniter目录(在publichtml中)之外创建这个

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

显示连接成功。那么,我该怎么办?而config/database.php中的数据库细节与上面的相同

默认情况下,MySQL将拒绝来自外部的连接。

您的MySQL服务器位于哪里?通常,如果你使用的是一些网络托管服务,它们允许你从CPanel解锁外部连接。请注意,它们中的大多数都需要一些时间才能使其真正活跃起来。

另外,请注意mysql_connect不返回对象,而只返回资源(连接ID)。因此,如果你真的想继续使用不推荐使用的(旧的)MySQL API,请这样做:

<?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扩展由于长时间未更新、缺乏OOP支持、缺乏对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);

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