Mysql high availability and high performance storage application series 3 - mysqld_multi configuration master-slave cluster

overview

The problems to be solved by master-slave replication are: 1) Write operations lock the table, which affects read operations and business. 2) Database backup. 3) With the increase of data, I/O operations increase, and a bottleneck occurs on a single machine.

Master-slave replication is the master node of the slave server, which replicates to multiple slave nodes, and adopts asynchronous mode by default.

Master-slave replication principle

The slave library server will open two separate threads, the I/O thread and the Sql process.

  • I/O thread: Responsible for connecting to the master after the connection is successful, sleep and wait for the master to generate new events, and save new events in its own relay log. The relay log is usually located in the cache of the operating system, so the overhead is very small.
  • Sql process: responsible for executing sql operations in the relay log.

Install Mysql from source code

Use cmake to install mysql-5.7.37, the Linux system is centos7, not much to say, let's start, the preparation before installation is very important.

1. Uninstall the system's own mysql

# rpm -qa | grep mysql
# rpm -qa | grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64

# rpm -e --nodeps mariadb*
# yum remove mariadb-libs-5.5.60-1.el7_5.x86_64

2. Add users and directory planning that mysql prohibits login

useradd -M -s /sbin/nologin mysql
# 软件目录(为mysql安装目录创建软链接或改目录名)
# ln -s  /usr/local/mysql-5.7.37 /usr/local/mysql

# 数据目录
# mkdir /usr/local/mysql-5.7.37/data


# 日志目录
# mkdir /var/log/mysql
# touch /var/log/mysql/mysqld.log
# chown -R mysql.mysql /var/log/mysql/mysqld.log

# 其他目录()
# mkdir /usr/local/mysql-5.7.37/tmp
# chown -R mysql.mysql /usr/local/mysql-5.7.37

3. Download and decompress the source code package, and download the boost software

boots A collection of C++ libraries that provide functions such as linear algebra, pseudo-random number generation, multi-threading, image processing, regular expressions, and unit testing.

# wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
# tar xf boost_1_59_0.tar.gz -C /usr/local

4. Install the dependencies before compiling

# yum install -y ncurses-devel libaio-devel gcc gcc-c++ glibc cmake autoconf openssl openssl-devel libstdc++*  libtool lrzsz  psmisc

5. Perform cmake to compile the mysql source file

The installation time after make is relatively long. My computer has executed it for nearly an hour and a half. The following is the parameter description during installation:

DCMAKE_INSTALL_PREFIX: Specify the installation directory of the MySQL program, default /usr/local/mysql
DEFAULT_CHARSET: Specify the default character set of the server, default latin1
DWITH_EXTRA_CHARSET=all: Support extended character set
DEFAULT_COLLATION: Specify the default proofreading rules of the server, default latin1_general_ci
DENABLED_LOCAL_INFILE: You can use load The data infile command imports files from the local, the default is OFF. (-DENABLED_LOCAL_INFILE=1 supports converting local files into database data)
DWITH_READLINE=1 Use the readline function, which is convenient for copying and pasting commands on the command line.
DWITH_xxx_STORAGE_ENGINE: Specify the storage engine that is statically compiled to mysql. MyISAM, MERGE, MEMBER and CSV are compiled to the server by default, and no special specification is required.
DWITH_MYISAM_STORAGE_ENGINE=1: Add MYISAM storage engine support
DWITH_INNOBASE_STORAGE_ENGINE=1: Add INNOBASE storage engine support
DWITH_MEMORY_STORAGE_ENGINE=1: Add MEMORY storage engine support
DSYSCONFDIR: Initialize parameter file directory
DMYSQL_DATADIR: Data file directory
DMYSQL_TCP_PORT: Service port Number, default 3306
DMYSQL_UNIX_ADDR: socket file Path, default /tmp/mysql.sock

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.37 \
-DMYSQL_DATADIR=/usr/local/mysql-5.7.37/data \
-DMYSQL_UNIX_ADDR=/usr/local/mysql-5.7.37/tmp/mysql.sock \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/usr/local/boost_1_59_0 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_ZLIB=bundled \
-DWITH_SSL=system \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_DEBUG=0
make && make install 

Configure Mysql

1. Initialize mysql

/usr/local/mysql/bin/mysqld --initialize --user=mysql
--basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

initial passwordA temporary password is generated for root@localhost: T>qxtIjrX8ap

2. Initialize 3307, 3308

/usr/local/mysql/bin/mysqld --no-defaults --initialize-insecure --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data3307 --user=mysql --explicit_defaults_for_timestamp
/usr/local/mysql/bin/mysqld --no-defaults --initialize-insecure --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data3308 --user=mysql --explicit_defaults_for_timestamp

3. Add the mysql command to the environment variable

Add the environment variable command at the end of /etc/profile, and finally execute source /etc/profile to make the configuration take effect

export PATH=$PATH:/usr/local/mysql/bin

4. my.cnf configuration file

[mysqld_multi]
mysqld=/usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
user=root
log=/usr/local/mysql/mysql_multi.log

[mysqld3307]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data3307
port=3307
user=mysql
socket=/tmp/mysql.sock3307
server_id=3307
log_bin=mysql-bin


[mysqld3308]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data3308
port=3308
user=mysql
socket=/tmp/mysql.sock3308
server_id=3308

execute and view

killall mysqld  

[root@ff355ce173e3 mysql]# mysqld_multi start
[root@ff355ce173e3 mysql]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld3307 is running
MySQL server from group: mysqld3308 is running

Login 3307

If there is no password, just press Enter.

mysql -uroot -p -P3307 -h127.0.0.1
//查看端口
mysql> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3307  |
+---------------+-------+
1 row in set (0.01 sec)


//查看binlog日志功能是否开启
mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin       | ON    |
+---------------+-------+
1 row in set (0.00 sec)

//查看server_id
mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 3307  |
+---------------+-------+
1 row in set (0.00 sec)

2. Create a duplicate user and grant permissions. First, execute flush privileges, otherwise an error will be reported.

flush privileges;
create user 'copy'@'%' identified by 'copy';

//配置权限
grant replication slave on *.* to "copy"@"%";

3. View the binlog location of the main library

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      747 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

Log in to 3308 and configure the slave library

CHANGE MASTER TO
MASTER_HOST='127.0.0.1',
MASTER_USER='copy',
MASTER_PORT=3307,
MASTER_PASSWORD='copy',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=747;
//查看从数据库信息
show slave status\G;

//start slave;

Guess you like

Origin blog.csdn.net/xuezhiwu001/article/details/129886259