0

I have codeigniter (php framework) running on an nginx webserver on my local machine (Mac OSX 10.8).

I am trying to connect to an external mySQL database hosted with mediaTemple.

When loading in the database helper in codeigniter, with all the correct details, l keep getting:

  A Database Error Occurred
  Unable to connect to your database server using the provided settings.

The contents of my database.php file is:

  $db['default']['hostname'] = 'external-db.#######.gridserver.com';
  $db['default']['username'] = '#######';
  $db['default']['password'] = '#######';
  $db['default']['database'] = '#######_olliejennings';
  $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;

The contents of my nginx.conf file is:

  daemon off;

  user olliejennings staff;

  worker_processes 2;
  pid /opt/boxen/data/nginx/nginx.pid;

  events {
     worker_connections 1024;
  }

  http {
     include mime.types;
     default_type application/octet-stream;

     log_format main '$remote_addr - $remote_user [$time_local] $status '
                     '"$request" $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "http_x_forwarded_for"';

     access_log /opt/boxen/log/nginx/access.log main;
     error_log  /opt/boxen/log/nginx/error.log debug;

     sendfile on;

     tcp_nopush  on;
     tcp_nodelay off;

     gzip              on;
     gzip_http_version 1.0;
     gzip_comp_level   2;
     gzip_proxied      any;

     upstream www-upstream-pool {
          server unix:/tmp/php-fpm.sock;
     }

     server {
          listen      80;
          server_name localhost;

          location / {
               root   /opt/boxen/config/nginx/public;
          }
     }

     include /opt/boxen/config/nginx/sites/*;
  }

And the contents of my sites/olliejennings.conf file is:

  server {
       server_name      olliejennings.dev;
       root         /Users/olliejennings/Code/olliejennings;
       index        index.php index.html index.htm;

       location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
         expires max;
         log_not_found off;
         add_header Pragma public;
         add_header Cache-Control "public, must-revalidate, proxy-revalidate";
       }

       location / {
         # Check if a file exists, or route it to index.php
         try_files $uri $uri/ /index.php;
       }

       location ~ \.php$ {
         include fastcgi_php_default.conf;
         fastcgi_pass 127.0.0.1:9000;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME $document_root/index.php;
       }
   }

NB: the php website works fine if i don't try and connect to the external database.


EDIT - Found differences is the mySQL settings between nginx and apache

This is what phpinfo() throws out when l connect to the database on apache:

mySQL config when using apache

This is what the phpinfo() throws out when using nginx as l can't connect to the database:

mySQL config when using nginx

Also the socket for mySQL when using nginx is wrong as it is not located at

/var/mysql/mysql.sock

But instead located at:

/temp/mysql.sock

I have chnaged these settings in the php.ini file, yet they don't appear to be read by php when using nginx, it appears to ignore anything l change within that file.

9
  • r u sure that hostname is not localhost? Commented Apr 11, 2013 at 13:39
  • Is your remote database allowed to accept remote connections? Commented Apr 11, 2013 at 13:45
  • Yes the database is allowed to accept remote connections, l can connect to this database via phpmyadmin or through sequel Pro Commented Apr 11, 2013 at 13:52
  • Did you triple check the username and pass? There shouldn't be anything explicitly denying your request... Commented Apr 11, 2013 at 14:54
  • 1
    Try adding $db['default']['port'] = 3306; to the db conf file. Maybe it needs to be explicitly set for some reason. Also, maybe try writing a little script with native php mysqli and running it on nginx to make certain it's an issue with nginx. Commented Apr 11, 2013 at 17:13

1 Answer 1

1

Okay so l have finally managed to fix this issue.

When l tried using native php mysqli that stormdrain suggested l was getting the message:

Connect failed: mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication.

So to fix this issue l did the following:

  1. Connect to mysql database using phpmyadmin
  2. Ran the following query SHOW VARIABLES LIKE 'old_passwords'
  3. If this returns ON then l knew that the problem lied with the database.
  4. Run this query to turn this variable off - SET SESSION old_passwords=FALSE;

Then l was able to connect to my mySQL database with out any problems or code change.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.