Mysql和MariaDB的最基本的集群方式是主从方式,也是众多中小企业使用最多的集群方式,该集群部署简单,轻松上手,让我们一步步来吧。 测试所用版本为MariaDB 10.1 源码安装,安装到默认路径/usr/local/mysql下。 $BUILD/autorun.sh$./configure$make-j4$sudo
Mysql和MariaDB的最基本的集群方式是主从方式,也是众多中小企业使用最多的集群方式,该集群部署简单,轻松上手,让我们一步步来吧。
测试所用版本为MariaDB 10.1 源码安装,安装到默认路径/usr/local/mysql下。
$ BUILD/autorun.sh $ ./configure $ make -j4 $ sudo make install
既然是主从数据库,至少需要两个数据库实例,本文例子将两个实例部署在同一台电脑里,计划配置如下表。
实例作用 |
地址 |
端口 |
数据文件路径 |
配置文件路径 |
主 |
localhost |
10001 |
/home/lyw/db/data/1 |
/home/lyw/db/etc/my1.cnf |
从 |
localhost |
10002 |
/home/lyw/db/data/2 | /home/lyw/db/etc/my2.cnf |
准备两个mysql的配置文件my1.cnf, 我们从默认配置复制过来
$ mkdir -p /home/lyw/db/etc/ $ cd /usr/local/mysql $ cp support-files/my-medium.cnf /home/lyw/db/etc/my1.cnf $ cp support-files/my-medium.cnf /home/lyw/db/etc/my2.cnf
my1.cnf修改如下几行
[mysqld] port = 10001 socket = /tmp/mysql1.sock log-bin = mysql-bin server-id = 10001 datadir = /home/lyw/db/data/1 user = lyw
my2.cnf修改如下几行
[mysqld] port = 10002 socket = /tmp/mysql2.sock log-bin = mysql-bin server-id = 10002 datadir = /home/lyw/db/data/2 user = lyw
分别初始化两个数据库
$ mkdir -p /home/lyw/db/data/ $ scripts/mysql_install_db --defaults-file=/home/lyw/db/etc/my1.cnf $ scripts/mysql_install_db --defaults-file=/home/lyw/db/etc/my2.cnf
启动两个数据库
由于是在一台电脑里,因此没有使用服务启动,而是直接用mysqld_safe启动,大家尽可根据自己的情况启动。
$ bin/mysqld_safe --defaults-file=/home/lyw/db/etc/my1.cnf & $ bin/mysqld_safe --defaults-file=/home/lyw/db/etc/my2.cnf &
检查两个数据库是否启动成功,对应的端口和sock文件已经存在,即表示启动成功。
$ sudo netstat -nlp |grep mysql tcp6 0 0 :::10001 :::* LISTEN 22211/mysqld tcp6 0 0 :::10002 :::* LISTEN 23536/mysqld unix 2 [ ACC ] STREAM LISTENING 365439 22211/mysqld /tmp/mysql1.sock unix 2 [ ACC ] STREAM LISTENING 368511 23536/mysqld /tmp/mysql2.sock
MariaDB默认有两行任何本机用户都能登陆,会影响新创建的复制用户的登陆,因此需要删除。然后创建复制用户方可生效。(如果是在两台不同的电脑中运行,可以不删除这两行。)
$ bin/mysql -S /tmp/mysql1.sock -uroot MariaDB [(none)]> use mysql; MariaDB [mysql]> delete from user where user=''; MariaDB [mysql]> flush privileges; MariaDB [mysql]> grant replication slave on *.* to 'rep'@'%' identified by '123456';
然后查看master数据库的运行状态,记下这两个值。
MariaDB [mysql]> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000005 | 1075 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
在从服务器上执行
$ bin/mysql -S /tmp/mysql2.sock -uroot MariaDB [(none)]> change master to master_host='localhost', master_port=10001, master_user='rep', master_password='123456', master_log_file='mysql-bin.000005', master_log_pos=1075; MariaDB [(none)]> start slave; MariaDB [(none)]> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: localhost Master_User: rep Master_Port: 10001 Connect_Retry: 60 Master_Log_File: mysql-bin.000005 Read_Master_Log_Pos: 1075 Relay_Log_File: lyw-hp-relay-bin.000002 Relay_Log_Pos: 537 Relay_Master_Log_File: mysql-bin.000005 Slave_IO_Running: Yes Slave_SQL_Running: Yes
如果Slave_IO_Running 和 Slave_SQL_Running 都为Yes,则说明启动主从配置成功。
测试:
在master数据库执行
MariaDB [mysql]> create database lyw; Query OK, 1 row affected (0.00 sec) MariaDB [mysql]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | lyw | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec)
在slave数据库执行
MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | lyw | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec)
可见在主库创建的数据库lyw,在从库也有。可以在主库尝试执行其他写入语句,在丛库也能看到同样的数据。
用同样的方法,一个主数据库后面可以跟多个从数据库,增加数据的可靠性和读的吞吐量。
如果主库也配置上从从库同步数据,即互为主从,那就是常说的双主,或主主,有些文章里会说修改下自增涨id以防止冲突,我却不这样认为,双主的两个数据库并非强一致,而是有一定的延迟,很多事务处理会出现错误,皆因为延迟导致。双主并不能大大增强写入的吞吐量,因为所有数据都要在每个库写入。虽然说因为批量的缘故可以有少量提升,但是对于延迟导致的错误来说,微不足道。
配置双主是有意义的,但是并非用于同时写入,同一时间还是只写一个库,而当主挂了后,为切换主从做的准备,切换后,主再次启动时,立马就能同步到从的数据。比如在cobar集群中,就可以考虑用双主配置。
以上是传统主从复制方法,从Mysql5.6和MariaDB10.0开始,有了新的主从复制方式,可参考这篇文章:http://bangbangba.blog.51cto.com/3180873/1702294

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.


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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor
