Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>I chose to use the Mysqli driver instead of Mysql for performance reasons and here is how I did it:</p> <p>Step 1) Edit the codeigniter/system/database/drivers/mysqli/mysqli_driver.php file and replace the db_connect function with the following code:</p> <pre><code>function db_connect() { if(isset($this-&gt;socket)){ return mysqli_connect(null, $this-&gt;username, null, $this-&gt;database, null, $this-&gt;socket); } elseif ($this-&gt;port != ”) { return mysqli_connect($this-&gt;hostname, $this-&gt;username, $this-&gt;password, $this-&gt;database, $this-&gt;port); } else { return mysqli_connect($this-&gt;hostname, $this-&gt;username, $this-&gt;password, $this-&gt;database); } } </code></pre> <p>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.</p> <pre><code>$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’; </code></pre> <p>Viola, your CodeIgniter application should now be able to connect and talk to your Google Cloud MySQL database!</p> <p>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…</p> <p>Answer courtesy of <a href="http://arlogilbert.com/post/67855755252/how-to-connect-a-codeigniter-project-to-google-cloud" rel="noreferrer">http://arlogilbert.com/post/67855755252/how-to-connect-a-codeigniter-project-to-google-cloud</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload