AceKYD
AceKYD

Reputation: 1140

CodeIgniter On Azure Connection to Windows Virtual Machine MySql

I'm working on a CodeIgniter web project that is being hosted on Azure and i decided to use MySQL on a virtual machine running Windows Server 2012 R2 in Azure following this tutorial as it seemed to be the best solution. https://azure.microsoft.com/en-gb/documentation/articles/virtual-machines-mysql-windows-server-2008r2/

I have it all setup and also managed to connect phpmyadmin hosted in my web project to the Virtual machine so its confirmed that everything works fine.

However, Codeigniter continues to give this error everytime

Unable to connect to your database server using the provided settings.

Filename: D:\home\site\wwwroot\system\database\DB_driver.php

Line Number: 124

I decided to create a test php file in the root of my installation with the following code and it connects successfully.

<?php
// Create connection : Update the hostname , DB username , DB password 
//, database name. 

$con=mysqli_connect("host","user","password",
"database");

// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_errno();
}
else
{
    echo "Connection with MySQL database was successful";
}
?>

PLEASE NOTE THAT VALUES HAVE BEEN CHANGED

I went into the system/database/drivers/mysql/mysql_driver.php and removed the "@" symbol to show the exact debugging error for db_pconnect() and i got

A PHP Error was encountered

Severity: Warning

Message: mysql_pconnect(): Access denied for user 'user'@'191.236.147.142' (using password: YES)

Filename: mysql/mysql_driver.php

Line Number: 91

But these same details work on a lone php script i mentioned above. Here is my codeIgniter Database config

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'myvmmysql.cloudapp.net';
$db['default']['username'] = 'user';
$db['default']['password'] = 'password';
$db['default']['database'] = 'database';
$db['default']['port'] = 3306;


$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;

Here is a summary of what i have done so far:

  1. I have tried mysql,mysqli and sqlsrv for $db['default']['dbdriver']
  2. I have also tried mysql in the test.php file and it still connects to the database
  3. I have granted public access to the port from all domains in the Virtual Machine
  4. I have given all privileges grant to the database user from all domains
  5. The Connection basically works everywhere except in codeigniter.
  6. I have also tried changing $db['default']['pconnect'] = FALSE; Not working NB: I have already done the necessary things involved in using database with codeigniter... this is more than that

Thank you

Upvotes: 0

Views: 1857

Answers (2)

Gary Liu
Gary Liu

Reputation: 13918

I have tried to reproduce your issue using 2.2.3 and 3.0.0 versions of codeiginter, however failed. I can connect to MySQL in VM successfully.

Please try to follow those steps:

1, Create a VM on Azure, install and configure MySQL.Please refer to the tutorials: https://azure.microsoft.com/en-gb/documentation/articles/virtual-machines-mysql-windows-server-2008r2/.

2, Create a Web app on Azure and set up deployment from git.

3, Download the condeiginter package to local, modify the database connection information in

\application\config\database.php

$db['default']['hostname'] = '<your vm name>.cloudapp.net';

$db['default']['username'] = '<database username>';

$db['default']['password'] = '<password>';

$db['default']['database'] = '<database name>';

4, Test MySQL connection on local, and deploy to Web app created above by git. Browse the Web app.

It works for me.

In addition, we could encounter this issue when we type the no password to the database user. So please check your database password in application\config\database.php .

By the way, you could connect to the MySQL Server using the workbench on your local for checking this error whether is existed.

Upvotes: 0

Abdulla Nilam
Abdulla Nilam

Reputation: 38642

First of all keep remeber you no need to use to connect database codes(mysql_connect()) in your file. All will be do by own in Codeigniter

So you no need to use this code(Below)

<?php
    // Create connection : Update the hostname , DB username , DB password 
    //, database name. 

        $con=mysqli_connect("host","user","password",
            "database");

    // Check connection
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_errno();
        }
        else
        {
            echo "Connection with MySQL database was successful";
        }
?>

Just go to config/autoload.php

$autoload['libraries'] = array('database')

This will connect your database.

And in config/database.php

$db['default']['hostname'] = '';//host name
$db['default']['username'] = '';//username
$db['default']['password'] = '';//password
$db['default']['database'] = '';//database name

Be sure about text(Caps/Nums)

Upvotes: 0

Related Questions