02.mysql多实例安装实战

Posted 流氓徐志摩

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了02.mysql多实例安装实战相关的知识,希望对你有一定的参考价值。

2.1 什么是mysql多实例?

    简单的说,mysql 多实例就是在一台服务器上同时开启多个不同的服务端口(如 3306、 3307),同时运行多个 mysql 服务进程,这些服务进程通过不同的 socket 监听不同的服务端 口来提供服务 这些 mysql 多实例公用一套 mysql 安装程序,使用不同的 my.cnf(也可以相同)配置 文件、启动程序(也可以相同)和数据文件,在提供服务时,多实例 mysql 在逻辑上看来是各自独立的,他们根据配置文件的对应设定值,获得服务器相应数量的硬件资源。

2.2 mysql多实例结构图

 

 

2.3 安装mysql多实例

2.3.1 实战安装 mysql 多实例需要的依赖包

[root@mysql ~]# yum install ncurses-devel libaio-devel –y
[root@mysql ~]# rpm -qa ncurses-devel libaio-devel libaio-devel-0.3.107-10.el6.x86_64 ncurses-devel-5.7-4.20090207.el6.x86_64​

2.3.2  安装编译 mysql 需要的软件

[root@mysql tools]# pwd /home/tools
[root@mysql tools]# ls cmake-2.8.8.tar.zip
[root@mysql tools]# unzip cmake-2.8.8.tar.zip Archive: cmake-2.8.8.tar.zip inflating: cmake-2.8.8.tar.gz
[root@mysql tools]# ls cmake-2.8.8.tar.gz cmake-2.8.8.tar.zip
[root@mysql tools]# tar xfv cmake-2.8.8.tar.gz
[root@mysql tools]# ls cmake-2.8.8 cmake-2.8.8.tar.gz cmake-2.8.8.tar.zip
[root@mysql tools]# cd cmake-2.8.8
[root@mysql cmake-2.8.8]# ./configure 
[root@mysql cmake-2.8.8]# gmake 
[root@mysql cmake-2.8.8]# gmake install

 

关于 cmake 软件也可以直接 yum 安装

yum install -y cmake

 

2.3.3 安装 mysql 软件

1、创建安装 mysql 软件的用户 mysql
[root@mysql ~]# useradd mysql -s /sbin/nologin –M
2、解压 mysql-5.5.27 的软件包
[root@mysql tools]# ls
cmake-2.8.8 cmake-2.8.8.tar.gz cmake-2.8.8.tar.zip mysql-5.5.27 mysql-5.5.27.tar.gz
3、编译安装 mysql(下面是编译参数)
/usr/local/bin/cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.27 \\
    -DMYSQL_DATADIR=/application/mysql-5.5.27/data \\
    -DMYSQL_UNIX_ADDR=/application/mysql-5.5.27/tmp/mysql.sock \\
    -DDEFAULT_CHARSET=utf8 \\
    -DDEFAULT_COLLATION=utf8_general_ci \\
    -DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \\
    -DENABLED_LOCAL_INFILE=ON \\
    -DWITH_INNOBASE_STORAGE_ENGINE=1 \\
    -DWITH_FEDERATED_STORAGE_ENGINE=1 \\
    -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \\
    -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \\
    -DWITHOUT_PARTITION_STORAGE_ENGINE=1 \\
    -DWITH_FAST_MUTEXES=1 \\
    -DWITH_ZLIB=bundled \\
    -DENABLED_LOCAL_INFILE=1 \\
    -DWITH_READLINE=1 \\
    -DWITH_EMBEDDED_SERVER=1 \\
    -DWITH_DEBUG=0
[root@mysql mysql-5.5.27]# make && make install
[root@mysql mysql-5.5.27]# ln -s /application/mysql-5.5.27 /application/mysql
[root@mysql ~]# echo "PATH=/application/mysql/bin:$PATH" >>/etc/profile 把 mysql 的命令放
入至全局环境变量之中
[root@mysql ~]# . /etc/profile
[root@mysql mysql-5.5.27]# cd /application/mysql
[root@mysql mysql]# ls
bin data include lib mysql-test scripts sql-bench
COPYING docs INSTALL-BINARY man README share support-files

2.3.4  创建存放两个 mysql 实例的数据目录

