我们现在模拟的是主从(1台主机、一台从机),其主从同步的原理,就是对bin-log二进制文件的同步,将这个文件的内容从主机同步到从机。
一、配置文件的修改
1、主机配置文件修改配置
我们首先需要mysql主机(192.168.254.130)的/etc/my.cnf配置文件,添加如下配置:
#主机唯一id server-id=1 #二进制日志 log-bin=mysql-bin #不需要同步的数据库 binlog-ignore-db=mysql binlog-ignore-db=information_schema #同步的数据库名称 binlog-do-db=mycat #二进制的格式 binlog_format=statement
我们看下目前整个my.cnf文件
[root@localhost desktop]# cat /etc/my.cnf [mysqld] datadir=/usr/local/mysql/data basedir=/usr/local/mysql socket=/usr/local/mysql/data/mysql.sock user=mysql # disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 server-id=1 log-bin=mysql-bin binlog-ignore-db=mysql binlog-ignore-db=information_schema binlog-do-db=mycat binlog_format=statement [mysqld_safe] log-error=/usr/local/mysql/data/mysqld.log pid-file=/usr/local/mysql/data/mysqld/mysqld.pid [root@localhost desktop]#
修改主机的配置文件后,我们需要通过命令重启下服务:
[root@localhost support-files]# ls magic mysqld_multi.server mysql-log-rotate mysql.server [root@localhost support-files]# pwd /usr/local/mysql/support-files [root@localhost support-files]# ./mysql.server restart
然后我们修改下从机(192.168.254.131)的配置文件。
2、从机的配置
从机的配置修改比较简单:
#从机机器唯一id server-id=2 #中继日志 relay-log=mysql-relay
同样修改配置后,我们重启下从机
二、mysql客户端命令操作
下面我们可以通过命令连接到mysql的命令端:
[root@localhost bin]# [root@localhost bin]# pwd /usr/local/mysql/bin [root@localhost bin]# ./mysql -uroot -p
1、主机操作
1)、创建同步用户
首先我们可以在主机创建一个专门用于主从同步用户,通过命令:
grant replication slave on *.* to 'slave'@'%' identified by '123456';
2)、查看同步文件状态
然后我们通过show master status;查看主机的同步内容状态:
mysql> show master status; +------------------+----------+--------------+--------------------------+-------------------+ | file | position | binlog_do_db | binlog_ignore_db | executed_gtid_set | +------------------+----------+--------------+--------------------------+-------------------+ | mysql-bin.000001 | 154 | mycat | mysql,information_schema | | +------------------+----------+--------------+--------------------------+-------------------+ 1 row in set (0.00 sec)
2、从机操作
1)、设置从机的主机
执行如下命令,这里设置了我们与主机建立同步的相关信息
change master to master_host='192.168.254.130', master_user='slave', master_password='123456', master_log_file='mysql-bin.000001', master_log_pos=430;
这里如果提示已经设置了主机配置,可以通过stop slave&reset master进行重置。
2)、启动同步
下面我们再通过start slave开启同步:
就可以看到:
mysql> show slave status\g *************************** 1. row *************************** slave_io_state: waiting for master to send event master_host: 192.168.254.130 master_user: slave master_port: 3306 connect_retry: 60 master_log_file: mysql-bin.000001 read_master_log_pos: 592 relay_log_file: mysql-relay.000002 relay_log_pos: 482 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: 592 relay_log_space: 685 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: 74397a99-accf-11eb-ae0d-000c2912d302 master_info_file: /usr/local/mysql/data/master.info sql_delay: 0 sql_remaining_delay: null slave_sql_running_state: slave has read all relay log; waiting for more updates 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 replicate_rewrite_db: channel_name: master_tls_version: 1 row in set (0.00 sec) mysql>
这里我们可以看到slave_io_running、slave_sql_running都为yes,则成功了,如果是下面这种:
*************************** 1. row *************************** slave_io_state: connecting to master master_host: 192.168.254.130 master_user: slave master_port: 3306 connect_retry: 60 master_log_file: mysql-bin.000001 read_master_log_pos: 430 relay_log_file: mysql-relay.000001 relay_log_pos: 4 relay_master_log_file: mysql-bin.000001 slave_io_running: connecting 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: 430 relay_log_space: 154 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: 1045 last_io_error: error connecting to master 'slave@192.168.254.130: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: /usr/local/mysql/data/master.info sql_delay: 0 sql_remaining_delay: null slave_sql_running_state: slave has read all relay log; waiting for more updates master_retry_count: 86400 master_bind: last_io_error_timestamp: 210505 00:18:08 last_sql_error_timestamp: master_ssl_crl: master_ssl_crlpath: retrieved_gtid_set: executed_gtid_set: auto_position: 0 replicate_rewrite_db: channel_name: master_tls_version: 1 row in set (0.00 sec)
我们可以看到last_io_error这里有错误,我们就可以去看下日志报的什么问题了,目前我这个是因为同步用户写错了才不能同步,按上面说的先停止同步重置,修改后同步命令,再操作一遍就可以了。
三、主从同步测试
1、主机创建库
我们先在主机创建我们前面设置的要同步的数据库mycat:
mysql> create database mycat; query ok, 1 row affected (0.00 sec) mysql> use mycat; database changed mysql> show databases; +--------------------+ | database | +--------------------+ | information_schema | | mycat | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql>
2、从机查看库
然后我们就能在从机看到这个库了
mysql> show databases; +--------------------+ | database | +--------------------+ | information_schema | | mycat | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
3、表数据的初始
下面我们进行表数据的测试
1)、主机
首先我们再主机建立表并插入数据
mysql> use mycat; database changed mysql> mysql> create table `test1`( -> id int auto_increment not null primary key, -> name varchar(10) default null -> ); query ok, 0 rows affected (0.03 sec) mysql> insert into test1(`id`,`name`) value(1,"petty"); query ok, 1 row affected (0.16 sec) mysql> select * from test1; +----+-------+ | id | name | +----+-------+ | 1 | petty | +----+-------+ 1 row in set (0.00 sec) mysql>
2)、从机
下面我们在从机查看看有没有成功同步:
mysql> use mycat; reading table information for completion of table and column names you can turn off this feature to get a quicker startup with -a database changed mysql> show tables; +-----------------+ | tables_in_mycat | +-----------------+ | test1 | +-----------------+ 1 row in set (0.00 sec) mysql> select * from test1; +----+-------+ | id | name | +----+-------+ | 1 | petty | +----+-------+ 1 row in set (0.00 sec) mysql>
可以看到我们的主从配置已经成功了。
四、多主多从
我们还可以多主多从,例如我们一个主从序列是编号1位主机、编号2为从机,然后编号3为主机、编号4为从机,同时编号1主机与编号3主机相互为主从,这样就是其中主机一台有问题,整个mysql集群还是能正常工作。
由于目前只有3台机,只使用三台来写demo(一台windows,两台linux)。
1、编号1主机(192.168.254.30)
1)、修改配置
我们首先需要修改其原来的etc/my.cnf文件,添加:
# 作为从机也修改其bin-log日志 log-slave-updates #自增长的幅度 auto-increment-increment=2 #自增长的开始位置 auto-increment-offset=1
整个文件的信息
[root@localhost desktop]# cat /etc/my.cnf [mysqld] datadir=/usr/local/mysql/data basedir=/usr/local/mysql socket=/usr/local/mysql/data/mysql.sock user=mysql # disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 server-id=1 log-bin=mysql-bin binlog-ignore-db=mysql binlog-ignore-db=information_schema binlog-do-db=mycat binlog_format=statement log-slave-updates auto-increment-increment=2 auto-increment-offset=1 [mysqld_safe] log-error=/usr/local/mysql/data/mysqld.log pid-file=/usr/local/mysql/data/mysqld/mysqld.pid [root@localhost desktop]#
修改这个文件后我们需要重启机器
2、编号2从机(192.168.254.31)
这台原来已经配置其连接30机器了,所以这次不用修改
3、编号3主机(192.168.254.1)
1)、修改配置文件
由于这台机器是windows,所以我们需要修改其的my.ini文件,在其最后面添加
server-id=3 log-bin=mysql-bin binlog-ignore-db=mysql binlog-ignore-db=information_schema binlog-do-db=mycat binlog_format=statement log-slave-updates auto-increment-increment=2 auto-increment-offset=2
注意我们上面改了server-id,同时也改了其的增长开始点auto-increment-offset=2。同时再重启服务。
2)、创建同步用户
首先我们可以在主机创建一个专门用于主从同步用户,通过命令:
grant replication slave on *.* to 'slave'@'%' identified by '123456';
3)、查看状态
mysql> show master status; +------------------+----------+--------------+--------------------------+-------------------+ | file | position | binlog_do_db | binlog_ignore_db | executed_gtid_set | +------------------+----------+--------------+--------------------------+-------------------+ | mysql-bin.000001 | 154 | mycat | mysql,information_schema | | +------------------+----------+--------------+--------------------------+-------------------+ 1 row in set (0.00 sec) mysql>
4)、设置同步的状态
下面我们运行其连接的主机(30)信息
change master to master_host='192.168.254.130', master_user='slave', master_password='123456', master_log_file='mysql-bin.000001', master_log_pos=430;
5)、编号1主机同步(192.168.254.30)
我们需要设置其去同步编号3主机(192.168.254.1),即我们前面查看的编号3的(master status):
change master to master_host='192.168.254.1', master_user='slave', master_password='123456', master_log_file='mysql-bin.000001', master_log_pos=154;
然后我们在编号1主机执行同步start slave;,也在编号3主机执行同步start slave;。
4、测试查看
1)、可能的问题(可略过)
现在我们测试,然后分别查看这两台的master状态show master status;。
mysql> show slave status\g *************************** 1. row *************************** slave_io_state: waiting for master to send event master_host: 192.168.254.1 master_user: slave master_port: 3306 connect_retry: 60 master_log_file: mysql-bin.000001 read_master_log_pos: 154 relay_log_file: localhost-relay-bin.000002 relay_log_pos: 320 relay_master_log_file: mysql-bin.000001 slave_io_running: yes slave_sql_running: yes ........
mysql> show slave status\g *************************** 1. row *************************** slave_io_state: waiting for master to send event master_host: 192.168.254.130 master_user: slave master_port: 3306 connect_retry: 60 master_log_file: mysql-bin.000002 read_master_log_pos: 462 relay_log_file: laptop-qr83qec0-relay-bin.000003 relay_log_pos: 675 relay_master_log_file: mysql-bin.000002 slave_io_running: yes slave_sql_running: yes replicate_do_db: .........
可以看到它们的同步都是yes。这里可能有问题,我们需要自己解决,例如我在编号1机器修改配置,然后在查看其的状态,
mysql> show master status; +------------------+----------+--------------+--------------------------+-------------------+ | file | position | binlog_do_db | binlog_ignore_db | executed_gtid_set | +------------------+----------+--------------+--------------------------+-------------------+ | mysql-bin.000002 | 462 | mycat | mysql,information_schema | | +------------------+----------+--------------+--------------------------+-------------------+ 1 row in set (0.00 sec) mysql>
我如果以这个消息去让编号3机器同步编号1,就会报(因为我又运行了一条新的插入语句),但建表语句是在日志mysql-bin.000001,而这里我因为重启了,其有有新的mysql-bin.000002,所以有修改回了原来编号2的同步信息。
mysql> show slave status\g *************************** 1. row *************************** slave_io_state: waiting for master to send event master_host: 192.168.254.130 master_user: slave master_port: 3306 connect_retry: 60 master_log_file: mysql-bin.000002 read_master_log_pos: 462 relay_log_file: laptop-qr83qec0-relay-bin.000002 relay_log_pos: 320 relay_master_log_file: mysql-bin.000002 slave_io_running: yes slave_sql_running: no replicate_do_db: replicate_ignore_db: replicate_do_table: replicate_ignore_table: replicate_wild_do_table: replicate_wild_ignore_table: last_errno: 1146 last_error: error 'table 'mycat.test1' doesn't exist' on query. default database: 'mycat'. query: 'insert into test1(`id`,`name`) value(2,"tom")'
2)、在编号3插入数据
下面我们再编号3插入数据,看编号1、2能不能看到
在编号3操作:
mysql> insert into test1(`id`,`name`) value(3,"kitt"); query ok, 1 row affected (0.01 sec) mysql> select * from test1; +----+-------+ | id | name | +----+-------+ | 1 | petty | | 2 | tom | | 3 | kitt | +----+-------+ 3 rows in set (0.00 sec) mysql>
在编号1查看
mysql> select * from test1; +----+-------+ | id | name | +----+-------+ | 1 | petty | | 2 | tom | | 3 | kitt | +----+-------+ 3 rows in set (0.00 sec) mysql>
在编号2查看
mysql> select * from test1; +----+-------+ | id | name | +----+-------+ | 1 | petty | | 2 | tom | | 3 | kitt | +----+-------+ 3 rows in set (0.00 sec) mysql>
可以看到目前我们已经同步成功了,在编号1中能查看到主机编号3的插入信息。
3)、编号1处理数据
下面我们在编号1操作查看
编号1:
mysql> insert into test1(`id`,`name`) value(4,"lisa"); query ok, 1 row affected (0.00 sec) mysql> select * from test1; +----+-------+ | id | name | +----+-------+ | 1 | petty | | 2 | tom | | 3 | kitt | | 4 | lisa | +----+-------+ 4 rows in set (0.00 sec) mysql>
编号3:
mysql> select * from test1; +----+-------+ | id | name | +----+-------+ | 1 | petty | | 2 | tom | | 3 | kitt | | 4 | lisa | +----+-------+ 4 rows in set (0.00 sec) mysql>
可以看到其是相互同步的。
到此这篇关于mysql实现主从配置和多主多从配置的文章就介绍到这了,更多相关mysql 主从配置和多主多从配置内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!