MYSQL集群的搭建
三台服务器实现主主从搭建:
第一台服务器ip:192.168.1.106
第二台服务器ip:192.168.1.195
第三台服务器ip:192.168.1.150
mysql主从的应用场景:数据分布 负载均衡 高可用和容错 备份
现在三台服务器都安装mysql开发环境 主从系统要一致 mysql版本和io磁盘
开始搭建:
首先得先建立一个复制账号,先进去mysql命令行,然后使用命令:
GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.*TO repl@'192.168.1.195' IDENTIFIED BY 'repl_1234';
注意:192.168.1.195这个ip为第二台从的ip。
然后就是手动同步数据:因为主服务器的一些库和表 在第二台服务器是没有的,所有要手动去导入到第二台服务器当中去:
可以使用mysqldump命令:比如我有一个最新的库phpwind,其他两台从服务器都没有,那么只需要把phpwind导出就可以了。
mysqldump -uroot --password=123456 --host=192.168.1.106 --single-transaction --flush-logs --master-data=2 -- phpwind> /tmp/alldb.sql
注意:要进去到主服务器,然后在导出此命令.
然后你会看到/tmp/alldb.sql以存在
然后把它压缩备份文件,并传到第二台服务器上面去:
gzip /tmp/alldb.sql
scp /tmp/alldb.sql.gz 192.168.52.130:/tmp/
然后看下第二台服务器是否有此文件.
然后修改第一台服务器的/etc/my.cnf配置文件并加入修改以下配置:
log-bin=mysql-bin
binlog_format=mixed
server-id = 106 # 此id必须唯一
binlog-ignore-db=mysql
binlog-ignore-db=test
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
然后保存 并退出
然后重启mysql
然后进入到mysql命令行 查看下主库的状态:
使用命令 show master status;
+------------------+----------+--------------+--------------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------------------------------+-------------------+
| mysql-bin.000029 | 658 | | mysql,test,information_schema,performance_schema | |
+------------------+----------+--------------+--------------------------------------------------+-------------------+
然后使用命令:show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000029
Position: 658
Binlog_Do_DB:
Binlog_Ignore_DB: mysql,test,information_schema,performance_schema
Executed_Gtid_Set:
1 row in set (0.00 sec)
ERROR:
No query specified
然后配置第二胎服务器192.168.1.195
先编辑配置/etc/my.cnf 并加以下配置:
log-bin=mysql-bin
binlog_format=mixed
server-id = 195
relay-log=mysql-relay-bin
master-info-repository=TABLE
relay-log-info-repository=TABLE
binlog-ignore-db=mysql
binlog-ignore-db=test
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
expire-logs-days=10
max_binlog_size = 10485760
配置完之后 重启数据库
然后进去mysql的命令行查看下slave状态:
show slave status;
show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.106
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000029
Read_Master_Log_Pos: 658
Relay_Log_File: mysql-relay-bin.000011
Relay_Log_Pos: 380
Relay_Master_Log_File: mysql-bin.000029
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 106
Master_UUID: 2d3ede7a-a972-11e6-a3c9-000c29893055
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
ERROR:
No query specified
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 658
Relay_Log_Space: 716
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
没有记录 需要主从设置才可以.
设置主从连接:
cd /tmp/
gunzip alldb.sql.gz
然后使用命令:
more alldb.sql |grep"CHANGE MASTER TO MASTER_LOG_FILE"
然后使用命令:
CHANGE MASTER TO MASTER_HOST='192.168.1.106',MASTER_USER='repl',MASTER_PASSWORD='repl_1234',MASTER_LOG_FILE='mysql-bin.000025',MASTER_LOG_POS=120;
然后把sql文件导入到mysql当中.
然后stop slave:
然后CHANGE MASTER TO MASTER_HOST='192.168.1.106',MASTER_USER='repl',MASTER_PASSWORD='repl_1234',MASTER_LOG_FILE='mysql-bin.000025',MASTER_LOG_POS=120;
然后启动slave start slave;
检验slave状态 show slave status\G 会发现一些字段信西正常
然后在第一台服务器的mysql命令行使用show full processlist;查看
mysql> show full processlist;
+----+------+---------------------+---------+-------------+------+-----------------------------------------------------------------------+-----------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+---------------------+---------+-------------+------+-----------------------------------------------------------------------+-----------------------+
| 1 | root | localhost | phpwind | Query | 0 | init | show full processlist |
| 19 | repl | 192.168.1.195:12596 | NULL | Binlog Dump | 75 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL |
+----+------+---------------------+---------+-------------+------+-----------------------------------------------------------------------+-----------------------+
然后可以在主服务器创建一个表 可查看下 表是否已经同步到了第二台服务器 经过查看已经同步!!

浙公网安备 33010602011771号