通过CodeIgniter连接到远程数据库
无法使用提供的设置连接到数据库服务器。
我在将应用程序连接到远程数据库时遇到困难。当我运行我的应用程序时,我收到了上面显示的错误。我已经在这个网站上查看过这个问题的先前答案,但我没有解决它。根据我了解到的情况,我需要替换主机名并使我的 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;
Unable to connect to your database server using the provided settings.
I've been having difficulty connecting my application to my remote database. I'm receiving the error shown above when I run my application. I've looked at previous answers to this question on this site, but I've gotten no luck at solving it. From what I learnt, I need to replace the hostname and make my mysql database accept external connections, which I have both done. I tried the same values in my database.php through the mysql command-line and it works.
So what else could I possibly be missing?
Here are my settings in 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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从你的 shell 尝试这个命令,看看你是否可以连接:
还要检查你是否没有不同的端口来连接到 mysql 设置,默认情况下,codeigniter 将连接到 3306,除非你定义了实际的端口集。这可能就是问题所在。
祝你好运
try this command from your shell to see if you can connect:
also check to see if you don't have a different port to connect to mysql set, by default codeigniter will connect to 3306, unless you define the actual port set. this might be the issue.
good luck
mysql 默认情况下仅接受本地主机连接,请确保您的服务器在本地正确配置
http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html
确保你的服务器不在私有子网中的路由器后面,您可能需要配置静态 NAT 将您的服务器端口转发到网络
mysql by default accepts only localhost connections, make sure your server is configured properly locally
http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html
make sure that your server isn't behind a router that's in a private subnet, you might neet to configure static NAT for portforwarding your server into the web
尝试将上面的代码更改为
try to change above code to
如果您在服务器中强制执行 SELinux 策略,请确保启用 httpd_can_network_connect 布尔标志
If you have enforced SELinux Policy in your server make sure you enable httpd_can_network_connect boolean flag