如何使用CodeIgniter连接到Google Cloud SQL数据库? [英] How do I connect to a Google Cloud SQL database using CodeIgniter?

查看:573
本文介绍了如何使用CodeIgniter连接到Google Cloud SQL数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google App Engine上的我的CodeIgniter应用程式无法连线至Google Cloud SQL上的我的资料库。我尝试了这么多东西。


  1. 我的网站加载时,我离开数据库用户名,密码&数据库名称为空,但具有数据库调用的页面显示错误。

  2. 我注意到我的数据库没有被创建并创建了一个新的数据库和一个拥有所有权限的用户。我在我的应用程序中输入此详细信息,现在,它甚至不连接到数据库服务器。

  3. 当我仅删除用户名和密码时,

我检查了mysql中的密码字段,它连接到数据库服务器,但不连接到数据库服务器。数据库为用户和我的用户拥有所有权限。我检查了所有拼写,这是正确的。该应用程序在本地工作。我该怎么固定?

解决方案

开箱即用的CodeIgniter不会连接到Google Cloud SQL实例,需要修改CI数据库驱动程序文件,这是因为CI期望它的选择是连接到localhost或远程tcpip主机,开发人员从来没有预期任何人都想直接连接到套接字。



我选择使用Mysqli驱动程序而不是Mysql的性能原因,这是我怎么做的:



步骤1)编辑codeigniter / system / database / drivers / mysqli / mysqli_driver.php文件,并用以下代码替换db_connect函数:

 函数db_connect()
{
if(isset($ this-> socket)){
return mysqli_connect(null,$ this-> username,null,$ this-> database ,null,$ this-> socket);
}
elseif($ this-> port!=)
{
return mysqli_connect($ this-> hostname,$ this-> username,$ this- > password,$ this-> database,$ this-> port);
}
else
{
return mysqli_connect($ this-> hostname,$ this-> username,$ this-> password,$ this-> database) ;
}
}

步骤2)修改应用程序的config / database.php (或者你想声明你的数据库设置) - 根据你的应用程序,你可以选择添加数据库到autoload数组在yourapp / config / autoload.php或者你可以选择手动调用load->数据库)函数。这假定您的应用程序名称为myappname。替换APPENGINE-ID和DATABASE-INSTANCE-ID和YOUR_DATABASE_NAME。

  $ db ['myappname'] ['hostname'] ='localhost'; 
$ db ['myappname'] ['username'] ='root';
$ db ['myappname'] ['password'] = null;
$ db ['myappname'] ['database'] ='YOUR_DATABASE_NAME';
$ db ['myappname'] ['dbdriver'] ='mysqli';
$ db ['myappname'] ['pconnect'] = FALSE;
$ db ['myappname'] ['dbprefix'] ='';
$ db ['myappname'] ['swap_pre'] ='';
$ db ['myappname'] ['db_debug'] = FALSE;
$ db ['myappname'] ['cache_on'] = FALSE;
$ db ['myappname'] ['autoinit'] = FALSE;
$ db ['myappname'] ['char_set'] ='utf8';
$ db ['myappname'] ['dbcollat​​'] ='utf8_general_ci';
$ db ['myappname'] ['cachedir'] =;
$ db ['myappname'] ['socket'] ='/ cloudsql / APPENGINE-ID:DATABASE-INSTANCE-ID';

Viola,您的CodeIgniter应用程序现在应该能够连接到您的Google Cloud MySQL数据库! / p>

现在,如果您想要真正想要和启用数据库缓存,请更改CI代码以使用memcache(最快)或Google云存储(更保证持久性)但我不会在这个博客中覆盖...



回答 http://arlogilbert.com/post/67855755252/how-to-connect-a-codeigniter-project-to-google-cloud


My CodeIgniter app on Google App Engine is not able to connect to my database on Google Cloud SQL. I tried so many things.

  1. My site loads when I leave database username, password & database name empty but, pages that have database calls show an error. It says that no database was selected.
  2. I noticed that my database was not created and created a new database and a user with all privileges. I entered this details in my app and now, it doesn't even connect to the database server. No pages serve.
  3. When I remove only the username & password fields in database.php, it connects to the database server but, doesn't connect to the database.

I checked the mysql database for users and my user has all privileges. I checked all spellings and it is correct. The app is working locally. HOW I CAN FIX THIS? i just can't get it to connect.

解决方案

Out of the box CodeIgniter will not connect to a Google Cloud SQL instance, modifications to the CI database driver files are required, this is because CI expects that it’s choices are either to connect to localhost or to a remote tcpip host, the developers never anticipated that anybody would want to connect directly to a socket.

I chose to use the Mysqli driver instead of Mysql for performance reasons and here is how I did it:

Step 1) Edit the codeigniter/system/database/drivers/mysqli/mysqli_driver.php file and replace the db_connect function with the following code:

function db_connect()
{
    if(isset($this->socket)){
        return mysqli_connect(null, $this->username, null, $this->database, null, $this->socket);
    }
    elseif ($this->port != ")
    {
        return mysqli_connect($this->hostname, $this->username, $this->password, $this->database, $this->port);
    }
    else
    {
        return mysqli_connect($this->hostname, $this->username, $this->password, $this->database);
    }
}

Step 2) Alter your application’s config/database.php (or wherver you want to declare your database settings) - Depending on your application you may choose to add "database" to the autoload array in the yourapp/config/autoload.php or you may choose to manually call the load->database() function. This assumes your application name is "myappname". Replace APPENGINE-ID and DATABASE-INSTANCE-ID and YOUR_DATABASE_NAME appropriately.

$db[‘myappname’][‘hostname’] = ‘localhost’;
$db[‘myappname’][‘username’] = ‘root’;
$db[‘myappname’][‘password’] = null;
$db[‘myappname’][‘database’] = ‘YOUR_DATABASE_NAME’;
$db[‘myappname’][‘dbdriver’] = ‘mysqli’;
$db[‘myappname’][‘pconnect’] = FALSE;
$db[‘myappname’][‘dbprefix’] = ‘’;
$db[‘myappname’][‘swap_pre’] = ‘’;
$db[‘myappname’][‘db_debug’] = FALSE;
$db[‘myappname’][‘cache_on’] = FALSE;
$db[‘myappname’][‘autoinit’] = FALSE;
$db[‘myappname’][‘char_set’] = ‘utf8’;
$db[‘myappname’][‘dbcollat’] = ‘utf8_general_ci’;
$db[‘myappname’][‘cachedir’] = ";
$db[‘myappname’][‘socket’] = ‘/cloudsql/APPENGINE-ID:DATABASE-INSTANCE-ID’;

Viola, your CodeIgniter application should now be able to connect and talk to your Google Cloud MySQL database!

Now if you want to get really fancy and enable the database caching, either make alterations to the CI code to use memcache (fastest) or Google Cloud Storage (more guaranteed persistance) but I won’t cover that in this blog…

Answer courtesy of http://arlogilbert.com/post/67855755252/how-to-connect-a-codeigniter-project-to-google-cloud

这篇关于如何使用CodeIgniter连接到Google Cloud SQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