MySQL 通过 CodeIgniter 连接到远程数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8107449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:39:58  来源:igfitidea点击:

Connecting to a remote database through CodeIgniter

mysqlcodeigniterremote-access

提问by Mikey

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.

我在将应用程序连接到远程数据库时遇到了困难。当我运行我的应用程序时,我收到了上面显示的错误。我在这个网站上查看了这个问题以前的答案,但我没有解决这个问题。根据我的了解,我需要替换主机名并使我的 mysql 数据库接受外部连接,这两个我都做过。我通过 mysql 命令行在我的 database.php 中尝试了相同的值,并且它有效。

So what else could I possibly be missing?

那么我还可能缺少什么?

Here are my settings in 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;

采纳答案by Robert Van Sant

try this command from your shell to see if you can connect:

在你的 shell 中试试这个命令,看看你是否可以连接:

mysql -h HOSTNAME -uUSERNAME -p DATABASE

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.

还要检查您是否没有其他端口可以连接到 mysql 集,默认情况下,codeigniter 将连接到 3306,除非您定义了实际的端口集。这可能是问题所在。

good luck

祝你好运

回答by cristi _b

mysql by default accepts only localhost connections, make sure your server is configured properly locally

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

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

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

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

回答by Mia Torres

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

try to change above code to

尝试将上面的代码更改为

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

回答by Gopal Gautam

If you have enforced SELinux Policy in your server make sure you enable httpd_can_network_connect boolean flag

如果您在服务器中强制执行 SELinux 策略,请确保启用 httpd_can_network_connect 布尔标志

setsebool -P httpd_can_network_connect=1