mysql主从
编译安装mysql
# yum -y install ncurses* cmake bison* openssl* gcc gcc-c++
安装编译mysql时需要的依赖包
# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35.tar.gz
下载mysql安装包
# groupadd mysql && useradd -g mysql mysql -s /sbin/nologin
创建Mysql组和用户 设置不允许登陆系统
# mkdir -p /data/mysql_db
#建立mysql数据库存放目录
# chown -R mysql:mysql /data/mysql_db
#设置mysql数据库目录权限
# tar zxvf mysql-5.6.26.tar.gz && cd mysql-5.6.26
#解压mysql包
# cmake . -DCMAKE_INSTALL_PREFIX=/app/mysql \
-DMYSQL_DATADIR=/data/mysql_db \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/data/mysql_db/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_SSL=yes
#执行Cmake编译Mysql指定安装目录,数据存放目录,配置文件存放目录
# make && make install
#执行编译安装
#配置Mysql:
# vim /etc/my.cnf
#新建mysql配置文件,增加下面配置项
[client]
port = 3306
socket = /data/mysql_db/mysql.sock
[mysql]
no-auto-rehash
[mysqld]
user = mysql
port = 3306
socket = /data/mysql_db/mysql.sock
pid-file=/data/mysql_db/mysql.pid
basedir = /app/mysql/
datadir = /data/mysql_db
open_files_limit = 51200
back_log = 600
table_open_cache = 4096
max_connections = 3000
max_connect_errors = 6000
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 16M
join_buffer_size = 16M
thread_cache_size = 300
thread_concurrency = 8
query_cache_size = 128M
query_cache_limit = 4M
thread_stack = 512K
transaction_isolation = READ-COMMITTED
tmp_table_size = 256M
max_heap_table_size = 256M
long_query_time = 10
slow_query_log = 5
slow_query_log_file = /data/log/mysql_slow-log.log
log_queries_not_using_indexes=1
log_bin=/data/mysql_db/mysql-binlog
binlog_format=mixed
binlog_cache_size = 4M
max_binlog_cache_size = 8M
max_binlog_size = 512M
expire_logs_days = 7
key_buffer_size = 32M
read_buffer_size = 8M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
lower_case_table_names = 1
skip-name-resolve
slave-skip-errors = 1032,1062
replicate-ignore-db=mysql
server-id = 1
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 2048M
innodb_data_file_path = ibdata1:1024M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M
innodb_log_file_size = 128M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0
[mysqldump]
quick
max_allowed_packet = 32M
[mysqld_safe]
open-files-limit = 8192
log-error=/data/mysql_db/mysql.err
::
# /app/mysql/scripts/mysql_install_db \
--user=mysql \
--datadir=/data/mysql_db \
--basedir=/app/mysql
#初始化mysql系统数据库
# cp /app/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
# vim /etc/rc.d/init.d/mysqld 修改下面两项目录配置
basedir=/app/mysql datadir=/data/mysql_db
# chmod 755 /etc/rc.d/init.d/mysqld
# chkconfig mysqld on
#把mysql加入系统服务,赋予执行权限,并设置为开机启动
# service mysqld start
#启动mysql
# ln -s /app/mysql/bin/* /usr/bin
#将mysql所有命令链接到系统环境变量中
#命令行输入mysql ,进入mysql数据库
#Mysql配置结束
#主从同步设置:
#有两台MySQL数据库服务器Master和slave,Master为主服务器,slave为从服务器,初始状态时,Master和slave中的数据信息相同,当Master中的数据发生变化时,slave也跟着发生相应的变化,使得master和slave的数据信息同步,达到备份的目的。
#负责在主、从服务器传输各种修改动作的媒介是主服务器的二进制变更日志,这个日志记载着需要传输给从服务器的各种修改动作。因此,主服务器必须激活二进制日志功能。从服务器必须具备足以让它连接主服务器并请求主服务器把二进制变更日志传输给它的权限。
mysql-master_ip=192.168.0.219
mysql-slvae_ip=192.168.0.227
#创建复制帐号
#在Master的数据库中建立一个备份帐户:每个slave使用标准的MySQL用户名和密码连接master。进行复制操作的用户会授予REPLICATION SLAVE权限。用户名的密码都会存储在文本文件master.info中
#GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO back_up@'192.168.0.227' IDENTIFIED BY '123456';
#建立一个帐户backup,并且只能允许从192.168.0.227这个地址上来登陆,密码是123456。
#拷贝数据
#假如是你完全新安装mysql主从服务器,这个一步就不需要。因为新安装的master和slave有相同的数据)关停Master服务器,将Master中的数据拷贝到B服务器中,使得Master和slave中的数据同步,并且确保在全部设置操作结束前,禁止在Master和slave服务器中进行写操作 使得两数据库中的数据一定要相同!
#配置master
#打开二进制日志,指定唯一的servr ID
server-id = 1 #为主服务器A的ID值
log_bin=/data/mysql_db/mysql-binlog #保证binlog可读,二进制变更日值
read-only = 0 #主机,读写都可以
#binlog-do-db = test #需要备份数据,多个写多行,不写全部都备份
#binlog-ignore-db = mysql #不需要备份的数据库,多个写多行
重启master,运行SHOW MASTER STATUS
#mysql> SHOW MASTER STATUS;
#+---------------------+----------+--------------+------------------+-------------------+
#| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
#+---------------------+----------+--------------+------------------+-------------------+
#| mysql-binlog.000004 | 120 | | | |
#+---------------------+----------+--------------+------------------+-------------------+
#1 row in set (0.04 sec)
#配置slave
#Slave的配置与master类似,同样需要重启slave的MySQL。
#log_bin=/data/mysql_db/mysql-binlog
server_id= 2
relay_log=mysql-relay-bin
log_slave_updates = 1
read_only= 1
#server_id是必须的,而且唯一。slave没有必要开启二进制日志,但是在一些情况下,必须设置,例如,如果slave为其它slave的master,必须设置bin_log。
#relay_log配置中继日志,log_slave_updates表示slave将复制事件写进自己的二进制日志
#开启了slave的二进制日志,却没有设置log_slave_updates,然后查看slave的数据是否改变,这是一种错误的配置。所以,尽量使用read_only,它防止改变数据(除了特殊的线程)。但是,read_only并是很实用,特别是那些需要在slave上创建表的应用。
#启动slave
#接下来就是让slave连接master,并开始重做master二进制日志中的事件。你不应该用配置文件进行该操作,而应该使用CHANGE MASTER TO语句,该语句可以完全取代对配置文件的修改,而且它可以为slave指定不同的master,而不需要停止服务器。如下:
#mysql> change master to master_host='192.168.0.219',master_user='back_up',master_password='123456',master_log_file='mysql-binlog.000004',master_log_pos=0;
#MASTER_LOG_POS的值为120,因为它是日志的开始位置
#你可以用SHOW SLAVE STATUS语句查看slave的设置是否正确:
#mysql> SHOW SLAVE STATUS\G
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.0.219
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-binlog.000004
Read_Master_Log_Pos: 120
Relay_Log_File: mysql-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-binlog.000004
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB: mysql
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 120
Relay_Log_Space: 120
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
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: 0
Master_UUID:
Master_Info_File: /data/mysql_db/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
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.01 sec)
#Slave_IO_State, Slave_IO_Running, 和Slave_SQL_Running是No
#表明slave还没有开始复制过程。日志的位置为120而不是4,这是因为0只是日志文件的开始位置,并不是日志位置。实际上,MySQL知道的第一个事件的位置是4。
#开始复制
mysql> start slave;
Query OK, 0 rows affected (0.06 sec)
#运行SHOW SLAVE STATUS查看输出结果:
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 192.168.0.219
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-binlog.000004
Read_Master_Log_Pos: 120
Relay_Log_File: mysql-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-binlog.000004
Slave_IO_Running: connecting #错误!
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 120
Relay_Log_Space: 120
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 1130
Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 60 retries: 1 #这样就是错的!
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
Master_UUID:
Master_Info_File: /data/mysql_db/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: 160728 10:15:04
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
#检查错误过程及解决:
1,检查错误日志,提示说是不能连接到master上
2,从slave执行 mysql -uback_up -p123456 -h 192.168.0.219,提示报错检查之前执行过的命令,授权有问题更改授权语句,重新执行
#成功结果应该是这样的
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.219
Master_User: back_up
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-binlog.000004
Read_Master_Log_Pos: 490
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 656
Relay_Master_Log_File: mysql-binlog.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 490
Relay_Log_Space: 829
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
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: 1
Master_UUID: a1359f50-54e0-11e6-bc54-000c2995c859
Master_Info_File: /data/mysql_db/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
#在这里主要是看:
Slave_IO_Running=Yes
Slave_SQL_Running=Yes
#你可查看master和slave上线程的状态。在master上,你可以看到slave的I/O线程创建的连接:
#在master上输入show processlist\G;
mysql> show processlist\G;
*************************** 1. row ***************************
Id: 18
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: init
Info: show processlist
*************************** 2. row ***************************
Id: 28
User: back_up
Host: 192.168.0.227:39804
db: NULL
Command: Binlog Dump
Time: 445
State: Master has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
#行2为处理slave的I/O线程的连接。
#在slave服务器上运行该语句:
show processlist\G;
mysql> show processlist\G;
*************************** 1. row ***************************
Id: 14
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: init
Info: show processlist
*************************** 2. row ***************************
Id: 15
User: system user
Host:
db: NULL
Command: Connect
Time: 539
State: Waiting for master to send event
Info: NULL
*************************** 3. row ***************************
Id: 16
User: system user
Host:
db: NULL
Command: Connect
Time: 615
State: Slave has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
#测试
#在master上执行
mysql> create database sql_sanxian;
Query OK, 1 row affected (0.09 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sql_sanxian |
| test |
+--------------------+
#在slave上执行
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sql_sanxian |
| test |
+--------------------+
#slvae已经同步到master的数据