MYsql主从复制:
操作系统:centOS6.6
准备文件:mariadb-5.5.40-linux-x86_64.tar.gz
(安装MYSQL,在node2和node3执行)
[[email protected] ~]# yum install mysql –y (安装mysql客户端)
[[email protected] ~]# scp mariadb-5.5.40-linux-x86_64.tar.gz node3:/root
[[email protected] ~]# useradd -r mysql
[[email protected] ~]# mkdir -pv /mydata/data
[[email protected] ~]# chown -R mysql.mysql/mydata/data/
[[email protected] ~]# tar -xfmariadb-5.5.40-linux-x86_64.tar.gz -C /usr/local/
[[email protected] ~]# cd /usr/local/
[[email protected] local]# lnmariadb-5.5.40-linux-x86_64/ -sv mysql
[[email protected] local]# cd mysql/
[[email protected] mysql]# chown -R root.mysql ./*
[[email protected] mysql]#scripts/mysql_install_db --user=mysql --datadir=/mydata/data/
[[email protected] mysql]# mkdir /etc/mysql
[[email protected] mysql]# cpsupport-files/my-large.cnf /etc/mysql/my.cnf
[[email protected] mysql]# vim /etc/mysql/my.cnf
# Try number of CPU‘s*2 forthread_concurrency
thread_concurrency = 8
datadir = /mydata/data
innodb_file_per_table =on
skip_name_resolve = on
# Replication Master Server (default)
# binary logging is required forreplication
log-bin=mysql-bin (启用二进制日志)
# binary logging format - mixed recommended
binlog_format=mixed
# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master ifomitted
server-id = 1
[[email protected] mysql]# cp support-files/mysql.server/etc/init.d/mysqld
[[email protected] mysql]# chkconfig --add mysqld
[[email protected] mysql]# chkconfig --list mysqld (查看是否添加到启动项)
[[email protected] mysql]# service mysqld start
[[email protected] mysql]# cd /mydata/data/
[[email protected] data]# ll
-rw-rw---- 1 mysqlmysql 245 Jan 25 10:13mysql-bin.000001
-rw-rw---- 1 mysqlmysql 19 Jan 25 10:13mysql-bin.index
。。。。。。。。切换NODE2。。。。。。。。。。。。
[[email protected] data]#/usr/local/mysql/bin/mysql (连接Mysql,也可以直接使用#mysql进入)
MariaDB [(none)]> GRANT REPLICATIONCLIENT,REPLICATION SLAVE ON *.* TO ‘repluser‘@‘172.16.%.%‘ IDENTIFIED BY‘replpass‘; (创建一个有复制权限的账号)
MariaDB [(none)]> FLUSH PRIVILEGES;
。。。。。。。。切换NODE3。。。。。。。。。。。。
修改从节点相关配置
[[email protected] mysql]# vim /etc/mysql/my.cnf
# Replication Master Server (default)
# binary logging is required forreplication
#log-bin=mysql-bin (不启用二进制日志)
# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master ifomitted
server-id = 20 (server-id必须修改,不能与node2相同)
relay-log = relay-bin (添加中继日志)
read-only = on (开启只读,使用on或1都可以)
[[email protected] mysql]# service mysqld restart
[[email protected] mysql]#/usr/local/mysql/bin/mysql (连接Mysql)
MariaDB [(none)]> SHOW GLOBAL VARIABLESLIKE ‘%only%‘; (查看只读是否开启)
。。。。。。。。切换NODE2。。。。。。。。。。。。
[[email protected] data]# /usr/local/mysql/bin/mysql
MariaDB [(none)]> SHOW MASTER STATUS;
| File | Position
| mysql-bin.000001 | 496 | (二进制文件日志以及位置)
。。。。。。。。切换NODE3。。。。。。。。。。。。
[[email protected] mysql]#/usr/local/mysql/bin/mysql
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘172.16.18.20‘, MASTER_USER=‘repluser‘ ,MASTER_PASSWORD=‘replpass‘ , MASTER_LOG_FILE=‘ mysql-bin.000001‘ ,MASTER_LOG_POS=496, MASTER_CONNECT_RETRY=5 , MASTER_HEARTBEAT_PERIOD=2;
(连接主服务器)
| MASTER_HOST = ‘host_name‘ 主服务器地址
| MASTER_USER = ‘user_name‘ 有复制权限的用户名
| MASTER_PASSWORD = ‘password‘ 用户密码
| MASTER_PORT = port_num 主服务器的端口
| MASTER_CONNECT_RETRY = interval 连接重试时间间隔
| MASTER_HEARTBEAT_PERIOD = interval 心跳检测时间间隔
| MASTER_LOG_FILE = ‘master_log_name‘ 主服务器二进制日志文件
| MASTER_LOG_POS = master_log_pos 二进制日志文件中的位置
MariaDB [(none)]> SHOW SLAVE STATUS\G (查看从服务器状态)
Slave_IO_Running: No
Slave_SQL_Running: No
[[email protected] ~]# cd /mydata/data/
[[email protected] data]# ll
-rw-rw---- 1 mysql mysql 245 Jan 25 11:21 relay-bin.000001
-rw-rw---- 1 mysqlmysql 43 Jan 25 11:21relay-log.info
MariaDB [(none)]> START SLAVE; (启用从服务器)
MariaDB [(none)]> SHOW SLAVE STATUS\G (查看从服务器状态)
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
。。。。。。。。切换NODE2。。。。。。。。。。。。
[[email protected] data]#/usr/local/mysql/bin/mysql
mysql> create databasemydb; (在主服务器创建一个数据库)
。。。。。。。。切换NODE3。。。。。。。。。。。。
[[email protected] mysql]#/usr/local/mysql/bin/mysql
mysql> show slave status\G; (查看状态)
Slave_IO_State: Waiting formaster to send event
Master_Host: 172.16.18.20
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 579 (位置已经发生变化)
mysql> show databases; (在NODE3查看,数据已经复制过来了)