**
mysql主从架构搭建:
**
环境准备:
主:192.168.189.161
从:192.168.189.162
##################################################
1.两个主机上同时安装mysql,并关闭防火墙
# yum -y install mysql mysql-server
# service iptables stop
# setenforce 0
##################################################
主上操作:
1.开启二进制日志
进入vim /etc/my.cnf
目录
server-id=1
log-bin=mysql-bin
重启mysql
# service mysqld restart
2.mysql主上操作授权复制账号
登录mysql mysql 授权并创建用户tom
mysql> grant replication slave on *.* to tom@'%' identified by '123';
查看二进制日志
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 242 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
#####################################################
在mysql从主机上操作
从上操作:
开启中继日志
进入vim /etc/my.cnf
目录目录
server-id=2
relay-log=mysql-relay
重启mysql
service mysqld restart
登录mysql
#mysql
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to
-> master_host='192.168.189.161',
-> master_user='tom',
-> master_password='123',
-> master_log_file='mysql-bin.000003',
-> master_log_pos=242;
Query OK, 0 rows affected (0.03 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.189.161
Master_User: tom
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 242
Relay_Log_File: mysql-relay.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running:` Yes`
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: 242
Relay_Log_Space: 402
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:
1 row in set (0.00 sec)