Mysql主从复制及读写分离的实现
Mysql5.6基于GTID的主从复制及使用Amoeba配置读写分离一、简介二、Mysql主从配置三、读写分离配置一、Amoeba简介Amoeba(变形虫)项目,该开源框架于2008年开始发布
Mysql 5.6 基于GTID的主从复制及使用Amoeba配置读写分离
一、简介
二、Mysql主从配置
三、读写分离配置
一、Amoeba简介
Amoeba(变形虫)项目,该开源框架于2008年开始发布一款 Amoeba for Mysql软件。这个软件致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQL的 时候充当SQL路由功能,专注于分布式数据库代理层(Database Proxy)开发。座落与 Client、DB Server(s)之间,对客户端透明。具有负载均衡、高可用性、SQL 过滤、读写分离、可路由相关的到目标数据库、可并发请求多台数据库合并结果。 通过Amoeba你能够完成多数据源的高可用、负载均衡、数据切片的功能,目前Amoeba已在很多企业的生产线上面使用
Amoeba优缺点
优点:
1、降低费用,简单易用
2、提高系统整体可用性
3、易于扩展处理能力与系统规模
4、可以直接实现读写分离及负载均衡效果,而不用修改代码
缺点:
1、不支持事务与存储过程
2、暂不支持分库分表,amoeba目前只做到分数据库实例
3、不适合从amoeba导数据的场景或者对大数据量查询的query并不合适(比如一次请求返回10w以上甚至更多数据的场合)
Mysql GTID
Mysql 5.6的新特性之一,加入了全局事务性ID(GTID:Global Transactions Identifier)来强化数据库的主备一致性,故障恢复,以及容错能力;也使得复制功能的配置、监控及管理变得更加易于实现,且更加健壮
二、Mysql主从配置
1、环境介绍:
两台Mysql数据库实现主从配置,172.16.14.2主机为Master;172.16.14.3为Slave
2、在Master服务器上安装并配置Mysql
######安装Mysql并加入到系统服务 [root@master ~]# tar xf mysql-5.6.13-linux-glibc2.5-x86_64.tar.gz -C /usr/local/ [root@master ~]# cd /usr/local/ [root@master local]# ln -s mysql-5.6.13-linux-glibc2.5-x86_64 mysql [root@master local]# cd mysql [root@master mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@master mysql]# chmod +x /etc/init.d/mysqld [root@master mysql]# chkconfig --add mysqld [root@master mysql]# echo "PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile [root@master mysql]# . /etc/profile ---------------------------------------------------------------------- ######提供主配置文件 [root@master mysql]# vim /etc/my.cnf [client] #password = your_password port = 3306 socket = /tmp/mysql.sock [mysqld] port = 3306 socket = /tmp/mysql.sock skip-external-locking key_buffer_size = 256M max_allowed_packet = 1M table_open_cache = 256 sort_buffer_size = 1M read_buffer_size = 1M read_rnd_buffer_size = 4M myisam_sort_buffer_size = 64M thread_cache_size = 8 query_cache_size= 16M thread_concurrency = 8 binlog-format=ROW log-slave-updates=true gtid-mode=on enforce-gtid-consistency=true master-info-repository=TABLE relay-log-info-repository=TABLE sync-master-info=1 slave-parallel-workers=2 binlog-checksum=CRC32 master-verify-checksum=1 slave-sql-verify-checksum=1 binlog-rows-query-log_events=1 report-port=3306 datadir=/data report-host=master.allen.com log-bin=mysql-bin server-id = 10 [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash [myisamchk] key_buffer_size = 128M sort_buffer_size = 128M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout ---------------------------------------------------------------------- ######初始化Mysql [root@master mysql]# useradd -r mysql [root@master mysql]# mkdir /data [root@master mysql]# chown -R mysql.mysql /data [root@master mysql]# chown -R root.mysql /usr/local/mysql/* [root@master mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/ [root@master ~]# service mysqld start3、在Slave服务器上安装Mysql与在Master服务器上安装方法相同,这里不在介绍,,而在Slave服务器上安装Mysql有两个参数与Master服务器不同;如下
4、在Master服务器上为Slave创建复制用户并测试连接
5、启动从节点的复制线程
[root@slave ~]# mysql mysql> change master to master_host='172.16.14.2',master_user='slave',master_password='passwd',master_auto_position=1; mysql> start slave; mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.16.14.2 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000005 Read_Master_Log_Pos: 191 Relay_Log_File: slave-relay-bin.000003 Relay_Log_Pos: 401 Relay_Master_Log_File: mysql-bin.000005 Slave_IO_Running: Yes #主要看这两项为“YES”说明成功 Slave_SQL_Running: Yes6、在Master服务器创建数据库查看Slave服务器是否更新
至此Mysql 5.6 基于GTID的复制已经完成,下面将介绍如何基于Mysql的主从复制架构做读写分离
三、读写分离配置
1、基于前面做的Mysql主从架构,然后在前端加一台服务器,用于实现Mysql的读写分离,IP地址为:172.16.14.1;由于Amoeba是java程序所研发,所以需要先安装JDK程序

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.
