How do I connect to a Google Cloud SQL database using CodeIgniter?

前端 未结 2 1547
离开以前
离开以前 2021-01-07 13:39

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
相关标签:
2条回答
  • 2021-01-07 14:06

    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

    0 讨论(0)
  • 2021-01-07 14:09

    Have you authorized your appengine app for access to the Cloud SQL instance? Go to the access control panel on the console for the instance (at https://cloud.google.com/console#/project/{project name}/sql/instances/{instance name}/access-control). Look for authorized app engine applications.

    Otherwise, if you're connecting to the instance successfully, you'll have to choose the database from your code or configuration (depending on the app). For example, from the "running wordpress" guide (https://developers.google.com/appengine/articles/wordpress) you have to define DB_NAME. If you're handling the connections in your own code you'll need to use mysql_select_db.

    From skimming the codeigniter docs, it looks like you need something like: $config['database'] = "mydatabase";

    I'm not familiar with this framework though, so check the docs yourself (http://ellislab.com/codeigniter/user-guide/database/configuration.html).

    0 讨论(0)
提交回复
热议问题