mysql简单复制服务搭建

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql简单复制服务搭建相关的知识,希望对你有一定的参考价值。

1.安装mysql源(centos7中默认是不包含mysql源)
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm 
yum install mysql-server

2.配置mysql-master
在my.cnf中添加:
prot=3306
log-bin=master-bin
log-bin-index=master-bin.index
server-id=1

3.重启mysql后新增复制用户
create user repl_user;
grant replication slave on *.* to repl_user identified by xyzzy;

4.配置备机mysql
在my.cnf中添加下面几行
relay-log=slave-bin
relay-log-index=slave-bin.index
server-id=2

5.发现备机无法连接到master服务器
nmap -v materIP  发现没有打开ssh的22端口
故打开:
firewall-cmd --zone=public --add-port=22/tcp --permanent

6.slave数据库报错
有报错信息:

mysql> CHANGE MASTER TO MASTER_HOST=192.168.52.129,

-> MASTER_USER=repl,

-> MASTER_PASSWORD=repl_1234,

-> MASTER_LOG_FILE=mysql-bin.000141,

-> MASTER_LOG_POS=120;

ERROR 1794 (HY000): Slave is not configuredor failed to initialize properl
y. You must at least set --server-id to enableeither a master or a slave. 
Additional error messages can be found in the MySQLerror log.

mysql>SHOW VARIABLES LIKE server_id;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 0     |
+---------------+-------+
1 row in set (0.00 sec)

id没有生效
配置里面有[mysqld]和[mysqld_safe],新增的配置文件放的位置不一样,将新增配置
放到mysqld下试试,ok成功。


mysql> show variables like server_id;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 2     |
+---------------+-------+
1 row in set (0.00 sec)

mysql> change master to 
    -> master_host=192.168.122.1
    -> ,master_port=3306,
    -> master_user=repl_user,
    -> master_password=xyzzy;
Query OK, 0 rows affected, 2 warnings (0.05 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
测试复制是否成功:
在master上创建一个数据库:
mysql> create database cydb;
Query OK, 1 row affected (0.00 sec)
在slave上查看是否有此数据库:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cydb               |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
至此,简单的复制环境搭配成功。

 

以上是关于mysql简单复制服务搭建的主要内容,如果未能解决你的问题,请参考以下文章

MySQL主从复制的简单搭建

MySQL主从复制的简单搭建

MySQL多源复制搭建

mysql5.7 主从复制简单环境搭建以及开启半同步复制等配置

mysql5.7 主从复制简单环境搭建以及开启半同步复制等配置

mysql5.7 主从复制简单环境搭建以及开启半同步复制等配置