您的位置:首页 > 数据库 > MySQL

mysql多实例安装配置指南

2018-02-17 20:52 399 查看
mysql多实例安装配置指南
1.MySQL多实例介绍
1.1什么事MySQL多实例?
简单的说,就是在一台机器上开启多个不同的服务端口(如:3006,3307),运行多个MySQL服务进程,这些服务进程通过
不同的socket监听不同的服务端口来提供各自的服务。
这些MySQL多实例公用一套MySQL安装程序,使用不同的my.cnf配置文件、启动程序,数据文件。在提供服务时,多实例在逻辑上
来起来是各自独立的,多个实例之间根据配置文件的设定值,来取得相关服务器的硬件资源。
1.2MySQL多实例特点
1.2.1有效利用服务器资源
当单个服务器资源有剩余的时,可以充分利用资源更多的服务。
1.2.2节约服务器资源
当公司资金紧张,但是数据库又需要各自尽量提供服务,而且,需要主从同步等技术是,多实例就再好不过了。
1.2.3资源互相抢占问题
当某个服务实例并发很高或者慢查询的时候,整个实例会消耗更多的内存、cpu、磁盘I/O,导致服务器上的其他实例提供的服务的
质量下降。
1.3MySQL多实例的生产场景应用。
1.3.1资金紧张型公司
1.3.2并发不是很大的业务
1.4MySQL多实例的常见配置方法
1.4.1多配置文件部署方案(安装mysql软件)
#安装mysql多实例的过程跟之前的单实例是一样的,可以参照之前的内容进行部署。

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#确保已经安装mysql单实例,并停止服务了。
[root@c601 bak]# ll /usr/local/mysql
total 40
drwxr-xr-x  2 mysql mysql 4096 Feb 11 22:27 bin
drwxr-xr-x  6 mysql mysql 4096 Feb 15 20:46 data
drwxr-xr-x  2 mysql mysql 4096 Feb 11 22:27 docs
drwxr-xr-x  3 mysql mysql 4096 Feb 11 22:27 include
drwxr-xr-x  3 mysql mysql 4096 Feb 11 22:27 lib
drwxr-xr-x  2 mysql mysql 4096 Feb 11 22:28 libexec
drwxr-xr-x 10 mysql mysql 4096 Feb 11 22:28 mysql-test
drwxr-xr-x  5 mysql mysql 4096 Feb 11 22:28 share
drwxr-xr-x  5 mysql mysql 4096 Feb 11 22:28 sql-bench
drwxr-xr-x  2 mysql root  4096 Feb 15 20:27 tmp
[root@c601 bak]# killall mysqld
[root@c601 bak]# killall mysqld
mysqld: no process killed
[root@c601 bak]# /etc/init.d/mysqld start
Starting MySQL.                                            [  OK  ]
[root@c601 bak]# /etc/init.d/mysqld stop
Shutting down MySQL.                                       [  OK  ]

安装多实例MySQL数据库
一、建立mysql多实例的条件
1.建立MySQL安装依赖的包
yum install ncurses-devel -y
yum install libaio-devel -yum
2.选择二进制安装方式为例
首先按照我们前面讲过的四种mysql的安装方式之一安装好数据库,如果是编译安装,则到make install之后截止,根据kiss原则,选择最简单的二进制安装,进行mysql多实例的安装。
cmake 安装5.5 
二、建立mysql账号
1.用root身份登录到Linux中,然后创建mysql组及用户:
groupadd nysql
useradd -s /sbin/nologin -g mysql -M -mysql
tail -1 /etc/passwd #检查用户
三、获得mysql软件
1.建立存放mysql软件包的目录
mkdir -p /home/oldboy/tools
2.上传后的mysql二进制软件包
rz
四、安装mysql软件
1.解压软件包,移动到规范目录下
tar xf mysql.. 
mkdir -p /application/
mv mysql.. /application/mysql
2.生成不带版本号的软连接
ln -s /application/mysql5.5.32 /application/mysql
五、创建多实例的数据文件目录
 pkill mysqld
采用/data目录作为mysql的根目录。
[root@moban application]# mkdir -p /data/{3306,3307}/data
[root@moban application]# tree /data
/data
├── 3306
│   └── data
└── 3307
└── data
六、配置多实例的配置文件。
vi /data/3306/my.cnf
vi /data/3307/my.cnf
[root@moban tools]# cp ./data/3306/my.cnf /data/3306/my.cnf
[root@moban tools]# cp ./data/3307/my.cnf /data/3307/my.cnf
[root@moban tools]# tree /data
/data
├── 3306
│   ├── data
│   └── my.cnf
└── 3307
    ├── data
    └── my.cnf
七、创建多实例的启动文件。
#1.多实例启动文件的启动mysql服务实质:
mysqld_safe --default-file=/data/3306/my.cnf 2>&1 >/dev/null &
mysqld_safe --default-file=/data/3307/my.cnf 2>&1 >/dev/null &
#2.多实例启动文件的停止mysql服务实质:
mysqladmin -u -root -poldboy123 -S /data/3306/mysql.sock shutdown
mysqladmin -u -root -poldboy123 -S /data/3307/mysql.sock shutdown
# # # #
[root@moban tools]# cp ./data/3306/mysql /data/3306
[root@moban tools]# cp ./data/3307/mysql /data/3307 
[root@moban tools]# tree /data
/data
├── 3306
│   ├── data
│   ├── my.cnf
│   └── mysql
└── 3307
    ├── data
    ├── my.cnf
    └── mysql
