简体   繁体   中英

How to config yii2 db connection with a mysql ndb cluster?

I have a mysql cluster with 2 sql nodes, I want to use yii2 to connect this cluster without more coding. It's perfect with Elastich search cluster with a thirth party doing like the bellow:

  'elasticsearch' => [
       'class' => 'yii\elasticsearch\Connection',
       'nodes' => [
            ['http_address' => '127.0.0.1:9200'],
            // configure more hosts if you have a cluster
        ],
        'dslVersion' => 7,
   ],

How can we do this with a mysql cluster?

after a range time for testing, I have an easy solution want to share for everyone needed.

We just use Yii db replication config, the master's array config is the nodes of mysql ndb cluster, that't it!

[
    'class' => 'yii\db\Connection',

    // common configuration for masters
    'masterConfig' => [
        'username' => 'sqlnodeuser',
        'password' => 'sqlnodepass',
        'attributes' => [
            // use a smaller connection timeout
            PDO::ATTR_TIMEOUT => 10,
        ],
    ],

    // list of master configurations
    'masters' => [
        ['dsn' => 'dsn for ndb sql node 1'],
        ['dsn' => 'dsn for ndb sql node 2'],
    ],
]

from yii2 official document: https://www.yiiframework.com/doc/guide/2.0/en/db-dao

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM