MySQL多实例配置

一MySQL 多实例

在一台服务器上开启多个不同的服务端口,运行多个MySQL服务进程,这些服务端口通过不同的socket监听不同的服务端口来提供各自的服务

共用一套MySQL安装程序,使用不同的配置文件,启动程序和数据文件;但是他们公用系统资源,比如CPU,内存,磁盘等,所以逻辑上独立,但是彼此还是有一定的影响,比如一个实例查询量很大,其他进程多少会受到一些影响。

 

作用:

有效利用服务器资源

节约服务器资源

 

应用场景:

资金紧张;并发访问并不是很大的业务

 

一般内存24G-32G,8核CPU,磁盘 6*600Gsas 15k,可以跑2-3实例

 

 

 

二 常见配置方案:

1多个配置文件 多个启动程序

2musql-multi

 

三 安装MySQL多个实例

我们使用方案一安装

第一步:检查依赖库:

ncurses-devellibaio-devel

 

第二步:结束所有mysqld进程

pkillmysqld

rm-f /etc/init.d/mysqld

 

第三步 创建2目录

mkdir-p /data/{3306,3307}/data

 

第四步 配置/data/3306/my.cnf

[client]

#password     = your_password

port          = 3306

socket       = /data/3306/mysql.sock

 

[mysqld]

user= mysql

port          = 3306

socket       = /data/3306/mysql.sock

basedir=/usr/local/mysql

datadir=/data/3306/data

open_files_limit=1024

back_log=600

skip-external-locking

#key_buffer_size = 16M

key_buffer_size= 16K

max_allowed_packet= 1M

table_open_cache= 64

sort_buffer_size= 512K

read_buffer_size= 1M

read_rnd_buffer_size= 1M

net_buffer_length= 8K

thread_stack= 128K

log-error=/data/3306/error.log

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

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

log-bin=/data/3306/mysql-bin

relay-log=/data/3306/relay-bin

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

binlog_cache_size=1M

max_binlog_cache_size=1M

max_binlog_size=2M

expire_logs_days=7

bulk_insert_buffer_size=1M

myisam_sort_buffer_size=1M

myisam_max_sort_file_size=10M

myisam_repair_threads=1

myisam_recover

lower_case_table_names=1

skip-name-resolve

slave-skip-errors=1032,1062

replicate-ignore-db=mysql

 

#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

#(using the "enable-named-pipe" option) will render mysqld useless!

#

#skip-networking

server-id   = 1

 

#Uncomment the following if you want to log updates

#log-bin=mysql-bin

 

#binary logging format - mixed recommended

#binlog_format=mixed

 

#Causes updates to non-transactional engines using statement format to be

#written directly to binary log. Before using this option make sure that

#there are no dependencies between transactional and non-transactional

#tables such as in the statement INSERT INTO t_myisam SELECT * FROM

#t_innodb; otherwise, slaves may diverge from the master.

#binlog_direct_non_transactional_updates=TRUE

 

#Uncomment the following if you are using InnoDB tables

#innodb_data_home_dir= /usr/local/mysql/data

#8innodb_data_file_path = ibdata1:128M:autoextend

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 = 32M

innodb_buffer_pool_size= 16M

innodb_file_io_threads=4

innodb_thread_concurrency=8

innodb_additional_mem_pool_size= 4M

#Set .._log_file_size to 25 % of buffer pool size

#innodb_log_file_size= 5M

innodb_log_file_size= 4M

#innodb_log_buffer_size= 8M

innodb_log_buffer_size= 2M

#innodb_flush_log_at_trx_commit= 1

innodb_flush_log_at_trx_commit=2

#innodb_lock_wait_timeout= 50

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= 16M

max_allowed_packet= 2M

 

[mysql]

no-auto-rehash

#Remove the next comment character if you are not familiar with SQL

#safe-updates

 

[mysqld_safe]

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

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

 

[myisamchk]

key_buffer_size= 8M

sort_buffer_size= 8M

 

[mysqlhotcopy]

interactive-timeout

 

 

同理配置/data/3307/my.cnf,我们只需要改一下端口

#Example MySQL config file for small systems.

#

#This is for a system with little memory (<= 64M) where MySQL is only used

#from time to time and it's important that the mysqld daemon

#doesn't use much resources.

#

#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          = 3307

socket       = /data/3307/mysql.sock

 

#Here follows entries for some specific program

 

#The MySQL server

[mysqld]

user= mysql

port          = 3307

socket       = /data/3307/mysql.sock

basedir=/usr/local/mysql

datadir=/data/3307/data

open_files_limit=1024

back_log=600

skip-external-locking

#key_buffer_size = 16M

key_buffer_size= 16K

max_allowed_packet= 1M

table_open_cache= 64

sort_buffer_size= 512K

read_buffer_size= 1M

read_rnd_buffer_size= 1M

net_buffer_length= 8K

thread_stack= 128K

log-error=/data/3307/error.log

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

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

#log-bin=/data/3307/mysql-bin

relay-log=/data/3307/relay-bin

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

binlog_cache_size=1M

max_binlog_cache_size=1M

max_binlog_size=2M

expire_logs_days=7

bulk_insert_buffer_size=1M

myisam_sort_buffer_size=1M

myisam_max_sort_file_size=10M

myisam_repair_threads=1

myisam_recover

lower_case_table_names=1

skip-name-resolve

slave-skip-errors=1032,1062

replicate-ignore-db=mysql

 

#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

#(using the "enable-named-pipe" option) will render mysqld useless!

#

#skip-networking

server-id   = 3

 

#Uncomment the following if you want to log updates

#log-bin=mysql-bin

 

#binary logging format - mixed recommended

#binlog_format=mixed

 

