mysql主从复制
一、环境
mysql-m:192.168.3.61
mysql-s:192.168.3.62
二、配置
查看mysql-m的server-id和log-bin
[root@mysql-m ~]# egrep "server-id|log-bin" /etc/my.cnf server-id = 1 log-bin=mysql-bin [root@mysql-m ~]# ll /data/mysql/data/ total 28700 -rw-rw---- 1 mysql mysql 18874368 May 18 11:00 ibdata1 -rw-rw---- 1 mysql mysql 5242880 May 18 11:00 ib_logfile0 -rw-rw---- 1 mysql mysql 5242880 May 18 10:39 ib_logfile1 drwx------ 2 mysql root 4096 May 18 10:39 mysql -rw-rw---- 1 mysql mysql 107 May 18 11:00 mysql-bin.000001 -rw-rw---- 1 mysql mysql 19 May 18 11:00 mysql-bin.index -rw-r----- 1 mysql root 3369 May 18 11:00 mysql-m.err -rw-rw---- 1 mysql mysql 6 May 18 11:00 mysql-m.pid srwxrwxrwx 1 mysql mysql 0 May 18 11:00 mysql.sock drwx------ 2 mysql mysql 4096 May 18 10:39 performance_schema drwx------ 2 mysql root 4096 May 18 10:39 test
查看mysql-s的server-id,确保和mysql-m的不一致
[root@mysql-s ~]# grep server-id /etc/my.cnf server-id = 2
三、创建主从复制用的账号,并授权
mysql> grant replication slave on *.* to ‘rep‘@‘192.168.3.62‘ identified by ‘123456‘; Query OK, 0 rows affected (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec)
四、配置主从
在master查看二进制日志和position信息
[root@mysql-m ~]# mysql -u root -plyao36843 -h 127.0.0.1 -e ‘show master status;‘ +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000005 | 107 | | | +------------------+----------+--------------+------------------+
在mysql-s上配置同步信息
mysql> system hostname mysql-s mysql> change master to master_host=‘192.168.3.61‘,master_user=‘rep‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000005‘,master_log_pos=107; Query OK, 0 rows affected (0.03 sec)
启动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.3.61 #master的IP地址 Master_User: rep #同步用的账号 Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000005 #同步的二进制文件 Read_Master_Log_Pos: 107 #同步的position点 Relay_Log_File: mysql-s-relay-bin.000002 Relay_Log_Pos: 253 Relay_Master_Log_File: mysql-bin.000005 Slave_IO_Running: Yes #主从同步的IO线程 Slave_SQL_Running: Yes #主从同步的SQL线程 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: 107 Relay_Log_Space: 411 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)
测试,在主库创建一个数据库linux
[root@mysql-m ~]# mysql -u root -plyao36843 -h 127.0.0.1 -e ‘create database linux;‘ [root@mysql-m ~]# mysql -u root -plyao36843 -h 127.0.0.1 -e ‘show databases;‘ +--------------------+ | Database | +--------------------+ | information_schema | | git | | linux | | mysql | | performance_schema | | test | +--------------------+
从库验证结果
mysql> system hostname mysql-s mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | linux | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) #从结果能看到在master上创建的linux库,已同步到slave上
在生产中我们应该讲master的数据导入到从库后,再启动slave同步
本文出自 “ly36843运维” 博客,请务必保留此出处http://ly36843.blog.51cto.com/3120113/1652236
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。