且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用CodeIgniter连接到Google Cloud SQL数据库?

更新时间:2022-10-31 10:59:00

开箱即用的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