[root@mysql ~]# mkdir /mysqldata/{3306,3307}/data -p
[root@mysql ~]# tree /mysqldata
/mysqldata
├── 3306
    │ └── data
└── 3307
    └── data 4 directories, 0 files​

2.3.5 创建两个 mysql 多实例的配置文件

2.3.5.1  3306 的实例

#实例1:3306配置文件
[root@mysql 3306]# ls
data my.cnf(配置文件) mysql(程序启动文件) mysql-bin.000001 mysql-bin.index
mysqld.pid mysql_oldboy3306.err mysql.sock
[root@mysql 3306]# cat my.cnf
[client]
port = 3306
socket = /mysqldata/3306/mysql.sock
[mysql]
no-auto-rehash
[mysqld]
user = mysql
port = 3306
socket = /mysqldata/3306/mysql.sock
basedir = /application/mysql
datadir = /mysqldata/3306/data
open_files_limit = 1024
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/3306/error.log
#log-slow-queries = /data/3306/slow.log
pid-file = /mysqldata/3306/mysql.pid
log-bin = /mysqldata/3306/mysql-bin
relay-log = /mysqldata/3306/relay-bin
relay-log-info-file = /mysqldata/3306/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 = 1
innodb_additional_mem_pool_size = 4M
innodb_buffer_pool_size = 32M
innodb_data_file_path = ibdata1:128M: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=/mysqldata/3306/mysql_oldboy3306.err
pid-file=/mysqldata/3306/mysqld.pid
 
实例 1、3306 的启动文件,并授权执行的权限
[root@mysql 3306]# chmod +x /mysqldata/3306/mysql
[root@mysql 3306]# cat mysql
#!/bin/sh
################################################
#this scripts is created by oldboy at 2007-06-09
################################################
#init
port=3306
mysql_user="root"
mysql_pwd="oldboy"
CmdPath="/application/mysql/bin"
mysql_sock="/mysqldata/${port}/mysql.sock"
#startup function
function_start_mysql()
{
 if [ ! -e "$mysql_sock" ];then
 printf "Starting MySQL...\\n"
 /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/mysqldata/${port}/my.cnf 2>&1 >
/dev/null &
 else
 printf "MySQL is running...\\n"
 exit
 fi
}
#stop function
function_stop_mysql()
{
 if [ ! -e "$mysql_sock" ];then
 printf "MySQL is stopped...\\n"
 exit
 else
 printf "Stoping MySQL...\\n"
 ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S
/mysqldata/${port}/mysql.sock shutdown
 fi
}
#restart function
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: /mysqldata/${port}/mysql {start|stop|restart}\\n"
esac
 
更改 3306 实例目录的属主和属组权限,初始化数据库
[root@mysql 3306]# chown -R mysql.mysql /mysqldata/3306
[root@mysql mysql]# ./scripts/mysql_install_db --datadir=/mysqldata/3306/data --
basedir=/application/mysql --user=mysql
Installing MySQL system tables...
OK
Filling help tables...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/application/mysql/bin/mysqladmin -u root password \'new-password\'
/application/mysql/bin/mysqladmin -u root -h mysql password \'new-password\'
Alternatively you can run:
/application/mysql/bin/mysql_secure_installation
which will also give you the option of removing the test
 
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /application/mysql ; /application/mysql/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /application/mysql/mysql-test ; perl mysql-test-run.pl
Please report any problems with the /application/mysql/scripts/mysqlbug script!
 
启动 3306 实例
[root@mysql 3306]# /mysqldata/3306/mysql start
[root@mysql 3306]# lsof -i :3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 31375 mysql 12u IPv4 98252 0t0 TCP *:mysql (LISTEN)

 

2.3.5.2  3307 的实例

3307 实例配置文件
[root@mysql 3307]# ls
data my.cnf(配置文件) mysql(程序启动文件) mysqld.pid mysql_oldboy3307.err
mysql.sock
[root@mysql 3307]# cat my.cnf
[client]
port = 3307
socket = /mysqldata/3307/mysql.sock
[mysql]
no-auto-rehash
[mysqld]
user = mysql
port = 3307
socket = /mysqldata/3307/mysql.sock
basedir = /application/mysql
datadir = /mysqldata/3307/data
open_files_limit = 1024
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/3307/error.log
#log-slow-queries = /data/3307/slow.log
pid-file = /mysqldata/3307/mysql.pid
#log-bin = /data/3307/mysql-bin
relay-log = /mysqldata/3307/relay-bin
relay-log-info-file = /mysqldata/3307/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 = 3
innodb_additional_mem_pool_size = 4M
innodb_buffer_pool_size = 32M
innodb_data_file_path = ibdata1:128M: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=/mysqldata/3307/mysql_oldboy3307.err
pid-file=/mysqldata/3307/mysqld.pid
 