八、授权mysql用户管理data目录
[root@moban tools]# chown -R mysql.mysql /data
[root@moban tools]# find /data/ -type f -name "mysql"|xargs ls -l
-rw-r--r-- 1 mysql mysql 1307 Feb 22 20:23 /data/3306/mysql
-rw-r--r-- 1 mysql mysql 1307 Feb 22 20:23 /data/3307/mysql
九、授权Mysql多实例服务所有启动文件可执行
[root@moban tools]# find /data/ -type f -name "mysql"|xargs chmod +x
[root@moban tools]# find /data/ -type f -name "mysql"|xargs ls -l
-rwxr-xr-x 1 mysql mysql 1307 Feb 22 20:23 /data/3306/mysql
-rwxr-xr-x 1 mysql mysql 1307 Feb 22 20:23 /data/3307/mysql
十、配置mysql启动命令的全局路径。
说明:mysqld_safe启动脚本默认的从/usr/local/mysql目录中读取另外一个启动脚本mysqld,因为我的安装目录为/application/mysql。所以找不到相关文件。
[root@moban bin]# mkdir /usr/local/mysql/bin -p
ln -s /application/mysql/bin/mysqld /usr/local/mysql/bin/mysqld

[root@moban tools]# tail -1 /etc/profile  
export PATH=/application/mysql/bin:$PATH
source /etc/profile
或者直接将/application/mysql/bin下的命令全部考到 /usr/local/sbin下

十一、mysql初始化。
[root@moban tools]# cd /application/mysql/scripts
[root@moban scripts]# ./mysql_install_db --basedir=/applica
4000
tion/mysql --datadir=/data/3306/data --user=mysql
[root@moban scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql 
#¥#为何要初始化呢?
将数据库的基础数据考入到数据库中
#¥##¥#
十二、启动数据库。
[root@moban bin]# /data/3306/mysql start
Starting MySQL...
[root@moban bin]# /data/3307/mysql start
Starting MySQL...
[root@moban bin]# netstat -lntup|grep 330[6,7]
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      12518/mysqld        
tcp        0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      13237/mysqld 
十三、关闭数据区。
修改一下配置文件。密码改成oldboy123。多实例的登录指定myql.sock
[root@moban bin]# mysql -S /data/3306/mysql.sock 

mysql> create database d3306;

mysql> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| d3306              |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.05 sec

[root@moban bin]# mysql -S /data/3307/mysql.sock 
mysql> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| d3307              |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.05 sec

\\\\\\\\\\\
2.为mysql实例添加密码。
[root@moban bin]# mysqladmin -u root -S /data/3306/mysql.sock password 'oldboy123'
[root@moban bin]# mysqladmin -u root -S /data/3307/mysql.sock password 'oldboy123' 
3.重启mysql。
[root@moban bin]# /data/3306/mysql stop
Stoping MySQL...
[root@moban bin]# netstat -lntup|grep 3306
[root@moban bin]# /data/3306/mysql start
Starting MySQL...
[root@moban bin]# netstat -lntup|grep 3306
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   
#3307一样的操作。
4.修改mysql脚本的权限改成 700
[root@moban bin]# find /data -type f -name "mysql"
/data/3306/mysql
/data/3307/mysql
[root@moban bin]# find /data -type f -name "mysql"|xargs chmod 700
[root@moban bin]# find /data -type f -name "mysql"|xargs chown root.root
[root@moban bin]# find /data -type f -name "mysql"|xargs ls -l
-rwx------ 1 root root 1310 Feb 22 21:25 /data/3306/mysql
-rwx------ 1 root root 1310 Feb 22 21:26 /data/3307/mysql

---------------------------------------------------------------
如何增加第三个实例3308
---------------------------------------------------------------
mkdir -p /data/3308/data 
cp /data/3306/my.cnf /data/3308
cp /data/3306/mysql /data/3308
授权mysql
 chown -R mysql.mysql /data/3308/
 
修改mysql配置文件my.cnf
修改端口3306为3308,server-id
使用vi替换。:g/A/s//B/g
初始化实例
[root@moban ~]# cd /application/mysql/scripts/
[root@moban scripts]# ./mysql_install_db --user=mysql --basedir=/application/mysql --datadir=/data/3308/data/
启动实例,登入实例,修改密码。重启实例。

#################################################################################
附:d:\Linux运维手册\tools\mysql多实例
#3306实例配置文件
[client]
port            = 3306
socket          = /data/3306/mysql.sock

[mysql]
no-auto-rehash

[mysqld]
user    = mysql
port    = 3306
socket  = /data/3306/mysql.sock
basedir = /application/mysql
datadir = /data/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 = /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
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=/data/3306/mysql_oldboy3306.err
pid-file=/data/3306/mysqld.pid
#3307配置文件
[client]
port            = 3307
socket          = /data/3307/mysql.sock

[mysql]
no-auto-rehash

[mysqld]
user    = mysql
port    = 3307
socket  = /data/3307/mysql.sock
basedir = /application/mysql
datadir = /data/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 = /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
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=/data/3307/mysql_oldboy3307.err
pid-file=/data/3307/mysqld.pid
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息