mysql 5.6 多实例_MySQL 5.6 多实例安装

摘要

在一台服务器上,启动多个端口,每个端口运行一个mysql。多端口多进程,每个端口就是一个实例,各个进程之间没有关系,但他们又同用相同的硬件资源。

1.0 多实例概述

这些多实例,使用相同的应用程序,运行不同的配置,使用各自的数据文件。

就像qq一样,一台电脑上可以登录多个qq,但是只安装了一套qq程序, 每个qq都有自己的数据文件 。

这种方式可以大限度的利用硬件资源,同时因为在一台服务器上,运行多个实例,如果服务器故障,这些实例都会受影响。另外,某个实例高并发或者慢查询时,整个实例会消耗大量系统的CPU与磁盘I/O等资源,导致服务器上 的其他实例的服务质量下降。

2.0 二级制包安装

与源码安装相比,二进制安装包太大,但是安装速度比源码包快很多。

2.1 二进制包下载

127.html

127.html

wget https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.45-linux-glibc2.12-x86_64.tar.gz

2.2 安装

tar xf mysql-5.6.45-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

ln -s /usr/local/mysql-5.6.45-linux-glibc2.12-x86_64 /usr/local/mysql

# 添加环境变量

echo 'export PATH=/usr/local/mysql/bin:$PATH' >>/etc/profile.d/path.sh

. /etc/profile

useradd -s /sbin/nologin mysql

mkdir /data/my3306/data -p

chown -R mysql.mysql /data/my3306

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

2.3 添加mysql配置

[root@master01 tools]# cat my3306.cnf

[client]

port = 3306 #mysql3306实例的端口

socket = /data/my3306/mysql.sock #socket文件

[mysql]

no-auto-rehash

[mysqld]

user = mysql

port = 3306

skip-grant-tables # 跳过登录认证,初始化时需要

socket = /data/my3306/mysql.sock

basedir = /use/local/mysql/

datadir = /data/my3306/data/

open_files_limit = 1024000

back_log = 600

max_connections = 800

max_connect_errors = 3000

table_cache = 614

external-locking = FALSE

max_allowed_packet =8M

sort_buffer_size = 1M

join_buffer_size = 1M

thread_cache_size = 100

thread_concurrency = 2

query_cache_size = 2M

query_cache_limit = 1M

query_cache_min_res_unit = 2k

default_table_type = InnoDB

thread_stack = 192K

transaction_isolation = READ-COMMITTED

tmp_table_size = 2M

max_heap_table_size = 2M

long_query_time = 1

log_long_format

log-error = /data/my3306/error.log

log-slow-queries = /data/my3306/slow.log

pid-file = /data/my3306/mysql.pid

log-bin = /data/my3306/mysql-bin

relay-log = /data/my3306/relay-bin

relay-log-info-file = /data/my3306/relay-log.info

binlog_cache_size = 1M

max_binlog_cache_size = 1M

max_binlog_size = 2M

expire_logs_days = 7

key_buffer_size = 16M

read_buffer_size = 1M

read_rnd_buffer_size = 1M

bulk_insert_buffer_size = 1M

myisam_sort_buffer_size = 1M

myisam_max_sort_file_size = 10G

myisam_max_extra_sort_file_size = 10G

myisam_repair_threads = 1

myisam_recover

lower_case_table_names = 1

skip-name-resolve

slave-skip-errors = 1032,1062

replicate-ignore-db=mysql

server-id = 3306

innodb_additional_mem_pool_size = 4M

innodb_buffer_pool_size = 32M

innodb_data_file_path = ibdata1:12M:autoextend

innodb_file_io_threads = 4

innodb_thread_concurrency = 8

innodb_flush_log_at_trx_commit = 2

innodb_log_buffer_size = 2M

innodb_log_file_size = 4M

innodb_log_files_in_group = 3

innodb_max_dirty_pages_pct = 90

innodb_lock_wait_timeout = 120

innodb_file_per_table = 0

[mysqldump]

quick

max_allowed_packet = 2M

[mysqld_safe]

