How to set up HyperDB for WordPress website? - database

I have a large database which I want to split into several databases but connected to my WordPress site. I searched through internet and came to a solution of using HyperDB class which is provided by WordPress Codex. I downloaded the files and I tried like below
$wpdb->add_database(array(
'host' => 'localhost', // If port is other than 3306, use host:port.
'user' => 'root',
'password' => '',
'name' => 'partitioning',
));
/**
* This adds the same server again, only this time it is configured as a slave.
* The last three parameters are set to the defaults but are shown for clarity.
*/
$wpdb->add_database(array(
'host' => 'localhost', // If port is other than 3306, use host:port.
'user' => 'root',
'password' => '',
'name' => 'slave',
'write' => 0,
'read' => 1,
'dataset' => 'global',
'timeout' => 0.2,
));
I am using xampp for the development version. After WordPress site installation I put the db-config.php along with wp-config.php file directory and db.php in wp-content folder. Doing this I get the blank page.
Can any one elaborate the process step by step how to set up the database and HyperDB scripts? I mean how to make the slave database or HyperDB will automatically make the slave database? How can I split any table to slave database? The whole process I mean.

If you're building a high-volume Wordpress site, and the database is the bottleneck (as it often is), HyperDB is absolutely the correct Wordpress solution. HyperDB is authored by Automattic, is used on Wordpress.com, and is designed as a proper Wordpress drop-in.
HyperDB supports MySQL read-replicas and partitioning, and which you use depends on your use-case.
These configuration options are actually reasonably well documented in the HyperDB Config file, which looks like this:
https://plugins.trac.wordpress.org/browser/hyperdb/trunk/db-config.php
This won't precisely work for your situation; you'll have to adjust this to fit your setup.
// This is the default database configuration
//
$wpdb->add_database(array(
'host' => 'localhost',
'user' => 'root',
'password' => '',
'name' => 'regular_db',
));
// This is your secondary database.
//
$wpdb->add_database(array(
'host' => 'localhost',
'user' => 'root',
'password' => '',
'name' => 'product_db',
'dataset' => 'products',
));
// This magic detects which database table is being read from/written to, and switches
// hyperdb connection based on that.
$wpdb->add_callback('my_db_product_detector');
function my_db_product_detector($query, $wpdb) {
// This makes the assumption that you've only got one relevant table, wp_products.
// Update this to whatever your table name(s) are.
if ( preg_match('^wp_products$', $wpdb->table) ) {
return 'products';
}
}
To begin with, to test that this works on your existing database, you should change product_db to regular_db, and go from there.
At this point, you have architectural decisions to make. If you simply want to reduce the size of your DB, then this now writes your products to a second DB. Alternatively, you might have a second database server entirely to share the load across different databases.
Creating a read replica is also worth considering, but that depends on what ecosystem you're working with. If you're using AWS, then RDS will create read replicas quite simply.

Related

Codeigniter cannot connect to database

What should I put for codeigniter database.php hostname?
I try putting "localhost","localhost:8080" but I cannot connect.
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root#localhost',
'password' => '',
'database' => 'hnc-cms',
The hostname is localhost.
But you have to set base url in config.php
Like this
$config['base_url'] = 'http://'. $_SERVER['HTTP_HOST'].'/yourpathname/';
turns out I found the solution and I not sure whats the issue that causes this.
When I uses window environment, I can put localhost as my hostname but when I changed to Mac, I need to specify the port number for mysql.
As for my case is localhost:3306, still curious why it happen.

Laravel 5 How to configure the Queue database driver to connect to a non-default database?

