MySQL的Master/Slave集群安装和配置_MySQL
本文讲述MySQL的Master/Slave集群安装和配置,安装的版本是最新的稳定版本GA 5.6.19。
为了支持有限的HA,我们使用Master/Slave简单的读写分离集群。有限的HA是指当Master不可用时,数据不会丢失,但在Master宕机的情况下是不可写的,必须手工处理故障。如果要支持更高的可用性,可以使用两台Master来做热切换。
Master和Slave的MySQL安装是相同的,只是my.cnf的配置不同,需要配置二进制日志文件复制。
没有特殊说明,命名中带#的为root用户操作,带$的为mysql Linux用户的操作。
安装准备
1. 在安装MySQL前,需要确认下面的系统软件已经安装在Linux中。
软件名称 |
软件描述 |
gcc-4.4.7 |
编程语言编译器 |
gcc-c++-4.4.7 |
C++语言编译器 |
cmake-2.6.4-5 |
跨平台的开源构建系统 |
ncurses-devel-5.7-3.20090208 |
控制打印控制台屏幕 |
# groupadd mysql
# useradd -g mysql mysql
# passwd mysql
3. 准备安装目录
创建MySQL安装目录,并赋权限给mysql用户:
# mkdir /usr/local/mysql-5.6.19
# chown mysql:mysql /usr/local/mysql-5.6.19
# chmod -R 770 /usr/local/mysql-5.6.19
4. 创建MySQL数据存储目录:
# mkdir /data
# mkdir /data/mysql
# chown mysql:mysql /data/mysql
5. 授权MySQL解压源码目录/usr/local/src目录的可执行权限给所有用户:
# chmod -R 757 /usr/local/src
安装MySQL
1. 解压缩安装包:
$ cd /usr/local/src
$ tar -xzvf mysql-5.6.19.tar.gz
2. 配置MySQL编译参数
$ cd /usr/local/src/mysql-5.6.19
$ cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.6.19\
-DMYSQL_DATADIR=$MYSQL_DATA_PATH\
-DSYSCONFDIR=/usr/local/mysql-5.6.19/conf\
-DDEFAULT_CHARSET=utf8\
-DDEFAULT_COLLATION=utf8_general_ci\
-DWITH_READLINE=1\
-DWITH_INNOBASE_STORAGE_ENGINE=1\
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1备注:
-DCMAKE_INSTALL_PREFIX: 配置MySQL的安装目录。
-DMYSQL_DATADIR: 配置MySQL的数据目录。
-DSYSCONFDIR: 配置MySQL的配置文件目录。
-DDEFAULT_CHARSET: 默认字符集。
-DDEFAULT_COLLATION:设定默认语言的排序规则。
-DWITH_READLINE:支持批量导入mysql数据。
-DWITH_INNOBASE_STORAGE_ENGINE:使用INNOBASE存储引擎。
-DWITH_ARCHIVE_STORAGE_ENGINE:常应用于日志记录和聚合分析,不支持索引。
-DWITH_BLACKHOLE_STORAGE_ENGINE:黑洞存储引擎。
-DWITH_PERFSCHEMA_STORAGE_ENGINE:性能模式引擎。3. 执行make
$ make
$ make install
4. 修改myql用户的环境变量,增加MYSQL_HOME,并把bin加到PATH:
$ vi ~/.bash_profile
在文件中增加蓝色字体部分:
# User specific environment and startup programs # MySQL home目录 export MYSQL_HOME=/usr/local/mysql-5.6.19
PATH=$PATH:$HOME/bin:$MYSQL_HOME/bin
export PATH |
$ source ~/.bash_profile
初始化MySQL
1. 安装service脚本
# cp /usr/local/mysql-5.6.19/support-files/mysql.server/etc/init.d/mysqld
# chown mysql:mysql/etc/init.d/mysqld
# chmod 700 /etc/init.d/mysqld
2. 创建mysql权限数据库
$ $MYSQL_HOME/scripts/mysql_install_db--basedir=$MYSQL_HOME --datadir=/data/mysql
3. 创建PID文件目录
$ mkdir $MYSQL_HOME/var
4. 为master配置my.cnf
my.cnf格式不正确,很容易在启动时错误,最好在原有文件的基础上通过vi工具在linux上修改。
如果文件已经损坏,可以通过默认的模板中拷贝:
$ cp $MYSQL_HOME/support-files/my-default.cnf$MYSQL_HOME/my.cnf
编辑my.cnf
$ vi $MYSQL_HOME/my.cnf
在文件中增加蓝色字体部分:
# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html [mysqld] # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. innodb_buffer_pool_size = 256M innodb_flush_log_at_trx_commit=1 # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. log_bin=master-bin log_bin_index=master-bin.index # These are commonly set, remove the # and set as required. # basedir = ..... datadir = /data/mysql port = 3306 # first master server id server_id = 1 socket = /tmp/mysql.sock pid-file = /usr/local/mysql-5.6.19/var/master.pid # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES |
5. 为slave配置my.cnf
和master的配置文件一样进行修改,但注意文件中的log文件名、datadir和server_id等内容不同。
编辑my.cnf
$ vi $MYSQL_HOME/my.cnf
在文件中增加蓝色字体部分:
# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html [mysqld] # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. innodb_buffer_pool_size = 256M innodb_flush_log_at_trx_commit=1 # Replication # relay-log=slave-relay-bin # relay-log-index=slave-relay-bin.index # These are commonly set, remove the # and set as required. # basedir = ..... datadir = /data/mysql port = 3306 # first slave server id of master 1 server_id = 101 socket = /tmp/mysql.sock pid-file = /usr/local/mysql-5.6.19/var/slave.pid # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES |
6. 启动MySQL
启动和停止master和slave都完全一样。
$ service mysqldstart
通过下面命令查看是否启动成功:
$ service mysqldstatus
7. 停止MySQL
$ service mysqld stop
管理MySQL安全性
默认MySQL的root的用户密码为空,为了提高安全性,应该设置root用户一个安全的密码。
在服务器上通过mysql用户打开MySQL客户端:$ mysql –u root
设置一个安全的密码:
mysql> SET PASSWORD FORroot@localhost=PASSWORD('secret');
配置Master和Slave之间的复制
1. 创建执行复制的MySQL用户
在Master上创建一个复制用户,其中secret为用户的密码:
mysql> CREATEUSER repl_user IDENTIFIED BY 'secret';
并赋给复制权限:
mysql>GRANTREPLICATION SLAVE ON *.* TO repl_user;
2. 锁定Master并获取二进制日志位置值
获取读锁:
mysql> FLUSH TABLES WITHREAD LOCK;
显示当前二进制文件名及位置值:
mysql> SHOW MASTER STATUS;3. 通过mysqldump工具获取Master数据快照
在另外一个会话中在Master上执行:
$ mysqldump -u root-p --all-databases --master-data > data_dump.sql
执行后,保存在当前目录中。4. 释放Master上的读锁
在获取读锁的会话中,执行释放锁命令:
mysql> UNLOCK TABLES;5. 导入dump数据到Slave中
通过scp拷贝data_dump.sql到Slave的机器上。
$ scp data_dump.sqlmysql@
在Slave上通过下面的命令导入到Slave的MySQL中。
$ mysql –u root -p
mysql> source data_dump.sql;6. 配置Master和Slave之间的复制
mysql> CHANGE MASTER TO MASTER_HOST='
MASTER_USER='repl_user',
MASTER_PASSWORD='secret',
MASTER_PORT = 3306,
MASTER_LOG_FILE='master-bin.000003',
MASTER_LOG_POS=881;
上面的MASTER_LOG_FILE和MASTER_LOG_POS需要配置6.2中查询到的信息。
mysql> START SLAVE;
7. 验证Master和Slave的状态
在Master上执行:
mysql> SHOW MASTER STATUS;在Slave上执行:
mysql> SHOW SLAVE STATUS;上面信息显示没有任何错误,Slave_IO_State信息为‘Waitigfor master to send event’,说明复制连接配置OK。
至此,两个MySQL的Master/Slave模式的集群已经部署成功,可以在Master上执行数据更新操作,发现可以正常复制到Slave上。

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