3307 实例的启动配置文件
[root@mysql 3307]# cat /mysqldata/3307/mysql
#!/bin/sh
################################################
#this scripts is created by oldboy at 2007-06-09
#oldboy QQ:31333741
#site:http://www.etiantian.org
#blog:http://oldboy.blog.51cto.com
#oldboy trainning QQ group: 208160987 226199307 44246017
################################################
#init
port=3307
mysql_user="root"
mysql_pwd="oldboy"
CmdPath="/application/mysql/bin"
mysql_sock="/mysqldata/${port}/mysql.sock"
#startup function
function_start_mysql()
{
 if [ ! -e "$mysql_sock" ];then
 printf "Starting MySQL...\\n"
 /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/mysqldata/${port}/my.cnf 2>&1 >
/dev/null &
 else
 printf "MySQL is running...\\n"
 exit
 fi
}
#stop function
function_stop_mysql()
{
 if [ ! -e "$mysql_sock" ];then
 printf "MySQL is stopped...\\n"
 exit
 else
 printf "Stoping MySQL...\\n"
 ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S
/mysqldata/${port}/mysql.sock shutdown
 fi
}
#restart function
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: /mysqldata/${port}/mysql {start|stop|restart}\\n"
esac
 
 
更改 3307 实例的目录权限,初始化数据库
[root@mysql 3307]# chown -R mysql.root /mysqldata/3307
[root@mysql mysql]# ./scripts/mysql_install_db --datadir=/mysqldata/3307/data --
basedir=/application/mysql --user=mysql
Installing MySQL system tables...
OK
Filling help tables...
OK
 
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/application/mysql/bin/mysqladmin -u root password \'new-password\'
/application/mysql/bin/mysqladmin -u root -h mysql password \'new-password\'
Alternatively you can run:
/application/mysql/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /application/mysql ; /application/mysql/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /application/mysql/mysql-test ; perl mysql-test-run.pl
Please report any problems with the /application/mysql/scripts/mysqlbug script!
启动 3307 实例
[root@mysql mysql]# /mysqldata/3307/mysql start
Starting MySQL...
[root@mysql mysql]# lsof -i :3307
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 32195 mysql 11u IPv4 99276 0t0 TCP *:opsession-prxy (LISTEN)

 

2.3.6 多实例通过本地登录

    多实例本地登录一般是通过 socket 文件来指定具体到那个实例的,此文件的具体位置是在 mysql 编译过程或者 my.cnf 文件里指定的,在本地登录数据库时,登录程序会通过socket 文件来判断登录的是哪个数据库实例例如:通过 mysql –uroot –ppcwangjixuan –S /mysqldata/3307/mysql.sock 可知,登录的是 3307 这个实例,mysql.sock 文件是 mysql 服务端与本地 mysql 客户端进行通信的 unix 套接字文件。

2.3.7  远程连接登录 mysql 多实例  

        远程登录 mysql 多实例中的一个实例中,通过 TCP 端口(port)来指定所要登录的mysql 实例,此端口的配置是在 mysql 配置文件 my.cnf 中指定的例如:在 mysql –oldboy –ppcwangjixuan –h 10.0.0.171 –P 3307 中,-P 为端口参数,后面接具体的实例端口,端口是一种逻辑连接位置,是客户端程序被分派到计算机上特殊服务程序的一种方式,强调提前在 10.0.0.171 上对 oldboy 用户做了授权。

以上是关于02.mysql多实例安装实战的主要内容,如果未能解决你的问题,请参考以下文章

从零开始的MYSQL02 --- MySQL启动方式及故障排查,多实例搭建

MySQL单机多实例配置实战

CentOS6 MySql5.5.53编译安装+多实例实战

MYSQL数据库多实例的安装配置mysql_multi启停实战

CentOS6 MySql5.5.53编译安装+多实例实战

mysql 多实例案例实战