mysql5.7 主从

一、安装mysql5.7

1.1 下载mysql源安装包

wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

1.2 安装mysql源

yum install mysql57-community-release-el7-8.noarch.rpm

1.3 检查mysql源是否安装成功

yum repolist enabled | grep "mysql.*-community.*"

1.4 安装

yum install mysql-community-server

1.5 启动mysql服务

systemctl start mysqld #启动
systemctl status mysqld #状态

1.6 开机启动

systemctl enable mysqld
systemctl daemon-reload

1.7 修改root本地登陆密码

grep 'temporary password' /var/log/mysqld.log
ALTER USER 'root'@'localhost' IDENTIFIED BY 'TabY_opaw5';

二、主从配置

2.1 主库建立同步帐号

    咱们要在主数据库里建立一个帐号,而且该帐号要授予 REPLICATION SLAVE 权限。mysql

create user 'repl'@'%' identified by 'TabX_opBo5';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';

2.2 配置主数据库

    要主数据库,你必需要启用二进制日志(binary logging),而且建立一个惟一的Server ID,这步骤可能要重启MySQL。 
主服务器发送变动记录到从服务器依赖的是二进制日志,若是没启用二进制日志,复制操做不能实现(主库复制到从库)。 
    复制组中的每台服务器都要配置惟一的Server ID,取值范围是1到(232)−1,你本身决定取值。 
配置二进制日志和Server ID,你须要关闭MySQL和编辑my.cnf或者my.ini文件,在 [mysqld] 节点下添加配置。 编辑my.cnf:sql

vi /etc/my.cnf
server-id = 1
log_bin = master-bin
log_bin_index = master-bin.index
binlog_do_db = test
binlog_ignore_db = mysql

    备注:server-id 服务器惟一标识,log_bin 启动MySQL二进制日志,binlog_do_db 指定记录二进制日志的数据库,binlog_ignore_db 指定不记录二进制日志的数据库。 
    重启mysql:数据库

systemctl restart mysqld

    登录主数据库,查看数据库状态:服务器

mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000003 |      154 | test         | mysql            |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set

    注意:master-bin.000003和154这两个值要记录下来ide

2.3 配置从数据库测试

    编辑my.cnf:spa

vi /etc/my.cnf

    添加rest

server-id = 2
relay-log = slave-relay-bin
relay-log-index = slave-relay-bin.index

    重启mysql日志

systemctl restart mysqld

    在slave服务器中登录mysql,链接master主服务器数据库code

change master to master_host='主库ip', master_port=3306, master_user='repl', master_password='TabX_opBo5', master_log_file='master-bin.000003', master_log_pos=154;

    启动slave:

start slave;
mysql> show slave status
;
+----------------------------------+---------------+-------------+-------------+---------------+-------------------+---------------------+------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
| Slave_IO_State                   | Master_Host   | Master_User | Master_Port | Connect_Retry | Master_Log_File   | Read_Master_Log_Pos | Relay_Log_File         | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID                          | Master_Info_File           | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State                                | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version |
+----------------------------------+---------------+-------------+-------------+---------------+-------------------+---------------------+------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
| Waiting for master to send event | 172.31.150.25 | root        |        3306 |            60 | master-bin.000003 |                 154 | slave-relay-bin.000010 |           321 | master-bin.000003     | Yes              | Yes               |                 |                     |                    |                        |                         |                             |          0 |            |            0 |                 154 |             528 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                     0 | No                            |             0 |               |              0 |                |                             |                1 | fa758afe-7a75-11e8-988c-00163e058649 | /var/lib/mysql/master.info |         0 | NULL                | Slave has read all relay log; waiting for more updates |              86400 |             |                         |                          |                |                    |                    |                   |             0 |                      |              |                    |
+----------------------------------+---------------+-------------+-------------+---------------+-------------------+---------------------+------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
1 row in set

三、测试

    在主库的test库中新建表和添加数据库,会自动同步到slave库中。

相关文章
相关标签/搜索