zoukankan      html  css  js  c++  java
  • YII配置mysql读写分离

    Mysql 读写分离

    YIi 配置

    <?php
    
    return [
        'class' => 'yiidbConnection',
        'masterConfig' => [
                   // 'dsn' => 'mysql:host=localhost;dbname=studyyii',
                    'username' => 'root',
                    'password' => 'caesar',
                    'charset' => 'utf8',
                    'attributes' => [
                        // use a smaller connection timeout
                        PDO::ATTR_TIMEOUT => 10,
                    ],
        ],
    
        // 配置主服务器组
        'masters' => [
            ['dsn' => 'mysql:host=172.31.80.31;dbname=studyyii'],
          //  ['dsn' => 'dsn for master server 2'],
        ],
    
        // 配置从服务器
        'slaveConfig' => [
            'username' => 'root',
            'password' => 'root',
            'charset' => 'utf8',
            'attributes' => [
                // use a smaller connection timeout
                PDO::ATTR_TIMEOUT => 10,
            ],
        ],
        // 配置从服务器组
        'slaves' => [
            ['dsn' => 'mysql:host=localhost;dbname=studyyii'],
        ],
    ];

    Linux mysql master 

    My.cnf:

    server_id = 1   #从服务器标识,范围1---2E32-1
    
    log_bin = mysql_bin
    
    binlog_do_db = studyyii
    
    binlog_do_db = mysql

    #mysql service restart

    windows mysql slave

    mysql.ini:

    [mysqld]
    
    port=3306
    
    server-id = 2
    
    replicate-do-db = study

    重启mysql 

    登陆mysql

    mysql> stop slave;

    mysql> change master to master_host='192.168.1.200',master_user='root',master_password='123456';

    mysql> start slave;

    mysql> show slave statusG;

    查看这两项是否为YESyes为正常。

    Slave_IO_Running: Yes

    Slave_SQL_Running: Yes

    Ok 了 读写分离,主从同步

    Mysql –master linux-slave

    My.ini:

    Server-id=1
    
    relay-log=relay-bin
    
    relay-log-index=relay-bin-index
    
    binlog-do-db = studyyii

    重启服务器

    登陆mysql

    Mysql –u root –p

    在主数据库建立slave连接的权限:

    mysql> grant replication slave,reload,super on *.* to 'root'@'172.31.80.31' iden

    tified by 'root';

    root为连接的账号,IP 和密码)

    Show master status;

    记住,以前我没设置权限,显示的position120 一直因为没开权限么启动

    至此,Windows mysql master 设置完毕

    二、打开Linux mysql

     Vi /etc/my.cnf #根据你服务器MySQL的设置来编辑,编辑mysql的配置文件

    MySQLd下面添加配置:

    wq 保存退出

    Service mysql restart  #我的mysql5.6.23 已经添加为系统服务,其它一下版本是mysqld ,反正就是重启mysql,根据自己的配置来

    Mysql –u root –p #连接mysql

    设置连接:

    change master to master_host='172.31.80.26',master_user='root',master_password='root', master_log_file='mysql-bin.000014',mas14',master_log_pos=344;

    #连接的账户 IP 密码为mastermysql的。Master_log_file log_pos 为前期Windows mysqlshow master status 中看到。

    出现OK之后

    Start slave;启动slave

    查看状态

    Show slave statusG;

    slave_IO_Running Slave_SQL_Running 都为yes  说明配置成功。

    如果IO_Running connecting 说明一直在连接,你的配置是有问题的,

    检查master 是否有错误,检查slave连接master是否有问题,

    然后你就看到更改数据 数据库可以同步了。

  • 相关阅读:
    83. Remove Duplicates from Sorted List
    35. Search Insert Position
    96. Unique Binary Search Trees
    94. Binary Tree Inorder Traversal
    117. Populating Next Right Pointers in Each Node II
    116. Populating Next Right Pointers in Each Node
    111. Minimum Depth of Binary Tree
    169. Majority Element
    171. Excel Sheet Column Number
    190. Reverse Bits
  • 原文地址:https://www.cnblogs.com/already/p/5063048.html
Copyright © 2011-2022 走看看