mysql 5.5多实例部署

本文介绍MySQL5.5数据库多实例部署方法,包括原理、特点、应用场景及具体部署步骤,帮助读者掌握多实例配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mysql5.5数据库多实例部署,我们可以分以下几个步骤来完成。

1、 mysql多实例的原理

2、 mysql多实例的特点

3、 mysql多实例应用场景

4、 mysql5.5多实例部署方法

一、mysql多实例的原理

mysql多实例,简单的说,就是在一台服务器上开启多个不同的mysql服务端口(如3306,3310),运行多个mysql服务进程。这些服务进程通过不同的socket监听不同的服务端口,来提供各自的服务。

这些mysql实例共用一套mysql安装程序,使用不同的my.cnf配置文件、启动程序、数据文件。在提供服务时,mysql多实例在逻辑上看来是各自独立的,各个实例之间根据配置文件的设定值,来取得服务器的相关硬件资源。

二、mysql多实例的特点

2.1 有效的利用服务器资源

当单个服务器资源有剩余时,可以充分利用剩余的服务器资源来提供更多的服务。

2.2 节约服务器资源

当公司资金紧张,但是数据库需要各自提供独立服务,而且需要主从同步等技术时,使用多实例就最好了。

2.3 出现资源互相抢占问题

当某个实例服务并发很高或者有慢查询时,会消耗服务器更多的内存、CPU、磁盘IO等资源,这时就会导致服务器上的其它实例提供访问的质量下降,出现服务器资源互相抢占的现象。

三、mysql多实例应用场景

3.1 资金紧张型公司的选择

当公司业务访问量不太大,又舍不得花钱,但同时又希望不同业务的数据库服务各自独立,而且需要主从同步进行等技术提供备份或读写分离服务时,使用多实例是最好不过的。

3.2 并发访问不是特别大的业务

当公司业务访问量不太大,服务器资源基本闲置的比较多,这是就很适合多实例的应用。如果对SQL语句优化的好,多实例是一个很值得使用的技术。即使并发很大,只要合理分配好系统资源,也不会有太大问题。

四、mysql5.5多实例部署方法

4.1 mysql5.5多实例部署方法

mysql5.5多实例部署方法一个是通过多个配置文件启动多个不同进程的方法,第二个是使用官方自带的mysqld_multi来实现。

第一种方法我们可以把各个实例的配置文件分开,管理比较方便。第二种方法就是把多个实例都放到一个配置文件中,这个管理不是很方便。所以在此我们选择第一种方法,而且以下实验我们全部是在此方法下进行的。

4.2 mysql5.5的安装及配置

要配置mysql5.5多实例,我们首先要安装mysql5.5,有关mysql5.5的安装可以查看博客的额上一篇文章。

mysql5.5安装完毕后,我们不要启动mysql,因为此时mysql是单实例的。

4.3 创建mysql多实例的数据目录

现在我们来创建mysql5.5多实例的数据目录,在此我们创建两个mysql实例

建两个数据目录data,data2


4.4 修改mysql5.5多实例my.cnf文件

实例3306和3310的数据目录创建完毕后,我们来配置实例3306与3310的my.cnf配置文件。

复制mysql5.5安装目录support-files下的my-medium.cnf为my.cnf,并把内容修改为下。现在以3306这个实例为例,如下:


# Example MySQL config file for medium systems.
#
# This is for a system with little memory (32M - 64M) where MySQL plays
# an important part, or systems up to 128M where MySQL is used together with
# other programs (such as a web server)
#
# MySQL programs look for option files in a set of
# locations which depend on the deployment platform.
# You can copy this option file to one of those
# locations. For information about these locations, see:
# http://dev.mysql.com/doc/mysql/en/option-files.html
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.

# The following options will be passed to all MySQL clients
[client]
#password       = your_password
port            = 3306
socket          = /usr/local/mysql/data/mysql.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port            = 3306
socket          = /usr/local/mysql/data/mysql.sock
user=mysql
port=3306
server_id=1
pid-file=/usr/local/mysql/data/mysql.pid
log-error=/var/log/mysqld.log
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
#LOAD DATA
secure_file_priv=/home/zhangjunzhao/data

# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
# 
#skip-networking

# Replication Master Server (default)
# binary logging is required for replication
log-bin=mysql-bin

# binary logging format - mixed recommended
binlog_format=mixed

# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id       = 1

# Replication Slave (comment out master section to use this)
#
# To configure this host as a replication slave, you can choose between
# two methods :
#
# 1) Use the CHANGE MASTER TO command (fully described in our manual) -
#    the syntax is:
#
#    CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,
#    MASTER_USER=<user>, MASTER_PASSWORD=<password> ;
#
#    where you replace <host>, <user>, <password> by quoted strings and
#    <port> by the master's port number (3306 by default).
#
#    Example:
#
#    CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
#    MASTER_USER='joe', MASTER_PASSWORD='secret';
#
# OR
#
# 2) Set the variables below. However, in case you choose this method, then
#    start replication for the first time (even unsuccessfully, for example
#    if you mistyped the password in master-password and the slave fails to
#    connect), the slave will create a master.info file, and any later
#    change in this file to the variables' values below will be ignored and
#    overridden by the content of the master.info file, unless you shutdown
#    the slave server, delete master.info and restart the slaver server.
#    For that reason, you may want to leave the lines below untouched
#    (commented) and instead use CHANGE MASTER TO (see above)
#
# required unique id between 2 and 2^32 - 1
# (and different from the master)
# defaults to 2 if master-host is set
# but will not function as a slave if omitted
#server-id       = 2
#
# The replication master for this slave - required
#master-host     =   <hostname>
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user     =   <username>
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password =   <password>
#
# The port the master is listening on.
# optional - defaults to 3306
#master-port     =  <port>
#
# binary logging - not required for slaves, but recommended
#log-bin=mysql-bin

# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = /usr/local/mysql/data
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = /usr/local/mysql/data
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
#innodb_buffer_pool_size = 16M
#innodb_additional_mem_pool_size = 2M
# Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size = 5M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout


以上是实例3306的my.cnf配置文件,现在我们来配置实例3310的my.cnf。实例3310的配置文件my.cnf我们直接复制实例3306的my.cnf文件,然后设置datadir目录。如下:

cp /data/3306/my.cnf /data/3310/my.cnf

4.5 初始化mysql多实例

实例3306和3310的my.cnf配置文件修改完毕后,我们需要来初始化这两个实例,使用mysql_install_db命令。如下:

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

/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data2 --user=mysql


4.6 修改mysql实例的数据库目录权限

mysql实例初始化完毕后,我们现在把实例3306和实例3307的数据目录权限重新赋予给mysql用户。如下:

chown -R mysql:mysql /data

chown -R mysql:mysql /data2



4.7 启动mysql5.5多实例

我们现在来启动实例。使用如下命令:


bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql &

bin/mysqld_safe --defaults-file=/etc/my2.cnf --user=mysql &


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值