Centos7 mysql 5.6 数据库主主复制

作者: print("") 分类: linux 发布时间: 2018-05-25 13:02

emmmm 瞎来没事做。想着就写一篇文章吧

主机系统是:centos7.4 64

内存:2G

CPU:4核心

具体怎么编译mysql 就不用说了吧.emmmmmmm

主的配置文件

[client]
#password   = your_password
port        = 3306
socket      = /tmp/mysql.sock

[mysqld]
binlog_cache_size = 64K
thread_stack = 256K
join_buffer_size = 1024K
query_cache_type = 1
max_heap_table_size = 128M
port        = 3306
socket      = /tmp/mysql.sock
datadir = /www/server/data
skip-external-locking
performance_schema_max_table_instances=400
table_definition_cache=400
key_buffer_size = 128M
max_allowed_packet = 100G
table_open_cache = 128
sort_buffer_size = 768K
net_buffer_length = 8K
read_buffer_size = 768K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 64
query_cache_size = 128M
tmp_table_size = 128M

explicit_defaults_for_timestamp = true
#skip-networking
#skip-name-resolve
max_connections = 100
max_connect_errors = 100
open_files_limit = 65535
sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

log-bin=mysql-bin
binlog_format=mixed
server-id   = 1
expire_logs_days = 10
slow_query_log=1
slow-query-log-file=/www/server/data/mysql-slow.log
long_query_time=3
#log_queries_not_using_indexes=on

#loose-innodb-trx=0
#loose-innodb-locks=0
#loose-innodb-lock-waits=0
#loose-innodb-cmp=0
#loose-innodb-cmp-per-index=0
#loose-innodb-cmp-per-index-reset=0
#loose-innodb-cmp-reset=0
#loose-innodb-cmpmem=0
#loose-innodb-cmpmem-reset=0
#loose-innodb-buffer-page=0
#loose-innodb-buffer-page-lru=0
#loose-innodb-buffer-pool-stats=0
#loose-innodb-metrics=0
#loose-innodb-ft-default-stopword=0
#loose-innodb-ft-inserted=0
#loose-innodb-ft-deleted=0
#loose-innodb-ft-being-deleted=0
#loose-innodb-ft-config=0
#loose-innodb-ft-index-cache=0
#loose-innodb-ft-index-table=0
#loose-innodb-sys-tables=0
#loose-innodb-sys-tablestats=0
#loose-innodb-sys-indexes=0
#loose-innodb-sys-columns=0
#loose-innodb-sys-fields=0
#loose-innodb-sys-foreign=0
#loose-innodb-sys-foreign-cols=0

default_storage_engine = InnoDB
innodb_data_home_dir = /www/server/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /www/server/data
innodb_buffer_pool_size = 256M
innodb_log_file_size = 64M
innodb_log_buffer_size = 368M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 120
innodb_max_dirty_pages_pct = 90
innodb_read_io_threads = 1
innodb_write_io_threads = 1

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 32M
sort_buffer_size = 768K
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

从节点一抹一样只是Server_id 修改了一下而已

在主备份一下数据库。全备吧。这样也就行

#mysqldump –uroot –p’123456’ --all-databases >liang.sql

scp 发送到对端服务器

从服务器把文件导入进去

mysql –uroot –p’123456’   <liang.sql

进入主服务器。添加一个用户授权

mysql>grant replication slave on *.* to  rep@'192.168.132.%' identified by '123456';

这里给主服务器锁表

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.08 sec)

查看一下用户

mysql> select user,host from mysql.user;

后面show master 

现在在从服务器操作

在salves实例中操作
Mysql>start slave   ##########开启主从复制开关
mysql> CHANGE MASTER TO
    -> MASTER_HOST='192.168.132.139',
    -> MASTER_PORT=3306,
    -> MASTER_USER='rep',
    -> MASTER_PASSWORD='123456',
    -> MASTER_LOG_FILE='mysql-bin.000002',
-> MASTER_LOG_POS=120;

Mysql>start slave   ##########开启主从复制开关
查看slave 的一个状态 
mysql> show slave status\G;

这两个都为yes时候表示是已经成功了

测试是否同步成功了。我在matsrt中新建一个数据库看看salave是否能同步成功。

我在master中新建了一个aa数据库,现在查看一下salves是否同步了

同步完之后,然后就设置从服务器为主节点

首先给从服务器锁表

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.08 sec)

查看master status

然后从节点也是一样的

在salves实例中操作
Mysql>start slave   ##########开启主从复制开关
mysql> CHANGE MASTER TO
    -> MASTER_HOST='192.168.132.139',
    -> MASTER_PORT=3306,
    -> MASTER_USER='rep',
    -> MASTER_PASSWORD='123456',
    -> MASTER_LOG_FILE='mysql-bin.000002',
-> MASTER_LOG_POS=120;

Mysql>start slave   ##########开启主从复制开关
查看slave 的一个状态 
mysql> show slave status\G;

然后查看到两个yes 就在两边自己新疆数据库看看看

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注