话说一个正确的文章能敌千钧万马,一句善意的点拨能敌百万雄狮,一个好友的帮助能让你拨开云雾见青天。搭建mysql主从同步,这两天看网上的博客教程很多,当然,错误的文章会误导你很多,我就被误导了。现将这两天的搭建过程详细记录:
前期准备:关闭防火墙
关闭SELINUX
关闭SELINUX
vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq #保存退出
setenforce 0 #立即生效
-------两台机器都要关闭防火墙和selinux
如果一定要开启防火墙的话,建议只需要加入3306端口就行,具体方法见我的另外一个文章(http://caizi.blog.51cto.com/5234706/1536463);
环境:linux虚拟机两台,系统是centos 6.3 64位
mysql 版本:5.6.11 linux 64位
两台服务器
主:172.19.0.116 Master116 主(master)
从:172.19.0.117 backup117 从(saver)
两台都要安装mysql如下步骤
安装系统所需要的依赖包
[root@ 116 ~]# yum -y install gcc gcc-c++ gcc-g77 autoconf automake zlib* fiex* libxml* ncurses-devel libmcrypt* libtool-ltdl-devel* cmake bison
创建mysql用户与用户组
[root@ 116~]# groupadd mysql
[root@ 116~]# useradd -r -g mysql mysql
安装mysql
[root@ 116~]# tar zxvf mysql-5.6.11.tar.gz
[root@ 116~]# cd mysql-5.6.11
[root@116 mysql-5.6.11]# cmake -DCMAKE_INSTALL_PREFIX=/opt/mysql/ -DMYSQL_DATADIR=/opt/mysql/data -DMYSQL_UNIX_ADDR=/opt/mysql/data/mysqld.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_TCP_PORT=3306 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_UNIX_ADDR=/opt/mysql/data/mysql.sock -DMYSQL_USER=mysql -DWITH_DEBUG=0
[root@ 116 mysql-5.6.11]# make && make install
赋予相关权限
[root@ 116 mysql-5.6.11]# chown -R mysql:mysql /opt/mysql
[root@116 mysql-5.6.11]# /opt/mysql/scripts/mysql_install_db --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data
mysql配置文件
[root@ 116 mysql-5.6.11]# cp /opt/mysql/support-files/my-default.cnf /etc/my.cnf ----部分文章有误,因为 版本不同
[root@116 mysql-5.6.11]# cp /opt/mysql/support-files/mysql.server /etc/init.d/mysql
[root@116 mysql-5.6.11]# chmod +x /etc/init.d/mysql
增加自动启动
[root@116 mysql-5.6.11]# chkconfig mysql on
启动mysql
[root@116 mysql-5.6.11]# /etc/init.d/mysql start
Starting MySQL...... [ OK ]
查看mysql 的3306端口,看到下面说明启动成功
[root@Master120 mysql-5.5.25]#ps aux | grep 3306
mysql 17237 3.6 8.4 615660 86264 pts/2 Sl 22:10 0:01 /opt/mysql/bin/mysqld --basedir=/opt/mysql
设置mysql初始密码,123456是密码,你可以设置自己需要的密码
[root@116 mysql-5.6.11]# /opt/mysql/bin/mysqladmin -u root password '123456'
[root@116 mysql-5.6.11]# /opt/mysql/bin/mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.11 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show master status;
Empty set (0.01 sec) ----红色为系统显示
mysql>
主从复制的配置
主的my.cnf的server-id=1,每个同步服务都必须设定一个唯一的编号。
cat /etc/my.cnf 查看my.cnf 内容
show variables like 'server_id'; #查看server-id的值是否为1
mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 |
+---------------+-------+
1 row in set (0.02 sec)
mysql>
在Master(这里为116机器)上增加一个用于复制的账号:
mysql>GRANT REPLICATION SLAVE ON *.* TO 'repl'@'172.19.0.%' IDENTIFIED BY '123456';
mysql>flush privileges; --这个地方注意:有的帖子写的是(mysql>FLUSH REPLICATION;)报错
mysql> show master status; - --- 这里没显示不知道为啥
设置Slave 主机
修改my.cnf的server-id,内容如下:
cat /etc/my.cnf 查看my.cnf 内容
backup117主机
server-id=2
show variables like 'server_id'; #查看server-id的值是否为2
mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id |2 |
+---------------+-------+
1 row in set (0.02 sec)
mysql>
开启Master与Slave的同步
在Slave上执行如下命令
mysql>CHANGE MASTER TO MASTER_HOST='172.19.0.116',
->MASTER_USER='repl',
->MASTER_PASSWORD='123456',
->MASTER_LOG_FILE='mysql-bin.000001',
->MASTER_LOG_POS=245;
之后执行 ----这个地方执行是一个块,我卡这里了
mysql>start slave; ###开启服务器线程 ----(不是其他文章里面的slave start;执行slave start;会报错的)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 172.19.0.116
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 245
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: No ----这个有问题
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
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: 245
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: 1236
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Binary log is not open'
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 4728455b-16cf-11e4-9cee-000c298ac0cb
Master_Info_File: /opt/mysql/data/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: 140729 12:30:56
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)
上面还是有点问题,没有找到问题所在,今后如果有解决的话再做补充!
转载于:https://blog.51cto.com/fangwei009/1536468