log-error=/data/my3306/mysql_3306.err

pid-file=/data/my3306/mysqld.pid

2.4 初始化 实例

127.html

解决方法 :安装autoconf库

命令:yum -y install autoconf   # 此包安装时会安装Data:Dumper模块

然后重新初始化。

/usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my3306.cnf --basedir=/usr/local/mysql --datadir=/data/my3306/data --user=mysql

127.html

# 先启动测试下:

/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my3306.cnf

127.html

可以正常启动,登录修改密码:

mysql -S /data/my3306/mysql.sock -u root

mysql> flush privileges;  # 跳过登陆认证的,要想刷新授权表。

Query OK, 0 rows affected (0.00 sec)

mysql> set password for root@localhost = password('123456');

Query OK, 0 rows affected (0.00 sec)

mysql>

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

# 停止实例,可直接kill掉

# 修改配置文件,删除“跳过登录认证”

2.5 添加启动文件

port="$1" # 传两个参数,端口号 和 动作

acction="$2"

user="root" # 用于关闭实例时的用户,关闭使用的mysqladmin shutdown

passwd="123456" # 和密码

my_conf="/etc/my${port}.cnf" # 启动时指定的配置文件位置

my_sock="/data/my$port/mysql.sock" # socket位置

err_log="/data/my$prot/mysql_3306.err" # 错误日志的位置

. /etc/init.d/functions

function _usage() {

if [ "$#" -ne 2 ];then

echo "USAGE: $0 {prot} {stop|start|restart}"

exit

fi

}

function _MySQL_start() {

if lsof -i :$port &>/dev/null;then

echo "error: mysql $port is running."

exit

else

"/usr/local/mysql/bin/mysqld_safe" --defaults-file=$my_conf 2>>$err_log 1>/dev/null &

sleep 3

if lsof -i :$port &>/dev/null;then action "start mysql $prot" /bin/true else action "start mysql $port" /bin/false fi fi

}

function _MySQL_stop() {

if lsof -i :$port &>/dev/null;then

"/usr/local/mysql/bin/mysqladmin" -u"$user" -p"$passwd" -S $my_sock shutdown 2>>$err_log 1>/dev/null &

sleep 3

if lsof -i :$port &>/dev/null;then action "stop mysql $prot" /bin/false else action "stop mysql $port" /bin/true fi else echo "error: mysql $port is not running." exit fi

}

case $2 in

start)

_MySQL_start

;;

stop)

_MySQL_stop

;;

restart)

_MySQL_stop

_MySQL_start

;;

*)

_usage

;;

esac

2.5.1 启动一个实例,测试脚本

127.html启动 & 关闭

这里的启动可以写出systemctl 管理,为了方便没有写,开机启动可以写到/etc/rc.local中。

echo -e "# 启动 3306 实例/n/data/mysql 3306 start" >>/etc/rc.local

chmod +x /etc/rc.d/rc.local # 给源文件加执行权限,rc.local 里面有提示

# 为了方便每次登陆,这里可以给mysql 添加一个别名

echo "alias my3306='mysql -uroot -p123456 -S /data/my3306/mysql.sock'" >>/etc/profile.d/alias.sh

. /etc/profile

127.html登录别名

3.0 安装第二个实例 3307

安装多个实例,只是重复3306的初始化过程,并创建相应的环境即可。

# 先创建环境

cp /etc/my3306.cnf /etc/my3307.cnf

sed -i 's/3306/3307/g' /etc/my3307.cnf

mkdir -p /data/my3307/data

touch /data/my3307/mysql_error_3307.err

chown -R mysql.mysql /data/my3307

# 进行初始化

/usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my3307.cnf --basedir=/usr/local/mysql --datadir=/data/my3307/data --user=mysql

# 启动实例,记得首次登陆添加 skip-grant-tables

/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my3307.cnf

127.html已启动

# 修改密码

127.html修改密码

然后删除 skip-grant-tables,kill 3307 进程,重新启动即可

127.html多实例

这样就完成了多实例,如果还有其他实例,一样操作。

https://www.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值