欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql使用mysqld_multi部署单机多实例的方法教程

程序员文章站 2022-07-08 11:35:59
前言 大家应该都有所体会,随着硬件层面的发展,linux系统多核已经是普通趋势,而mysql是单进程多线程,所以先天上对多进程的利用不是很高,虽然5.6版本已经在这方面改...

前言

大家应该都有所体会,随着硬件层面的发展,linux系统多核已经是普通趋势,而mysql是单进程多线程,所以先天上对多进程的利用不是很高,虽然5.6版本已经在这方面改进很多,但是也没有达到100%,所以为了充分的利用系统资源,mysql有自己的补充,那就是可以部署多实例,一个实例一个端口。

mysqld_multi设计用于管理在同一台机器上运行的多个mysqld进程,这些进程使用不同的socket文件并监听在不同的端口上。mysqld_multi可以批量启动、关闭、或者报告这些mysqld进程的状态。

下面话不多说了,来一起看看详细的介绍吧。

一、mysql编译安装:

cd /usr/local/src
wget http://mirrors.sohu.com/mysql/mysql-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz 
tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
cd /usr/local/mysql
useradd mysql
mkdir -p /data/mysql
chown -r mysql:mysql /data/mysql
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
cp support-files/my-default.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld

二、准备第一个多实例3307

2.1 创建目录:

mkdir /usr/local/mysql3307
chown -r mysql.mysql /usr/local/mysql3307/
mkdir -p /data/mysql3307
chown -r mysql.mysql /data/mysql3307
mkdir -p /home/data/mysql3307/binlog
chown -r mysql.mysql /home/data/mysql3307

2.2 配置文件

[root@zhdya01 ~]# vim /etc/my.cnf
# for advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** do not edit this file. it's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of mysql.
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin =/usr/local/mysql/bin/mysqladmin
log =/var/log/mysqld_multi.log
[mysqld1]
socket = /usr/local/mysql3307/mysql.sock
port = 3307
pid-file = /usr/local/mysql3307/mysql.pid
datadir = /data/mysql3307
log_bin=/home/data/mysql3307/binlog
server-id = 1
innodb_buffer_pool_size = 128m
innodb_flush_log_at_trx_commit = 0

2.3 初始化数据库

/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql3307

三、准备第二个多实例3308

3.1 创建目录:

mkdir /usr/local/mysql3308
chown -r mysql.mysql /usr/local/mysql3308/
mkdir -p /data/mysql3308
chown -r mysql.mysql /data/mysql3308
mkdir -p /home/data/mysql3308/binlog
chown -r mysql.mysql /home/data/mysql3308

3.2 配置文件

[root@zhdya01 ~]# vim /etc/my.cnf
# for advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** do not edit this file. it's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of mysql.
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin =/usr/local/mysql/bin/mysqladmin
log =/var/log/mysqld_multi.log
[mysqld1]
socket = /usr/local/mysql3307/mysql.sock
port = 3307
pid-file = /usr/local/mysql3307/mysql.pid
datadir = /data/mysql3307
log_bin=/home/data/mysql3307/binlog
server-id = 1
innodb_buffer_pool_size = 128m
innodb_flush_log_at_trx_commit = 0
[mysqld2]
socket = /usr/local/mysql3308/mysql.sock
port = 3308
pid-file = /usr/local/mysql3308/mysql.pid
datadir = /data/mysql3308
log_bin=/home/data/mysql3308/binlog
server-id = 2
innodb_buffer_pool_size = 128m
innodb_flush_log_at_trx_commit = 0

3.3 初始化数据库

/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql3308

四、启动多实例

/etc/init.d/mysqld start
mysqld_multi --defaults-extra-file=/etc/my.cnf start 1,2
[root@zhdya01 bin]# !net
netstat -lntp -lntp
active internet connections (only servers)
proto recv-q send-q local address  foreign address  state pid/program name 
tcp 0 0 0.0.0.0:22  0.0.0.0:*  listen 1241/sshd  
tcp 0 0 127.0.0.1:25  0.0.0.0:*  listen 2087/master  
tcp6 0 0 :::3306   :::*   listen 4406/mysqld  
tcp6 0 0 :::3307   :::*   listen 4197/mysqld  
tcp6 0 0 :::3308   :::*   listen 3359/mysqld  
tcp6 0 0 :::8080   :::*   listen 2222/java  
tcp6 0 0 :::22   :::*   listen 1241/sshd  
tcp6 0 0 ::1:25   :::*   listen 2087/master 

五、查看启动状态

[root@zhdya01 bin]# mysqld_multi --defaults-extra-file=/etc/my.cnf report
reporting mysql servers
mysql server from group: mysqld1 is running
mysql server from group: mysqld2 is running

六、停止多实例

[root@zhdya01 bin]# mysqld_multi --defaults-extra-file=/etc/my.cnf stop 1,2
[root@zhdya01 bin]# mysqld_multi --defaults-extra-file=/etc/my.cnf report
reporting mysql servers
mysql server from group: mysqld1 is not running
mysql server from group: mysqld2 is not running

七、各自登录mysql实例

[root@zhdya01 bin]# mysql --socket=/usr/local/mysql3307/mysql.sock
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 1
server version: 5.6.35-log mysql community server (gpl)
copyright (c) 2000, 2016, oracle and/or its affiliates. all rights reserved.
oracle is a registered trademark of oracle corporation and/or its
affiliates. other names may be trademarks of their respective
owners.
type 'help;' or '\h' for help. type '\c' to clear the current input statement.
mysql> 

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。