为什么mysql要使用主从模型-创新互联
本文主要给大家介绍为什么mysql要使用主从模型,文章内容都是笔者用心摘选和编辑的,具有一定的针对性,对大家的参考意义还是比较大的,下面跟笔者一起了解下为什么mysql要使用主从模型吧。
成都创新互联长期为上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为洛浦企业提供专业的做网站、成都网站建设,洛浦网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。主从复制的原理:主云服务器(master)上的二进制日志(binlog)中记录的操作,可以在从云服务器(slave)上的中继日志(relaylog)得到重放,进而可以实现数据的同步,保证数据的一致性;
主从复制的前提条件:
1. 主云服务器的数据需要先进行一次完全备份,在从云服务器上恢复;
2. 开启免密ssh登录;
3. 主云服务器上开启二进制日志,写入server_id,sync_binlog参数设置为1;
4. 从云服务器上开启中继日志,写入server_id,开启读锁(从云服务器只可读不可写);
从云服务器上对复制相关的线程(show slave status\G);
Slave_IO_Running: Yes
IO_thread:用于和Master相连接,监控和接收Master的二进制日志;
Slave_SQL_Running: Yes
SQL_thread:用于监控,读取和重放中继日志的日志信息,并将数据写入到数据库中;
根据从云服务器上对复制的线程,我们就可以知道,主从复制就是,master上的操作都被记录在binlog中,然后slave上的IO_thread就将master中的binlog记录的内容复制到本地的relaylog中,最后SQL_thread就将relaylog记录的内容重放,达到数据一致的目标;
1.首先需要master和slave可以免密通信;
2.对master和slave的主配置文件进行修改;
Master的mysql主配置文件:(默认/etc/my.cnf)
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd innodb_file_per_table=ON skip_name_resolve=ON log_bin=/var/lib/mysql/binlog #开启二进制日志 server_id=101 #赋予一个id sync_binlog=1 #二进制日志同步至磁盘上 innodb_flush_log_at_trx_commit=1 #每执行完一个事务后,及时写入磁盘 [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid # # include all files from the config directory # !includedir /etc/my.cnf.d
Slave的mysql的主配置文件(默认/etc/my.cnf);
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd innodb_file_per_table=ON skip_name_resolve=ON server_id=201 #赋予一个server_id read_only=ON #开启读锁; relay_log=slavelog #开启slave日志 [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid # # include all files from the config directory # !includedir /etc/my.cnf.d
3.启动master,将所有的数据库进行完全备份,并在slave上进行重放;
[root@master ~]# mysqldump -uroot -hlocalhost --all-databases -p > alldata.sql Enter password:
[root@master ~]# scp alldata.sql 172.16.75.2:/ alldata.sql 100% 2803KB 22.1MB/s 00:00
[root@slave ~]# mysql -uroot -p < /alldata.sql Enter password:
5.master授权一个具有replication的用户,可以让slave用于登录master进行复制日志内容,并记录当前二进制日志文件名及坐标(show master logs);
MariaDB [(none)]> grant replication slave on *.* to 'repuser'@'%' identified by 'reppass';
MariaDB [(none)]> show master logs; +---------------+-----------+ | Log_name | File_size | +---------------+-----------+ | binlog.000001 | 30379 | | binlog.000002 | 1038814 | | binlog.000003 | 9598 | | binlog.000004 | 647 | | binlog.000005 | 285 | | binlog.000006 | 720 | | binlog.000007 | 264 | | binlog.000008 | 264 | | binlog.000009 | 264 | | binlog.000010 | 264 | | binlog.000011 | 12140997 | +---------------+-----------+ 11 rows in set (0.00 sec)
6.在slave上使用授权用户进行指定master的相关属性信息,并启动复制线程;
MariaDB [(none)]> change master to master_host='172.16.75.1',master_user='repuser',master_password='reppass',master_port=3306,master_log_file='binlog.000011',master_log_pos=12140997; MariaDB [(none)]> start slave; MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.16.75.1 Master_User: repuser2 Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000011 Read_Master_Log_Pos: 12140997 Relay_Log_File: slavelog.000018 Relay_Log_Pos: 12141278 Relay_Master_Log_File: binlog.000011 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: 12140997 Relay_Log_Space: 12141846 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: 101 1 row in set (0.00 sec)
至此,主从模型搭建完毕,当我们在master上进行数据操作时,slave上也会进行记录,并重放至本地数据中;
验证:master上创建一个数据表,slave上也查看;
master端:
MariaDB [hellodb]> create table stu_info(SID int auto_increment not null primary key); Query OK, 0 rows affected (0.10 sec) MariaDB [hellodb]> desc stu_info; +-------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+----------------+ | SID | int(11) | NO | PRI | NULL | auto_increment | +-------+---------+------+-----+---------+----------------+ 1 row in set (0.00 sec)
slave端:
MariaDB [(none)]> show tables from hellodb; +-------------------+ | Tables_in_hellodb | +-------------------+ | classes | | coc | | courses | | scores | | students | | teachers | | toc | +-------------------+ 7 rows in set (0.00 sec) MariaDB [(none)]> desc hellodb.stu_info; +-------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+----------------+ | SID | int(11) | NO | PRI | NULL | auto_increment | +-------+---------+------+-----+---------+----------------+ 1 row in set (0.03 sec)
看完以上关于为什么mysql要使用主从模型,很多读者朋友肯定多少有一定的了解,如需获取更多的行业知识信息 ,可以持续关注我们的行业资讯栏目的。
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
新闻标题:为什么mysql要使用主从模型-创新互联
路径分享:http://cdiso.cn/article/dcooei.html