首页 > 代码库 > MYsql编译安装及主从复制
MYsql编译安装及主从复制
MYsql主从复制:
操作系统:centOS6.6
准备文件:mariadb-5.5.40-linux-x86_64.tar.gz
(安装MYSQL,在node2和node3执行)
[root@node1 ~]# yum install mysql –y (安装mysql客户端)
[root@node2 ~]# scp mariadb-5.5.40-linux-x86_64.tar.gz node3:/root
[root@node2 ~]# useradd -r mysql
[root@node2 ~]# mkdir -pv /mydata/data
[root@node2 ~]# chown -R mysql.mysql/mydata/data/
[root@node2 ~]# tar -xfmariadb-5.5.40-linux-x86_64.tar.gz -C /usr/local/
[root@node2 ~]# cd /usr/local/
[root@node2 local]# lnmariadb-5.5.40-linux-x86_64/ -sv mysql
[root@node2 local]# cd mysql/
[root@node2 mysql]# chown -R root.mysql ./*
[root@node2 mysql]#scripts/mysql_install_db --user=mysql --datadir=/mydata/data/
[root@node2 mysql]# mkdir /etc/mysql
[root@node2 mysql]# cpsupport-files/my-large.cnf /etc/mysql/my.cnf
[root@node2 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
[root@node2 mysql]# cp support-files/mysql.server/etc/init.d/mysqld
[root@node2 mysql]# chkconfig --add mysqld
[root@node2 mysql]# chkconfig --list mysqld (查看是否添加到启动项)
[root@node2 mysql]# service mysqld start
[root@node2 mysql]# cd /mydata/data/
[root@node2 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。。。。。。。。。。。。
[root@node2 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。。。。。。。。。。。。
修改从节点相关配置
[root@node3 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都可以)
[root@node3 mysql]# service mysqld restart
[root@node3 mysql]#/usr/local/mysql/bin/mysql (连接Mysql)
MariaDB [(none)]> SHOW GLOBAL VARIABLESLIKE ‘%only%‘; (查看只读是否开启)
。。。。。。。。切换NODE2。。。。。。。。。。。。
[root@node2 data]# /usr/local/mysql/bin/mysql
MariaDB [(none)]> SHOW MASTER STATUS;
| File | Position
| mysql-bin.000001 | 496 | (二进制文件日志以及位置)
。。。。。。。。切换NODE3。。。。。。。。。。。。
[root@node3 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
[root@node3 ~]# cd /mydata/data/
[root@node3 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。。。。。。。。。。。。
[root@node2 data]#/usr/local/mysql/bin/mysql
mysql> create databasemydb; (在主服务器创建一个数据库)
。。。。。。。。切换NODE3。。。。。。。。。。。。
[root@node3 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查看,数据已经复制过来了)
MYsql编译安装及主从复制