通过CodeIgniter连接到远程数据库

时间:2011-11-12 20:46:09

标签: mysql codeigniter remote-access

  

无法使用提供的设置连接到数据库服务器。

我一直无法将应用程序连接到远程数据库。我在运行应用程序时收到上面显示的错误。我在这个网站上看过这个问题的先前答案,但我没有运气解决它。从我学到的东西,我需要替换主机名,让我的mysql数据库接受外部连接,我已经完成了。我通过mysql命令行在我的database.php中尝试了相同的值,它可以工作。

那么我还有什么可能遗失的?

以下是我在database.php中的设置:

$active_group = 'staging';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'myDatabase';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

$db['staging']['hostname'] = 'XX.XX.XXX.XXX';
$db['staging']['username'] = 'user';
$db['staging']['password'] = 'pwd';
$db['staging']['database'] = 'myDatabase';
$db['staging']['dbdriver'] = 'mysql';
$db['staging']['dbprefix'] = '';
$db['staging']['pconnect'] = TRUE;
$db['staging']['db_debug'] = TRUE;
$db['staging']['cache_on'] = FALSE;
$db['staging']['cachedir'] = '';
$db['staging']['char_set'] = 'utf8';
$db['staging']['dbcollat'] = 'utf8_general_ci';
$db['staging']['swap_pre'] = '';
$db['staging']['autoinit'] = TRUE;
$db['staging']['stricton'] = FALSE;

4 个答案:

答案 0 :(得分:4)

从shell中尝试此命令,看看是否可以连接:

mysql -h HOSTNAME -uUSERNAME -p DATABASE

还要检查你是否有不同的端口连接到mysql set,默认情况下codeigniter会连接到3306,除非你定义了实际的端口集。这可能是个问题。

祝你好运

答案 1 :(得分:3)

mysql默认只接受localhost连接,确保你的服务器在本地正确配置

http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html

确保您的服务器不在私有子网中的路由器后面,您可能需要配置静态NAT以将服务器移植到网络中

答案 2 :(得分:1)

$active_group = 'staging';
$active_record = TRUE;

尝试将上述代码更改为

$active_group = 'default';
$active_record = TRUE;

答案 3 :(得分:0)

如果您已在服务器中强制执行SELinux Policy,请确保启用httpd_can_network_connect boolean flag

    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Dalston.SR2</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>1.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.zalando</groupId>
        <artifactId>spring-cloud-config-aws-kms</artifactId>
        <version>1.6</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>