标签:mysql主从、slave、master、replication
操作系统环境:CentOS release 6.5 (Final)
MySQL数据库 :mysql Ver 14.14 Distrib 5.5.45
master 192.168.10.180
slave 192.168.10.80
主从服务器的hosts均添加了ip主机名对应关系
大体的步骤有以下几步
1.服务器角色指定
2.mysql数据库安装配置
3.主库上的配置
3.1 设置server-id并开启binlog
3.2 创建用户用于同步,测试是否可以连接
3.3 对数据库锁表只读
3.4 查看主库状态
3.5 master全备
3.6 传输备份到slave
4.从库上的配置
4.1 设置server-id
4.2 还原来自master的全备
4.3 配置同步参数
4.4 测试
步骤2参考之前的安装配置,本次均是在cmake编译安装的基础上进行操作的
均打开开机启动
[root@master ~]# chkconfig --list mysqld mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@slave ~]# chkconfig --list mysqld mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
3.1修改参数文件
[root@master ~]# vi /etc/my.cnf server-id = 1 log-bin = /app/mysql-5.5.45/data/mysql-bin [root@master ~]# grep -E "server-id|log-bin" /etc/my.cnf log-bin = /app/mysql-5.5.45/data/mysql-bin server-id = 1 #log-bin=mysql-bin [root@master ~]# service mysqld restart Shutting down MySQL. SUCCESS! Starting MySQL.. SUCCESS!
注意配置在mysqld模块下重启数据库
3.2创建用户,测试
[root@master ~]# mysql -uroot -prootroot mysql> grant replication slave on *.* to ‘slave_rep‘@‘192.168.10.80‘ identified by ‘123456‘; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges ; Query OK, 0 rows affected (0.00 sec) replication slave是必须的权限,*.*表示所有的表,也可以指定具体的某个库 slave_rep‘@‘192.168.10.80‘为用户名和指定的ip 从库测试: [root@slave ~]# mysql -uslave_rep -p123456 -h 192.168.10.180
3.3 对数据库锁表只读
mysql> show master status ; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 1135 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) mysql> show master logs ; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 1135 | +------------------+-----------+ 1 row in set (0.00 sec) mysql> flush table with read lock ; Query OK, 0 rows affected (0.01 sec)
注意5.1和5.5锁表有不同 tables和table
开启新终端进行全备
[root@master ~]# mysqldump -uroot -prootroot -A -B --events --master-data=2|gzip > /tmp/mysql_rep.sql.gz
主库解锁
mysql> unlock tables; Query OK, 0 rows affected (0.00 sec) [root@master ~]# scp /tmp/mysql_rep.sql.gz 192.168.10.80:/tmp
从库配置
4.1 设置server-id
[root@slave ~]# vi /etc/my.cnf [root@slave ~]# grep -E "server-id" /etc/my.cnf server-id = 3 [root@slave ~]# service mysqld restart Shutting down MySQL. SUCCESS! Starting MySQL.. SUCCESS! [root@slave ~]# [root@slave ~]# mysql -uroot -prootroot -e "show variables like ‘%server_id%‘;" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 3 | +---------------+-------+
4.2 还原来自master的全备
[root@slave ~]# gunzip /tmp/mysql_rep.sql.gz [root@slave ~]# mysql -uroot -prootroot < /tmp/mysql_rep.sql
4.3 配置同步参数
mysql> change master to -> master_host=‘192.168.10.180‘, -> master_port=3306, -> master_user=‘slave_rep‘, -> master_password=‘123456‘, -> master_log_file=‘mysql-bin.000001‘, -> master_log_pos=1135; Query OK, 0 rows affected (0.07 sec)
以上操作产生的信息存放在数据目录下的/app/mysql-5.5.45/data/master_info中,从主库接收后更新这个文件
4.4 启动slave
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.10.180 Master_User: slave_rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 1135 Relay_Log_File: slave-relay-bin.000002 Relay_Log_Pos: 253 Relay_Master_Log_File: mysql-bin.000001 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: 1135 Relay_Log_Space: 409 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 1 row in set (0.00 sec) ERROR: No query specified 从库的中继日志信息 [root@slave data]# cat relay-log.info ./slave-relay-bin.000002 253 mysql-bin.000001 1135
4.5 测试
主库创建数据库,建表,插入数据,从库查看同步信息
[root@master ~]# mysql -uroot -prootroot mysql> create database rep1 ; Query OK, 1 row affected (0.00 sec) mysql> use rep1 ; Database changed mysql> create table test (num int); Query OK, 0 rows affected (0.01 sec) mysql> insert into test values(1); Query OK, 1 row affected (0.01 sec) mysql> insert into test values(2); Query OK, 1 row affected (0.00 sec)
从库查看
[root@slave ~]# mysql -uroot -prootroot mysql> show databases ; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | rep1 | | test_rep | +--------------------+ 5 rows in set (0.00 sec mysql> select * from rep1.test ; +------+ | num | +------+ | 1 | | 2 | +------+ 2 rows in set (0.00 sec) 查看中继日志信息 [root@slave data]# cat relay-log.info ./slave-relay-bin.000002 794 mysql-bin.000001 1676 [root@slave data]# [root@slave data]# mysqlbinlog slave-relay-bin.000002 从库信息 mysql> show processlist\G ; *************************** 1. row *************************** Id: 17 User: system user Host: db: NULL Command: Connect Time: 831 State: Waiting for master to send event Info: NULL *************************** 2. row *************************** Id: 18 User: system user Host: db: NULL Command: Connect Time: 373 State: Slave has read all relay log; waiting for the slave I/O thread to update it Info: NULL *************************** 3. row *************************** Id: 19 User: root Host: localhost db: NULL Command: Query Time: 0 State: NULL Info: show processlist 3 rows in set (0.00 sec) ERROR: No query specified 主库信息 mysql> show processlist\G ; *************************** 1. row *************************** Id: 8 User: slave_rep Host: slave:53940 db: NULL Command: Binlog Dump Time: 867 State: Master has sent all binlog to slave; waiting for binlog to be updated Info: NULL *************************** 2. row *************************** Id: 9 User: root Host: localhost db: rep1 Command: Query Time: 0 State: NULL Info: show processlist 2 rows in set (0.00 sec) ERROR: No query specified
本文出自 “相守姑娘说” 博客,请务必保留此出处http://sugarlovecxq.blog.51cto.com/6707742/1697139
标签:mysql主从、slave、master、replication
原文地址:http://sugarlovecxq.blog.51cto.com/6707742/1697139