Scenario description for nodes to completely evacuate from ProxmoxVE and rejoin the cluster. When a node in the ProxmoxVE cluster is damaged and cannot be repaired quickly, the faulty node needs to be kicked out of the cluster cleanly and the residual information must be cleaned up. Otherwise, new nodes using the IP address used by the faulty node will not be able to join the cluster normally; similarly, after the faulty node that has separated from the cluster is repaired, although it has nothing to do with the cluster, it will not be able to access the web management of this single node. In the background, information about other nodes in the original ProxmoxVE cluster will appear, which is very annoying. Evict nodes from the cluster. If ProxmoxVE is a Ceph hyper-converged cluster, you need to log in to any node in the cluster (except the node you want to delete) on the host system Debian, and run the command

PHP database optimization method in high concurrency environment With the rapid development of the Internet, more and more websites and applications need to face high concurrency challenges. In this case, database performance optimization becomes particularly important, especially for systems that use PHP as the back-end development language. This article will introduce some database optimization methods in PHP high concurrency environment and give corresponding code examples. Using connection pooling In a high-concurrency environment, frequent creation and destruction of database connections may cause performance bottlenecks. Therefore, using connection pooling can

In today's cloud computing era, containerization technology has become one of the most popular technologies in the open source world. The emergence of Docker has made cloud computing more convenient and efficient, and has become an indispensable tool for developers and operation and maintenance personnel. The application of multi-node cluster technology is widely used based on Docker. Through multi-node cluster deployment, we can utilize resources more efficiently, improve reliability and scalability, and also be more flexible in deployment and management. Next, we will introduce how to use Docker to