In Laravel 5.1, we can set the Queue connection configurations in config/queue.php.
QUEUE_DRIVER=database
'database' => [
'driver' => 'database',
'table' => 'jobs',
'queue' => 'default',
'expire' => 60,
],
However, it will only use the default database connection in config/database.php.
If I have 2 database, 1 default database mysql1 in localhost, and 1 database mysql2 in a remote server, and the Queue jobs table is in the remote database mysql2, how can I configure the Queue database driver to use the remote mysql2 database? Please note that the main App is using the default database in localhost.
You can use the 'connection' parameter in queue.php to set the correct database connection ( from the ones you've defined in database.php ).
'database' => [
'connection' => 'mysql2',
'driver' => 'database',
'table' => 'jobs',
'queue' => 'default',
'expire' => 60,
],
I was looking for the same thing and found it in the source code.
NOTE: This will not only read the jobs from this connection ( when running the queue ), but also write them to this connection ( when dispatching a new Job ) .
The best answer here did not work for me, not to say it isn't the best answer for a different issue than mine. My issue was that Laravel did not cache my config settings.
After going into file \config\queue.php and changing the default driver...
'default' => env('QUEUE_DRIVER', 'database'),
The queue was still running on the sync driver.
I then checked the file...
\bootstrap\cache\config.php
Around line 30 I saw this...
'queue' =>
array (
'default' => 'sync',
...but to connect to the database, it should be...
'queue' =>
array (
'default' => 'database',
This resolved the issue...
php artisan config:cache
Running the config:cache commmand rewrites the config.php file to the current driver settings.
You can set the $connection variable in the model. Note that this will only affect Eloquent queries and will not work for the Fluid Query Builder.
class Jobs extends Eloquent {
protected $connection = "database2"
}
This would of course require you to have a 2nd named connection in your config/database.php file that is 'database2' => [...].

How to create more than 2 connection with same database in cakephp

I want to have more than one connection to my database.
i am using persistent => false and 5 ajax calls has been made which fetch data, and now each query is waiting for prvs to complete.
I want all these 5 queries run in parallel
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'user' => 'cluster',
'password' => '',
'database' => 'cluster',
'prefix' => '',
);
EDIT:-
I got my problem, the problem is blocking connections are being made, how to make non blocking request for my requests..?
I think you want to excute a set of query simultaneously, I think you need to connect databse dynamically, Just see the bellow link, may be help you
https://stackoverflow.com/a/13224580/2460470
Just copy and paste your connection and name it different and use that name in your models useDbConfig property, that's all.
But what you say doesn't make much sense. Each of the 5 AJAX calls creates a complete new request and runs through the whole php stack before it even reaches the DB. The way you want to "optimize" this just doesn't make sense.

how use Acl to limit user access

I have same login for 3 different type of users, admin/client/user, everyone has different layout and privileges but the issue is after login every one is able to access all pages. after a lot of search on Google and stack overflow i decided to use Acl. I created tables in database via right procedure recommended by Acl given here AccessControlList. I set permission in following way. But still all type of user are accessing all pages.
$this->Acl->allow(
array('model' => 'User', 'foreign_key' => 1),
'admins'
);
$this->Acl->allow(
array('model' => 'User', 'foreign_key' => 2),
'clients'
);
any help will be appreciated.

Multiple sessions for different instances of Cakephp in the same domain

Did you know that if you run multiple instances of the same application in Cakephp in the same domain, they will share the same Session? For example, suppose you have instances running at:
www.example.com/instance1 and www.example.com/instance2
If you login in the first instance and access instance2, you’ll see that you will already be logged in. This happens because Cakephp, per default, uses the PHP Session storage mechanism.
If this is not the behaviour you expect, Cakephp allows you to choose from three options for the Session handling method: php (default), cake and database. The current method is stored in the Session.save variable in app/config/core.php.
Changing the method from php to cake will make Cakephp store the Session variables in the app/tmp/sessions directory. If you do it, remember to create and give the appropriate permissions to this directory.
Voilá, that’s all you need to do have separate Sessions for each of your Cakephp instances.
Please open the core.php & change the application cookie path then session will be store according to application cookie path
For www.example.com/instance1
Configure::write('Session', array(
'defaults' => 'database',
'ini' => array(
'session.cookie_path' => '/instance1',
),
'cookie' => 'instance1',
'cookieTimeout' => 0,
'checkAgent' => false
));
For www.example.com/instance2
Configure::write('Session', array(
'defaults' => 'database',
'ini' => array(
'session.cookie_path' => '/instance2',
),
'cookie' => 'instance2',
'cookieTimeout' => 0,
'checkAgent' => false
));

Resources