#Causes updates to non-transactional engines using statement format to be

#written directly to binary log. Before using this option make sure that

#there are no dependencies between transactional and non-transactional

#tables such as in the statement INSERT INTO t_myisam SELECT * FROM

#t_innodb; otherwise, slaves may diverge from the master.

#binlog_direct_non_transactional_updates=TRUE

 

#Uncomment the following if you are using InnoDB tables

#innodb_data_home_dir= /usr/local/mysql/data

#8innodb_data_file_path = ibdata1:128M:autoextend

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 = 32M

innodb_buffer_pool_size= 16M

innodb_file_io_threads=4

innodb_thread_concurrency=8

innodb_additional_mem_pool_size= 4M

#Set .._log_file_size to 25 % of buffer pool size

#innodb_log_file_size= 5M

innodb_log_file_size= 4M

#innodb_log_buffer_size= 8M

innodb_log_buffer_size= 2M

#innodb_flush_log_at_trx_commit= 1

innodb_flush_log_at_trx_commit=2

#innodb_lock_wait_timeout= 50

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= 16M

max_allowed_packet= 2M

 

[mysql]

no-auto-rehash

#Remove the next comment character if you are not familiar with SQL

#safe-updates

 

[mysqld_safe]

log-error=/data/3307/mysql_3307.err

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

 

[myisamchk]

key_buffer_size= 8M

sort_buffer_size= 8M

 

[mysqlhotcopy]

interactive-timeout

 

第五步:初始化

为什么初始化?
初始化就是创建基础的数据库文件,列入生成mysql表等

./mysql_install_db--basedir=/usr/local/mysql/ --datadir=/data/3306/data/ --user=mysql

./mysql_install_db--basedir=/usr/local/mysql/ --datadir=/data/3307/data/ --user=mysql

注意:必须指定--user 参数,否则如果你用用root用户下,那么生成文件全是root权限的,mysql用户登录会有问题

 

第六步:多实例的启动

如果my.cnf不在/etc下面,我们在启动mysql需要指定my.cnf的位置比如:

mysqld_safe --defaults-file=/data/3306/my.cnf 2>&1 > /dev/null &

mysqld_safe --defaults-file=/data/3307/my.cnf 2>&1 > /dev/null &

 

第七步:多实例的关闭

mysqldadmin–u${username} –p${password} -S ${sock-file} shutdown

mysqldadmin-uroot -p123456 -S /data/3306/mysql.sock shutdown

mysqldadmin-uroot -p123456 -S /data/3307/mysql.sock shutdown

 

第八步:启动数据库

编写启动脚本

#!/bin/bash

 

#init

MYSQL_HOME=/usr/local/mysql

PORT=3306

USERNAME="root"

PASSWORD="123456"

MYSQL_SOCK=/data/$PORT/mysql.sock

MYSQL_CONFIG=/data/$PORT/my.cnf

#startfucntion

function_start_mysql(){

      if [ ! -e "$MYSQL_SOCK" ]; then

           printf "StartingMySQL...\n"

           /bin/sh $MYSQL_HOME/bin/mysqld_safe--defaults-file=$MYSQL_CONFIG 2>&1 > /dev/null &

      else

           printf "MySQL isrunning...\n"

           exit

      fi

}

 

#stop

function_stop_mysql(){

      if [ ! -e "$MYSQL_SOCK" ]; then

           printf "MySQL is stopped...\n"

           exit

      else

           printf "StoppingMySQL...\n"

           /bin/sh $MYSQL_HOME/bin/mysqldadmin-u$USERNAME -p$PASSWORD -S $MYSQL_SOCK shutdown

      fi

}

 

#restart

function_restart_mysql(){

      printf "Restarting MySQL.....\n"

      function_stop_mysql

      sleep 2

      function_start_mysql

}

 

case$1 in

start)

      function_start_mysql

;;

stop)

      function_stop_mysql

;;

restart)

      function_restart_mysql

;;

*)

      printf "Usage:/data/$PORT/mysql{start|stop|restart}\n"

esac

 

/opt/shell/mysql/3306/mysql.shstart

/opt/shell/mysql/3307/mysql.shstart

 

第九步:登录mysql

这时候我们可以以root用户登录,也可以用匿名用户登录,但是是有问题的,为什么呢,我们没有为root用户设置密码,默认是需要为root用户设置密码。

mysql-uroot -p123456 -S /data/3306/mysql.sock

ERROR2002 (HY000): Can't connect to local MySQL server through socket'/usr/local/mysql/tmp/mysql.sock' (2)

方案:

1以安全模式进入到MySQL

/usr/local/mysql/bin/mysqld_safe--defaults-file=/data/3306/my.cnf --skip-grant-tables  &

2重新开一个窗口,然后mysql-uroot –S /data/3306/mysql

usemysql;

UPDATEuser SET password=password('123456' )WHERE user='root';

3刷新权限表

flush privileges;

4退出mysql,关闭安全模式

5然后正常启动mysqld,/opt/shell/mysql/3306/mysql.shstart

6然后在登录mysql

mysql-uroot -p 123456 -S /data/3306/mysql

7发现还是有问题:

ERROR1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

8那应该是我们没有给root用户赋予权限

9然后再停掉mysql,再以安全模式,进入mysql

10设置权限

usemysql;

GRANTALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

这时候会有一个错误,说在skip-grant-tables模式下,不能执行此操作。

没关系,我们刷新一下权限表

flush privileges;

再次执行:GRANTALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

再刷新一系权限表。

11退出安全模式,重新正常启动mysql,然后登陆

mysql-uroot -p 123456 -S /data/3306/mysql

然后就可以了。

然后3307也是做同样的操作。

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

莫言静好、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值