Common clusters in PHP include LAMP cluster, Nginx cluster, Memcached cluster, Redis cluster and Hadoop cluster. Detailed introduction: 1. LAMP cluster. LAMP refers to a combination of Linux, Apache, MySQL and PHP. It is a common PHP development environment. In a LAMP cluster, multiple servers run the same application and are balanced through a load balancer. Requests are distributed to different servers; 2. Nginx cluster, Nginx is a high-performance web server and so on.

How to use MongoDB to implement data clustering and load balancing functions Introduction: In today's big data era, the rapid growth of data volume has put forward higher requirements for database performance. In order to meet these requirements, data clustering and load balancing have become indispensable technical means. As a mature NoSQL database, MongoDB provides rich functions and tools to support data clustering and load balancing. This article will introduce how to use MongoDB to implement data clustering and load balancing functions, and provide specific code

Workerman is a high-performance PHPSocket framework that allows PHP to handle asynchronous network communication more efficiently. In Workerman's documentation, there are detailed instructions and code examples on how to implement a server cluster. In order to implement a server cluster, we first need to clarify the concept of a server cluster. A server cluster connects multiple servers to a network to improve system performance, reliability and scalability by sharing loads and resources. In Workerman, you can use the following two methods

How to configure the cluster environment of MySQL database? Introduction: With the development of the Internet and the continuous growth of data volume, the database has become one of the core systems necessary for every enterprise. At the same time, in order to ensure high data availability and read and write performance requirements, database cluster environments have gradually become the choice of enterprises. This article will introduce how to configure the cluster environment of MySQL database and provide corresponding code examples. 1. Environment preparation Before configuring the cluster environment of the MySQL database, we need to ensure that the following environment preparations have been completed: Install M

MySQL cluster deployment and maintenance project experience summary MySQL is one of the most commonly used databases in Internet application development. As the business scale continues to expand, stand-alone MySQL can no longer meet the demand. Therefore, it is necessary to consider using the MySQL cluster solution to expand the performance and capacity of the database. . This article summarizes the experience and lessons learned in the MySQL cluster deployment and maintenance process, hoping to be helpful to everyone's MySQL cluster deployment and maintenance work. 1. Deployment and architecture design of MySQL cluster MySQL cluster
