Can't connect to external database with Codeigntier - mysql
I want Codeigntier to connect to a MySQL database on another server (not localhost). How do I configure this in the config/database.php file?
I set the hostname to the ip address with the proper username/password but it's not working.
The error I get says:
A Database Error Occurred: Unable to connect to your database server using the provided settings.
Heru, it seems like you simply want to change the current DB configuration from localhost to your live server. Do this, open up your database.php file in the application/config folder. Then find these lines.
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = ''; // might be empty or might be full
$db['default']['database'] = 'your_database';
and change it to this . . .
$db['default']['hostname'] = 'your_host_name';
$db['default']['username'] = 'your_user_name';
$db['default']['password'] = 'your_password';
$db['default']['database'] = 'your_new_database';
depending if your on a shared host or not and other factors. Your host may provide you with a new host name , may require a password and may provide you with a pre-configured database name. Look into the host provider for these pieces of information.
if that doesn't work . . .
You could also try to change it to this which finds your servers IP and converts it to a named address, but your database may or may not be sitting on a different address. But it's worth a shot.
$hostIP = $_SERVER['SERVER_ADDR'];
(string)$hostname = gethostbyaddr($hostIP);
$db['default']['hostname'] = $hostname;
$db['default']['username'] = 'your_user_name';
$db['default']['password'] = 'your_password';
$db['default']['database'] = 'your_new_database';
also if your confident of your database IP, change the above
(string)$hostname = gethostbyaddr($hostIP)
to
(string)$hostname = gethostbyaddr('10.11.12.123'); // with your IP #
This strikes me as a database permissions issue, not a CI issue. You're probably not authorized to connect from your IP. What happens when you run this on some random PHP page:
mysql_connect($server, $un, $pw) or die( mysql_error());
To fix this, simply run:
GRANT SELECT, DELETE, UPATE, INSERT ON $dbname.% TO $un#% IDENTIFIED BY ('$pw')
Triq,
You can add another set of database config to your config/database.php file (on top of your default set) like the following (you can change dbwrite to other name that you like):
$db['dbwrite']['hostname'] = "[other db host]";
$db['dbwrite']['username'] = "[other db username]";
$db['dbwrite']['password'] = "[other db password]";
$db['dbwrite']['database'] = "[other db name]";
$db['dbwrite']['dbdriver'] = "mysql";
$db['dbwrite']['dbprefix'] = "";
$db['dbwrite']['pconnect'] = FALSE;
$db['dbwrite']['db_debug'] = FALSE;
$db['dbwrite']['cache_on'] = FALSE;
$db['dbwrite']['cachedir'] = "";
$db['dbwrite']['char_set'] = "utf8";
$db['dbwrite']['dbcollat'] = "utf8_general_ci";
Then, you can pull this db reference using the following code:
$dbw = $this->load->database('dbwrite',TRUE);
$dbw->query(xxx); // this will execute the query on the other db
Make sure dbwrite is the same name that you are using on your other db config. $this refers to $CI instance.
You can easily Crated multiple database connection using codeigniter
$db['default']['hostname'] = "localhost:3306";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "buy599_erp";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRU`enter code here`E;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
$db['magento6']['hostname'] = "localhost:3306";
$db['magento6']['username'] = "root";
$db['magento6']['password'] = "";
$db['magento6']['database'] = "magento6";
$db['magento6']['dbdriver'] = "mysql";
$db['magento6']['dbprefix'] = "";
$db['magento6']['pconnect'] = TRUE;
$db['magento6']['db_debug'] = TRUE;
$db['magento6']['cache_on'] = FALSE;
$db['magento6']['cachedir'] = "";
$db['magento6']['char_set'] = "utf8";
$db['magento6']['dbcollat'] = "utf8_general_ci";
$db['magento6stalkbuylove']['hostname'] = "localhost:3306";
$db['magento6stalkbuylove']['username'] = "root";
$db['magento6stalkbuylove']['password'] = "";
$db['magento6stalkbuylove']['database'] = "magento6stalkbuylove";
$db['magento6stalkbuylove']['dbdriver'] = "mysql";
$db['magento6stalkbuylove']['dbprefix'] = "";
$db['magento6stalkbuylove']['pconnect'] = TRUE;
$db['magento6stalkbuylove']['db_debug'] = TRUE;
$db['magento6stalkbuylove']['cache_on'] = FALSE;
$db['magento6stalkbuylove']['cachedir'] = "";
$db['magento6stalkbuylove']['char_set'] = "utf8";
$db['magento6stalkbuylove']['dbcollat'] = "utf8_general_ci";
Related
How to fix "Unable to select the specified database" in xampp and code igniter
I'm having a problem in codeigniter message "Unable to select the specified database Filename: E:\xampp\htdocs\marketingtools\system\database\DB_driver.php" My database is existing and the credentials are correct because most of the time the program runs properly and then suddenly the error occur I don't know what is happening because i did not change anything in xampp settings and codeigniter settings but the error happens and ill wait for a while the program runs properly again. here is the sample of my code $active_group = 'default'; $active_record = TRUE; $db['default']['hostname'] = 'localhost'; $db['default']['username'] = 'root'; //aims_prog $db['default']['password'] = ''; //yqUMaEBrvM8j6emD $db['default']['database'] = 'marketingtools'; $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;
Refused connection to database in production but not local
I am using codeigniter 3.0 and I am trying to connect to a remote data base. I can successfully connect from my personal pc using a remote mysql user. However when I put the site in production mode on my linux cpanel hosting it cannot connect. Are there any known issues with this type of connection versus a local one? My webhost is linux (cpanel) and my database is mysql and lives on a windows vps. Here's my connection info: $db['default']['username'] = "root"; $db['default']['password'] = "mysql"; $db['default']['database'] = "cms"; $db['default']['hostname'] = "localhost"; $db['default']['dbdriver'] = "mysqli"; $db['default']['dbprefix'] = ""; $db['default']['pconnect'] = FALSE; $db['default']['db_debug'] = TRUE; $db['default']['cache_on'] = FALSE; $db['default']['cachedir'] = ""; $db['default']['char_set'] = "utf8"; $db['default']['dbcollat'] = "utf8_general_ci"; /** * * THIS IS YOUR GAME DATABASE CONENCTION INFO * **/ $db['conquer']['username'] = "root"; $db['conquer']['password'] = "mysql"; $db['conquer']['database'] = "cq"; $db['conquer']['hostname'] = "104.238.133.31:3306"; $db['conquer']['dbdriver'] = "mysqli"; $db['conquer']['dbprefix'] = ""; $db['conquer']['pconnect'] = FALSE; $db['conquer']['db_debug'] = TRUE; $db['conquer']['cache_on'] = FALSE; $db['conquer']['cachedir'] = ""; $db['conquer']['char_set'] = "utf8"; $db['conquer']['dbcollat'] = "utf8_general_ci"; $active_group = "default"; $active_record = TRUE; This works perfect in development I am using an IP for the host name and my mysql user on the vps has remote connections enabled.
In linux base server it use localhost for host name. but in windows server there is an ip address $db['default']['hostname'] = '';//change ip to here $db['default']['username'] = '';//input relevant username $db['default']['password'] = '';//input relevant PW $db['default']['dbdriver'] = '';//input relevant Driver
How to Insert data into some other database in codeigniter
I have a requirement I need to insert data into two tables of different databases.. $insert_query = 'INSERT INTO table1 (col1, col2) values(1, 2)'; $insert_otherdb_query= 'INSERT INTO otherdb.table1 (col1, col2) values(1, 2)'; $this->db->query($insert_query); //works fine $this->db->query($insert_otherdb_query); //doesn't work Error Table 'mydb.otherdb.table1' doesn't exist It does not ignore the mydb which is my default database... Any suggesstions ?
You have to define a second set of database parameters. CI isn’t developed to really have two DB connections though, it is more for swapping test and production dbases. That said there are some tricks around it. So first define a second set of DB info like so: /* FORUM */ $active_group = "forum"; $active_record = TRUE; $db['forum']['hostname'] = "xxxxx"; $db['forum']['username'] = "xxxxx"; $db['forum']['password'] = "xxxxx"; $db['forum']['database'] = "xxxxx"; $db['forum']['dbdriver'] = "mysql"; $db['forum']['dbprefix'] = ""; $db['forum']['pconnect'] = TRUE; $db['forum']['db_debug'] = TRUE; $db['forum']['cache_on'] = FALSE; $db['forum']['cachedir'] = ""; $db['forum']['char_set'] = "utf8"; $db['forum']['dbcollat'] = "utf8_general_ci"; /* TEST SITE */ $active_group = "default"; $active_record = TRUE; $db['default']['hostname'] = "xxxxx"; $db['default']['username'] = "xxxxx"; $db['default']['password'] = "xxxxx"; $db['default']['database'] = "xxxxx"; $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"; Your active db will be the one you defined LAST. Once you have done this you can manually connect to the second one (or put it in MY_Controller if you always need to). You can then load your second database like so: $this->other_db= $this->CI->load->database('forum', TRUE); Access dbase 1 with $this->db and dbase 2 with $this->other_db (or whatever you called it). thank you
Codeigniter internally connect to the database using settings from the database.php config file. Also you are using the same db object which has a connection to the first db, it will never work. You may have several options to do this: 1) You can use Web services. Create a web service for the other database using CI and send data to that webservice. On the webservice side, insert the data in the database table. This is the most good way to use, as both code is separated and communication is only done by the web services. You can use SOAP or REST web services. 2) You can connect to other databases using direct php code. Use mysqli to connect to other db and run your queries in plane PHP code. Please note that doing so, you will be not able to use any db feature of the CI. In my opinion, the first option will be a good thing, if there are lots of db operations on the second database, else use second. Thank you
I got a better solution. $dsn = 'mysql://user:password#localhost/db1'; $this->db1 = $this->load->database($dsn, true); $dsn = 'mysql://user:password#localhost/db2'; $this->db2= $this->load->database($dsn, true); $dsn = 'mysql://user:password#localhost/db3'; $this->db3= $this->load->database($dsn, true); Usage $this->db1 ->insert('tablename', $insert_array); $this->db2->insert('tablename', $insert_array); $this->db3->insert('tablename', $insert_array);
To connect manually to a desired database you can pass an array of values: $config['hostname'] = "localhost"; $config['username'] = "myusername"; $config['password'] = "mypassword"; $config['database'] = "mydatabase"; $config['dbdriver'] = "mysql"; $config['dbprefix'] = ""; $config['pconnect'] = FALSE; $config['db_debug'] = TRUE; $config['cache_on'] = FALSE; $config['cachedir'] = ""; $config['char_set'] = "utf8"; $config['dbcollat'] = "utf8_general_ci"; $this->load->database($config, true); Access your table as usual without using database name. Hope this helped you. For more info, visit: http://ellislab.com/codeigniter/user-guide/database/connecting.html Any problems, let me know
using localhost MySql from online Codeigniter
I have a Codeigniter project on a machine and it runs as public access. However the MySql database it uses is accessible only through localhost. When I run the application, I get A Database Error Occurred Unable to connect to your database server using the provided settings. Filename: core/Loader.php Line Number: 346 I follwed the instructions mentioned here but I am still getting the same error Try 1. print_r($params) and print_r($active_record); 2. if the $params are still correct then simplify $active_record 3. echo mysql_error(); 4. $db[‘default’][‘db_debug’] = true/false 5. $db[‘default’][‘stricton’] = true/false; [edited] I have set the parameters as $db['default']['hostname'] = 'mysql12.freehostia.com'; $db['default']['username'] = 'username'; $db['default']['password'] = 'password'; $db['default']['database'] = 'myDB'; $db['default']['dbdriver'] = 'mysql'; $db['default']['dbprefix'] = ''; $db['default']['pconnect'] = false; $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;
CI Database Name
I have a problem in connecting database in Codeigniter , cause my database name is "database-db_sample", so I'm wondering if the -(hypen) in my db name is the reason why I got an error, This is the error i got A Database Error Occurred Unable to select the specified database: database-db_sample Can someone tell me what's I had done wrong, This is my db config $db['online']['hostname'] = "localhost"; $db['online']['username'] = "root"; $db['online']['password'] = ""; $db['online']['database'] = "database-db_sample"; $db['online']['dbdriver'] = "mysql"; $db['online']['dbprefix'] = ""; $db['online']['pconnect'] = TRUE; $db['online']['db_debug'] = TRUE; $db['online']['cache_on'] = FALSE; $db['online']['cachedir'] = ""; $db['online']['char_set'] = "utf8"; $db['online']['dbcollat'] = "utf8_general_ci"; $db['default']['swap_pre'] = ''; $db['default']['autoinit'] = TRUE; $db['default']['stricton'] = FALSE;
Your problem is either you don't have proper permissions set to root or mySQL doesn't like your use of hyphens. So if you want to start new from phpMyAdmin. Start by going to your localhost->phpMyAdmin->privileges tab->add new user. Type in a username and use localhost then check the box for "create a database with same name...". That's it! Just don't use hyphens. Your database will have the same name as your username with all proper permissions.