Currently, I have my database configuration something like this:
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "my_username";
$db['default']['password'] = "my_password";
I actually want to connect to a different MySQL database hosted on a different server. Where would I find the hostname I need to use? Is there anything else I need to do besides changing the hostname?
Make an array of arrays, and always explicitly index them as needed:
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "foo";
$db['default']['password'] = bar";
$db['default']['database'] = "blablabla";
$db['development']['hostname'] = "localhost";
$db['development']['username'] = "boo";
$db['development']['password'] = "par";
$db['development']['database'] = "blablabladev";
$this->dbdev = $this->load->database('development', TRUE);
Usually, you'll be able to get this information from the same place you created the database. So if you used a control panel on your web host to create the database, it's likely you will be able to find the address by looking around the control panel's MySql section.
Some web hosts don't allow remote access to the database either, so be wary of that.
To answer your second question, yes, you will likely have to change the username and password, the new values should also be found in the area where you created the database.
Apologies if this is a very generic answer.
$db['default']['hostname'] = "new.host.name.com";
Also make sure that mysql on your new.host.name.com will accept external connection, by providing your localhost IP
Related
I setup the mysql database user_storage, and disabled the internal api, as per stated by documentation, but... then I'm lock out because no user was created...
How do I create the first admin user at the database?
Please make sure you have the following configuration in your deployment.toml. Also make sure you have created the user database correctly. https://ei.docs.wso2.com/en/latest/micro-integrator/setup/databases/setting-up-MySQL/#creating-the-databases
[user_store]
type = "database"
read_only = "false"
[[datasource]]
id = "WSO2_USER_DB"
url= "jdbc:mysql://localhost:3306/userdb"
username="root"
password="root"
driver="com.mysql.jdbc.Driver"
[realm_manager]
data_source = "WSO2_USER_DB"
[internal_apis.file_user_store]
enable = false
I am trying to connect to a MySQL database.
I found this script and I am trying to use it on my PC and web host, but it doesn't show any output.
Please have a look at this code. I am running perl at xampp.
#!C:\xampp\perl\bin\perl.exe
print "Content-type: text/html\n\n";
use DBI;
use strict;
my $driver = "localhost";
my $database = "data";
my $dsn = "DBI:$driver:database = $database";
my $userid = "root";
my $password = "";
my $dbh = DBI->connect($dsn, $userid, $password ) or die $DBI::errstr;
I am using the same database with PHP.
but it doesn't show any output.
Judging by the CGI header you're displaying, I assume that you're running this as a CGI program. In which case, it's no surprised that you're not seeing any output - you're not sending any output.
If you ran it as a command line program (and it's often a good idea to get stuff working on the command line before leaping into CGI programming), then you would see the "Content-Type" header. Alternatively, you could add some output to your program and see if that appears in your browser. Something simple like:
print 'It works!';
I'd also like to add, that CGI looks rather outdated these days and there are far better (by which I mean easier and more powerful) ways to write web applications with Perl. You might like to read CGI::Alternatives to get an idea of what is available.
Update:
I've just seen this question asked on Facebook (please don't cross-post without telling people) and I've noticed that your $driver variable is wrong. If you're connecting to MySQL, then $driver should be "mysql" (so that DBI loads "DBD::mysql").
A DSN for the MySQL driver looks like this
DBI:mysql:database=$database;host=$hostname;port=$port
where the host and port fields are optional. (The database is also optional, but you don't want to leave that out.) There are several more esoteric options too, but they're irrelevant here
But you're supplying
DBI:localhost:database = data
Which doesn't even specify a MySQL connection, so I'm not surprised if it doesn't work! I don't know whether the spaces are legal, but I would leave them out to keep in line with the documentation.
You should change that statement to
my $dsn = "DBI:mysql:database=$database;host=$driver"
You may remove ;host=$driver if you wish (why have you called the host name "driver"?) as localhost is the default. A DSN that specifies just a database name and uses the default for all the other fields may be contracted to just
my $dsn = "DBI:mysql:$database"
It may be easier to just write print statements at first to generate some output. You will want to print a MIME content type header of text/plain instead of text/html. Try print "$DBI::errstr\n" for now instead of die, as the latter writes to stderr which won't appear in your browser
you could add:
if ($dbh) {
print 'connect ok';
} else {
print 'connect failed';
}
I'm writing an application that has a functions.php file that a javascript file is accessing via Ajax. Currently I have each function connecting to the database, running queries, then closing the database. I know there has to be a more efficient way of doing this. I'd like to only input the database credentials once and then have all the functions use it. Whats the most efficient way to do this? I've read quite a few of the answers here on this topic but they're all different and I'm lost. Just point me in the right direction :)
Currently my functions are opening the database like this.
$db = new mysqli("hostname", "username", "password");
$db -> select_db("database name");
Or like this
mysql_connect("hostname", "username", "password");
mysql_select_db('database name') or die( "Unable to select database");
The simple answer is connection pooling. A connection pool is a pool of connections that are always connected to the database. You can set a high water mark and a low water mark for the number of connections.
When your application requests a connection from the pool it will then use one of the idle connections and reuse it. This is how you scale database connectivity.
You are using PHP so look at the following:
http://www.oracle.com/technetwork/topics/php/php-scalability-ha-twp-128842.pdf
http://php.net/manual/en/mysqlnd-ms.pooling.php
There is a lot of information around this on the net.
You can just check whether the database connection variable is already set:
if (!isset($db)) {
$db = new mysqli(...);
$db->select_db("database_name");
}
and similarly with mysql_connect (it returns a connection resource, so you can assign it to a variable, even though it's an optional argument to the other functions).
Another way is to use a function with a static variable:
function connect_db() {
static $db = new mysqli(...);
static $selected = $db->select_db("database_name");
return $db;
}
A third options is to connect to the database once at the beginning of the script, instead of in each function. Then either pass $db to each function, or access it with global $db;.
I need to connect to another mysql database I am doing following
$dsn = "{$dbdriver}://$vars[username]:$vars[password]#$vars[hostname]/$vars[database]";
$db2 = $this->EE->load->database($dsn, true);
$res = $db2->from('categories')->get()->result_array();
But I get error message Fatal error: Call to a member function result_array() on a non-object
That is third party script so db connection already made early. Then that script should connect to another db but 1st db connection already created
$dsn contains correct data. Why I get this error?
Thanks
Write parameters of 2nd database on confid/database.php also.
$db['second_db']['hostname'] = 'localhost';
$db['second_db']['username'] = 'foo';
...
And
$db2 = $this->EE->load->database('second_db', true);
I have figured it out. The query should contains additional param db_debug=1 so the dsn string should look like
$dsn = "mysql://$vars[username]:$vars[password]#$vars[hostname]/$vars[database]?db_debug=1";
If you read the codeigniter user guide you will find the answers easily. The heading is
Connecting to Multiple Databases
https://www.codeigniter.com/user_guide/database/connecting.html
I'm starting a new project built on Zend Framework. I know all about controllers, layouts and views. But I don't know how to add a MySQL resource.
Basically, I would like to have some model classes with getters and setters and for each, a resource class witch would handle MySQL queries. These resources classes need access to a DB class which performs the actual queries. The configuration for the DB would have to be in a separate file somewhere as either XML data, .ini or PHP array.
How can I obtain that? Where should I put each the files (right now, I have the default Zend directory structure)?
You dont need to create an instace of connection to database ,Zend does it automatically..just add these following to your config file
resources.db.adapter = "PDO_MYSQL"
resources.db.isDefaultTableAdapter = true;
resources.db.params.host = "yourserver"
resources.db.params.username = "username"
resources.db.params.password = "pwd"
resources.db.params.dbname = "dbname"
you can use it later
$DB = Zend_Db_Table_Abstract::getDefaultAdapter();
Zend comes with a set of DB classes that you can use. Documentation is here: http://framework.zend.com/manual/en/zend.db.html Before asking questions like this please do a bit of Googling.
[general]
db.adapter = PDO_MYSQL
db.params.host = server
db.params.username = username
db.params.password = password
db.params.dbname = dbname
$config = new Zend_Config_Ini(ROOT_DIR.'/application/config.ini', 'general');
$DB = Zend_Db::factory($config->db);
Zend_Db_Table::setDefaultAdapter($DB);
$DB = Zend_Db_Table_Abstract::getDefaultAdapter();