Welcome Guest, Not a member yet? Register   Sign In
how to connect multiple server problem
#1

[eluser]akioshin[/eluser]
please guys check my code i really need your response

this is my database.php

Code:
$server1['hostname'] = "localhost";
$server1['username'] = "akioshin";
$server1['password'] = "admin";
$server1['database'] = "bmds";
$server1['dbdriver'] = "mysql";
$server1['dbprefix'] = "";
$server1['pconnect'] = FALSE;
$server1['db_debug'] = TRUE;
$server1['cache_on'] = FALSE;
$server1['cachedir'] = "";
$server1['char_set'] = "utf8";
$server1['dbcollat'] = "utf8_general_ci";

$server2['hostname'] = "192.168.0.2";
$server2['username'] = "akioshin";
$server2['password'] = "admin";
$server2['database'] = "bmds";
$server2['dbdriver'] = "mysql";
$server2['dbprefix'] = "";
$server2['pconnect'] = FALSE;
$server2['db_debug'] = TRUE;
$server2['cache_on'] = FALSE;
$server2['cachedir'] = "";
$server2['char_set'] = "utf8";
$server2['dbcollat'] = "utf8_general_ci";

and i get this error

Quote:An Error Was Encountered

No database connection settings were found in the database config file.

but if i change my database.php to this
Code:
$active_group = "server1";
$active_group = "server2";
$active_record = TRUE;

$db['server1']['hostname'] = "localhost";
$db['server1']['username'] = "akioshin";
$db['server1']['password'] = "admin";
$db['server1']['database'] = "bmds";
$db['server1']['dbdriver'] = "mysql";
$db['server1']['dbprefix'] = "";
$db['server1']['pconnect'] = FALSE;
$db['server1']['db_debug'] = TRUE;
$db['server1']['cache_on'] = FALSE;
$db['server1']['cachedir'] = "";
$db['server1']['char_set'] = "utf8";
$db['server1']['dbcollat'] = "utf8_general_ci";

$db['server2']['hostname'] = "http://192.168.0.2/xampp";
$db['server2']['username'] = "akioshin";
$db['server2']['password'] = "admin";
$db['server2']['database'] = "bmds";
$db['server2']['dbdriver'] = "mysql";
$db['server2']['dbprefix'] = "";
$db['server2']['pconnect'] = FALSE;
$db['server2']['db_debug'] = TRUE;
$db['server2']['cache_on'] = FALSE;
$db['server2']['cachedir'] = "";
$db['server2']['char_set'] = "utf8";
$db['server2']['dbcollat'] = "utf8_general_ci";

and this error appear

Quote:A Database Error Occurred

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

this is my controller class

Code:
class Testing_Controller extends Controller
{
    function Testing_Controller()
    {
        parent::Controller();
    }
    
    function index()
    {
        $this->load->model('Testing_Model');
        $this->Testing_Model->test();
    }
}

and my model class

Code:
class Testing_Model extends Model
{
    function Testing_Model()
    {
        parent::Model();
    }
    
    function test()
    {
        
        $DB1 = $this->load->database($server1,TRUE);
        //$DB2 = $this->load->database('server2',TRUE);
        
        
        $DB1->query("INSERT INTO user(username) VALUES('yuuhi')");
        //$DB2->query("INSERT INTO user(username) VALUES('yuuhi')");
    }
}
#2

[eluser]jedd[/eluser]
Two suggestions.

First, are you autoloading the database class (config/autoload.php) - because you're not loading it in the code you've shown.

Second, you have successfully connected from the CLI?

Code:
$   mysql  -u  akioshin  -p bmds
.. using the password 'admin' (or whatever it is really set to)?
#3

[eluser]JayTee[/eluser]
[quote author="akioshin" date="1236152789"]but if i change my database.php to this
Code:
$active_group = "server1";
$active_group = "server2";
$active_record = TRUE;

..
[/quote]
Not to nitpick - you're setting the same variable name to two different values; the end result being that the active DB connection is server2; have you verified that you can connect to the server2 settings reliably?

You may also want to reconsider your db loading style. You're passing a variable and not using the default "$this" variable to reference CI.
http://www.ci.localhost/user_guide/datab...cting.html
#4

[eluser]akioshin[/eluser]
yes im sure that i can connect to server2 i can, sir jaytee what do you mean about db loading style?
#5

[eluser]jedd[/eluser]
When you say 'I'm sure' - do you mean that 'yes, i've tested this manually, including writing to the DB as my code is doing inserts rather than selects and that's a higher privelege requirement than just connecting', or do you mean 'I'm confident'?

Also, why (in your config) is the hostname for server2 a URL, and not, well, say, you know .. a hostname?
#6

[eluser]JayTee[/eluser]
[quote author="akioshin" date="1236164215"]yes im sure that i can connect to server2 i can, sir jaytee what do you mean about db loading style?[/quote]

If you look in the documentation, the method for loading a db connection is:
Code:
$this->load->database('name_of_connection')
Your code example from your original post:
Code:
function test()
    {
        
        $DB1 = $this->load->database($server1,TRUE);
        //$DB2 = $this->load->database('server2',TRUE);
        
        
        $DB1->query("INSERT INTO user(username) VALUES('yuuhi')");
        //$DB2->query("INSERT INTO user(username) VALUES('yuuhi')");
    }
From a testing perspective, I'd recommend starting with your first DB:
Code:
function test()
{
  $this->load->database('server1');
  $this->db->query("INSERT INTO user(username) VALUES('yuuhi')");
}
Then try it as a separate function with the other server connection - then start moving forward with the $DB1/$DB2 style and go forward from there.
#7

[eluser]akioshin[/eluser]
thanks anyway boss its now working, after trying with only one server it works and when i put the second server got the error again,and the answer is that i must first grant both servers,and now its working thanks guys




Theme © iAndrew 2016 - Forum software by © MyBB