Mysql集群
mysql集群架构方式很多,根据不同的需求做不一样的架构,简单一点的就是mysql的replication,也就是Mysql的复制功能,模式有:master-slaves,master-slaves-slaves,master-master-slaves等可以有多个分层,那么现在我所要说的是master-slaves的模式(其他的模式原理基本都一样),然后再通过mysql官方提供的Mysql-proxy实现读写分离,达到负载均衡的效果。
环境:
主机:master:192.168.1.109,slave1:192.168.1.110,mysqlProxy:192.168.1.112。(workstation10平台创建虚拟机模拟)
操作系统:Red Hat Enterprise Linux Server release 5.8
mysql:mysql-5.5.37-linux2.6-i686.tar.gz
mysql-proxy:mysql-proxy-0.8.4-linux-glibc2.3-x86-32bit.tar.gz
1、安装mysql:
[root@localhost ~]# cd /usr/local //进入到安装目录
[root@localhost local]# tar -zxvf mysql-5.5.37-linux2.6-i686.tar.gz //解压mysql二进制包
[root@localhost local]# ln -s mysql-5.5.37-linux2.6-i686 mysql //解压之后加上链接
[root@localhost local]# groupadd mysql //增加mysql用户组
[root@localhost local]# useradd -g mysql mysql //增加mysql用户帐号
[root@localhost local]# cd mysql //进入安装之后的mysql目录
[root@localhost mysql]# chown -R root:mysql . //修改文件属组和属主,注意该命令后面有一个点,表示当前目录
[root@localhost mysql]# chown -R mysql:mysql data/ //把data目录的属主改为mysql
[root@localhost mysql]# cp support-files/my-large.cnf /etc/my.cnf //拷贝mysql配置文件放在etc目录下并改名为my.cnf
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld //拷贝mysql的启动脚本放在/etc/init.d/目录下
[root@localhost mysql]# service mysqld start //启动mysql
至此mysql安装已经结束,接下来就是mysql的主从配置。(其他两台主机也照此安装mysql)
2、配置master:
[root@localhost mysql]# vi /etc/my.cnf //编辑配置文件
server-id=1 //默认是1,不是的话则改为1
thread_concurrency = 2 //线程并发数(CPU*2)
log-bin=mysql-bin //打开二进制日志功能
保存退出,重新启动mysql。
登入mysql:
[root@localhost mysql]# mysql -uroot -p //默认密码空,直接敲回车进入
mysql> grant replication slave on *.* to repl@'192.168.1.%' identified by '123456';
mysql>flush privileges;
mysql>show grants for 'repl'@'192.168.1.%'; //查看授权,有记录说明ok
mysql>show master status/G; //查看master服务器状态,有二进制日志文件名和记录位置(position)
至此mysql配置完成。
3、配置slave
[root@localhost mysql]#vi /etc/my.cnf //编辑配置文件
server-id=11 //设置与master不一样就行,
thread_concurrency = 2 //线程并发数(CPU*2)
relay-log=mysql-log //打开中继日志
relay-log-index=mysql-log.index //设置relay-log-index文件名
保存退出,重新启动mysql。
登入mysql:
[root@localhost mysql]# mysql -uroot -p //默认密码空,直接敲回车进入
mysql>change master to
->master_host='192.168.1.109', //master服务器ip
->mastet_user='repl', //用户名
->mastet_password='123456', //密码
->master_log_file='mysql-bin.000001', //master服务器二进制日志名
->master_log_pos=107;
mysql>slave start; //启动slave
mysql> SHOW SLAVE STATUS/G; //查看slave状态
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.1.109
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 4
Relay_Log_File: mysql-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: yes
Slave_SQL_Running: yes
...omitted...
Seconds_Behind_Master: NULL
如果 Slave_IO_Running和Slave_SQL_Running显示为yes则配置ok,过程中可能会出现:
Slave I/O: error connecting to master 'repl@192.168.1.109:3306' - retry-time: 60 retries: 86400, Error_code: 2003
引起问题原因是防火墙拦截了,解决办法就是修改防火墙配置,简单直接的办法就是把master服务器的防火墙关掉,执行/etc/init.d/iptables stop命令。
接下来测试,在master中创建一个数据库db_test,查看slave中是是否存在db_test。
至此slave配置结束,其他从服务器按此方式配置。
4、配置mysql-proxy,代理服务最好另外部署一台机器,尤其是上线运行后,所以这里测试也是用了单独一台虚拟机,没有部署在master或slave机器上。
[root@localhost local]# tar -zxvf mysql-proxy-0.8.4-linux-glibc2.3-x86-32bit.tar.gz //解压
[root@localhost local]# ln -s mysql-proxy-0.8.4-linux-glibc2.3-x86-32bit mysql-proxy //加一个快捷链接
[root@localhost local]# groupadd mysql-proxy //创建用户组
[root@localhost local]# useradd -g mysql-proxy mysql-proxy //创建用户
[root@localhost local]# cd mysql-proxy //进入mysql-proxy目录
[root@localhost mysql-proxy]# chown -R root:mysql-proxy . //更改目录属主和属组,命令后面有一个点(.)表示当前目录
[root@localhost mysql-proxy]# vi /etc/profile.d/mysql-proxy.sh //把mysql的bin目录加到PATH变量中,
export
PATH=$PATH:
/usr/local/mysql-proxy/bin
[root@localhost mysql-proxy]# source /etc/profile //让配置文件立刻生效
[root@localhost mysql-proxy]# mysql-proxy --help-all //查看mysql-proxy命令,出现如下信息:
<br>
mysql-proxy 命令
--help-all ———— 用于获取全部帮助信息
--proxy-address=host:port ———— 代理服务监听的地址和端口
--admin-address=host:port ———— 管理模块监听的地址和端口
--proxy-backend-addresses=host:port ———— 后端mysql服务器的地址和端口(主服务器)
--proxy-read-only-backend-addresses=host:port ———— 后端只读mysql服务器的地址和端口(从服务器)
--proxy-lua-script=file ———— 完成mysql代理功能的Lua脚本
--daemon ———— 以守护进程模式启动mysql-proxy
--defaults-file=/path/to/conf_file_name ———— 默认使用的配置文件路径
--log-file=/path/to/log_file_name ———— 日志文件名称
--log-level=level ———— 日志级别
--log-use-syslog ———— 基于syslog记录日志
--user=user_name ———— 运行mysql-proxy进程的用户
2014-05-10 00:09:22: (critical) plugin proxy 0.8.4 started<br>2014-05-10 00:09:22: (debug) max open file-descriptors = 1024<br>2014-05-10 00:09:22: (message) proxy listening on port :4040<br>2014-05-10 00:09:22: (message) added read/write backend: 192.168.1.109:3306<br>2014-05-10 00:09:22: (message) added read-only backend: 192.168.1.110:3306
[root@localhost mysql-proxy]#netstat -ntulp | grep :4040 //查看监听的端口
tcp 0 0 0.0.0.0:4040 0.0.0.0:* LISTEN 10056/mysql-proxy
现在我们需要在master服务器中创建一个远程登入的mysql账号
master:
mysql> GRANT ALL ON *.* TO root@
'192.168.1.%'
IDENTIFIED BY
'123456'
;
Query OK, 0 rows affected (0.07 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.04 sec)
在从服务器上做测试:slave:[root@bogon ~]
# mysql -uroot -p123456 -h192.168.1.112 --port=4040


MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
