经过上一篇博文mysql系列之多实例1----介绍对mysql多实例进行了简单的介绍,本片博文将开始针对mysql多实例的第一种实现方案,基于多配置文件的mysql多实例进行部署实现。
环境: CentOS 6.5 x86_64位 采用最小化安装,系统经过了基本优化 selinux 为关闭状态,iptables 为无限制模式 mysql版本:mysql-5.5.38 源码包存放位置:/usr/local/src 源码包编译安装位置:/usr/local/mysql 数据库存放位置:/mydata
本方案仅以同一台服务器上跑2个实例为例,演示基于多配置文件的mysql多实例
一、安装mysql程序
1、准备软件环境
[ root@nolinux ~]# yum install wget make cmake gcc gcc-c++ ncurses ncurses-devel perl -y
2、准备mysql源码包
获取源码包的方式有很多,你可以去http://dev.mysql.com/downloads/mysql/自行下载
[root@nolinux ~]# cd /usr/local/src/ [root@nolinux src]# ll total 21232 -rw-r--r--. 1 root root 21739681 Jun 3 20:39 mysql-5.5.38.tar.gz [root@nolinux src]# tar zxf mysql-5.5.38.tar.gz [root@nolinux src]# ll total 21236 drwxr-xr-x. 31 7161 wheel 4096 May 12 00:39 mysql-5.5.38 -rw-r--r--. 1 root root 21739681 Jun 3 20:39 mysql-5.5.38.tar.gz
3、建立mysql用户
[root@nolinux ~]# useradd -r -u 306 mysql
4、mysql安装
[root@nolinux src]# cd mysql-5.5.38 [root@nolinux mysql-5.5.38]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.5.38 \ -DMYSQL_DATADIR=/usr/local/mysql-5.5.38/data \ -DMYSQL_UNIX_ADDR=/usr/local/mysql-5.5.38/tmp/mysql.sock \ -DDEFAULT_CHARSET=gbk \ -DDEFAULT_COLLATION=gbk_chinese_ci \ -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 [root@nolinux mysql-5.5.38]# make [root@nolinux mysql-5.5.38]# make install
5、为mysql安装目录制作软链接
编译安装完成之后,我们需要为mysql安装目录做一个link
[root@nolinux ~]# cd [root@nolinux ~]# ln -s /usr/local/mysql-5.5.38 /usr/local/mysql
6、配置 mysql 环境变量
[root@nolinux ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile [root@nolinux ~]# tail -1 /etc/profile export PATH=/usr/local/mysql/bin:$PATH [root@nolinux ~]# source /etc/profile [root@nolinux ~]# echo $PATH /usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
二、mysql 多实例部署
1、建立各个实例的对应目录
[root@nolinux ~]# mkdir /mydata/{3306,3307}/data -p [root@nolinux ~]# chown -R mysql.mysql /mydata/ [root@nolinux ~]# tree /mydata/ /mydata/ ├── 3306 # 3306端口的mysql实例目录 │ └── data # 3307端口的mysql数据目录 └── 3307 # 3307端口的mysql实例目录 └── data # 3307端口的mysql数据目录 4 directories, 0 files
2、各个实例的配置文件部署
a、查看默认模板配置文件
[root@nolinux ~]# ls /usr/local/mysql/support-files/my-*/usr/local/mysql/support-files/my-huge.cnf /usr/local/mysql/support-files/my-innodb-heavy-4G.cnf /usr/local/mysql/support-files/my-large.cnf /usr/local/mysql/support-files/my-medium.cnf /usr/local/mysql/support-files/my-small.cnf
b、为每个实例选择配置文件
这里我们在以上模板文件中选择一个
[root@nolinux ~]# cp /usr/local/mysql/support-files/my-small.cnf /mydata/3306/my.cnf [root@nolinux ~]# cp /usr/local/mysql/support-files/my-small.cnf /mydata/3307/my.cnf
c、修改各个实例的配置文件,修改结果如下
端口为3306的配置文件:
[root@nolinux ~]# cat /mydata/3306/my.cnf [client] port = 3306 socket = /mydata/3306/mysql.sock [mysql] no-auto-rehash [mysqld] user = mysql port = 3306 socket = /mydata/3306/mysql.sock basedir = /usr/local/mysql datadir = /mydata/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 = /mydata/3306/error.log log-slow-queries = /mydata/3306/slow.log pid-file = /mydata/3306/mysql.pid log-bin = /mydata/3306/mysql-bin relay-log = /mydata/3306/relay-bin relay-log-info-file = /mydata/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=/mydata/3306/mysql_3306.err pid-file=/mydata/3306/mysqld.pid
端口为3307的配置文件:
[client] port = 3307 socket = /mydata/3307/mysql.sock [mysql] no-auto-rehash [mysqld] user = mysql port = 3307 socket = /mydata/3307/mysql.sock basedir = /usr/local/mysql datadir = /mydata/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 = /mydata/3307/error.log log-slow-queries = /mydata/3307/slow.log pid-file = /mydata/3307/mysql.pid #log-bin = /mydata/3307/mysql-bin relay-log = /mydata/3307/relay-bin relay-log-info-file = /mydata/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=/mydata/3307/mysql_3307.err pid-file=/mydata/3307/mysqld.pid
3、各个实例的启动文件部署
这里我将多实例的实例启动脚本,集成到一个shell脚本里面,这样子方便启动管理!
[root@nolinux ~]# cat /etc/init.d/mysqld #!/bin/bash # # This script is used to manage open, shut down and restart the mysql server # Written by sunsky # Mail : [email protected] # QQ : 274546888 # Date : 2014-7-20 21:23:00 # . /etc/init.d/functions if ! [ $# -eq 2 ];then echo "Usage: $0 {start|stop|restart|reload|help} instance_port" exit fi PORT=$2 USER=root PASSWD='sunsky' BIN_DIR='/usr/local/mysql/bin' SOCKET_FILE=/mydata/${port}/mysql.sock DEFAULT_FILE=/mydata/$${port}/my.cnf tac () { if [ $? == 0 ];then success else failure fi } mysql_start () { if [ -e "$SOCKET_FILE" ];then echo "Start fail,Mysql instance $PORT has already started";[ -e "$SOCKET_FILE" ];tac else echo "$PORT start mysql instance" $BIN_DIR/mysqld_safe --defaults-file=$DEFAULT_FILE ;tac fi } mysql_stop() { if [ ! -e $SCOKET_FILE ];then echo "Mysql instance $PORT has stopped";[ ! -e "$SOCKET_FILE" ];tac else echo "$PORT stop mysql instance";$BIN_DIR/mysqladmin -u $USER -p$PASSWD -S $SOCKET_FILE shutdown &>/dev/null;tac fi } mysql_restart () { mysql_stop sleep 3 mysql_start } case "$1" in start) mysql_start ;; stop) mysql_stop ;; restart|reload) mysql_stop mysql_start ;; *) echo "Usage: $0 {start|stop|restart|reload|help} instance_port" esac [root@nolinux ~]# chmod u+x /etc/init.d/mysqld [root@nolinux ~]# /etc/init.d/mysqld Usage: /etc/init.d/mysqld {start|stop|restart|reload|help} instance_port
4、初始化数据库的数据文件
[root@nolinux ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata/3306/data --user=mysql WARNING: The host 'nolinux' could not be looked up with resolveip. This probably means that your libc libraries are not 100 % compatible with this binary MySQL version. The MySQL daemon, mysqld, should work normally with the exception that host name resolving will not work. This means that you should use IP addresses instead of hostnames when specifying MySQL privileges ! 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: /usr/local/mysql/bin/mysqladmin -u root password 'new-password' /usr/local/mysql/bin/mysqladmin -u root -h nolinux password 'new-password' Alternatively you can run: /usr/local/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 /usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl cd /usr/local/mysql/mysql-test ; perl mysql-test-run.pl Please report any problems at http://bugs.mysql.com/ [root@nolinux ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata/3307/data --user=mysql WARNING: The host 'nolinux' could not be looked up with resolveip. This probably means that your libc libraries are not 100 % compatible with this binary MySQL version. The MySQL daemon, mysqld, should work normally with the exception that host name resolving will not work. This means that you should use IP addresses instead of hostnames when specifying MySQL privileges ! 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: /usr/local/mysql/bin/mysqladmin -u root password 'new-password' /usr/local/mysql/bin/mysqladmin -u root -h nolinux password 'new-password' Alternatively you can run: /usr/local/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 /usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl cd /usr/local/mysql/mysql-test ; perl mysql-test-run.pl Please report any problems at http://bugs.mysql.com/
5、多实例的启动测试
[root@nolinux ~]# /etc/init.d/mysqld start 3306 3306 start mysql instance [root@nolinux ~]# [ OK ] [root@nolinux ~]# /etc/init.d/mysqld start 3307 3307 start mysql instance [root@nolinux ~]# [ OK ]
6、更改mysql的root用户
[root@nolinux ~]# mysqladmin -uroot password 'sunsky' -S /mydata/3306/mysql.sock [root@nolinux ~]# lsof -i tcp:3306 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 22789 mysql 12u IPv4 73079 0t0 TCP *:mysql (LISTEN) [root@nolinux ~]# mysqladmin -uroot password 'sunsky' -S /mydata/3307/mysql.sock [root@nolinux ~]# lsof -i tcp:3307 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 24296 mysql 11u IPv4 74117 0t0 TCP *:opsession-prxy (LISTEN) [root@nolinux ~]# netstat -lntup | grep 330 tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 24296/mysqld tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 22789/mysqld
7、多实例的关闭和重启测试
[root@nolinux ~]# /etc/init.d/mysqld stop 3306 3306 stop mysql instance [root@nolinux ~]# [ OK ] [root@nolinux ~]# /etc/init.d/mysqld restart 3306 3306 stop mysql instance 3306 start mysql instance [FAILED] [root@nolinux ~]# [ OK ] [root@nolinux ~]# /etc/init.d/mysqld stop 3307 3307 stop mysql instance [root@nolinux ~]# [ OK ] [root@nolinux ~]# /etc/init.d/mysqld restart 3307 3307 stop mysql instance 3307 start mysql instance [FAILED] [root@nolinux ~]# [ OK ]
8、配置多实例的开机自启动
通过将mysql多实例的脚本与对应的参数放入/etc/rc.local文件,来实现mysql多实例的开机自启动
[root@nolinux ~]# echo '# To start the mysql instance boot since 3306' >> /etc/rc.local [root@nolinux ~]# echo '/etc/init.d/mysqld start 3306' >> /etc/rc.local [root@nolinux ~]# echo '# To start the mysql instance boot since 3307' >> /etc/rc.local [root@nolinux ~]# echo '/etc/init.d/mysqld start 3307' >> /etc/rc.local [root@nolinux ~]# tail -4 /etc/rc.local # To start the mysql instance boot since 3306 /etc/init.d/mysqld start 3306 # To start the mysql instance boot since 3307 /etc/init.d/mysqld start 3307
9、mysql实例部署之后的安全优化
实例刚刚部署完之后的安全优化,主要有两方面:
1、为root设置密码,由于我们在前面已经设置过了,这里就不再设置了
2、我们要查看mysql默认的用户,并将多余账户删除掉
这里仅仅以,端口为3306的mysql实例安全优化为例,做演示:
[root@nolinux ~]# mysql -uroot -psunsky -S /mydata/3306/mysql.sock Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.5.38-log Source distribution Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +------------------------+ | Database | +------------------------+ | information_schema | | mysql | | performance_schema | | test | +------------------------+ 4 rows in set (0.00 sec) mysql> drop database test; Query OK, 0 rows affected (0.02 sec) mysql> show databases; +------------------------+ | Database | +------------------------+ | information_schema | | mysql | | performance_schema | +------------------------+ 3 rows in set (0.00 sec) mysql> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | 127.0.0.1 | | root | ::1 | | | localhost | | root | localhost | | | nolinux | | root | nolinux | +------+-----------+ 6 rows in set (0.00 sec) mysql> delete from mysql.user where user='root' and host='::1'; Query OK, 1 row affected (0.00 sec) mysql> delete from mysql.user where user='' and host='localhost'; Query OK, 1 row affected (0.00 sec) mysql> delete from mysql.user where user='' and host='nolinux'; Query OK, 1 row affected (0.00 sec) mysql> delete from mysql.user where user='root' and host='nolinux'; Query OK, 1 row affected (0.00 sec) mysql> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | 127.0.0.1 | | root | localhost | +------+-----------+ 2 rows in set (0.00 sec)
端口为3307的mysql实例优化,操作步骤如上!
你也可以使用如下方法一键优化!
mysql -uroot -psunsky -S /mydata/3306/mysql.sock << EOF drop database test; show databases; delete from mysql.user where user='root' and host='::1'; delete from mysql.user where user='' and host='localhost'; delete from mysql.user where user='' and host='nolinux'; delete from mysql.user where user='root' and host='nolinux'; select user,host from mysql.user; EOF
以上就是基于多配置文件方案的mysql多实例部署的整个完整操作了!希望能对大家有所帮助!下面补充一个,在现有基于多配置文件的mysql多配置文件环境下,增加实例的操作!
增加mysql实例案例
此处以增加一个mysql实例为例:
1、建立新实例对应的目录并授权
[root@nolinux ~]# mkdir -p /mydata/3308/data [root@nolinux ~]# cp /mydata/3306/my.cnf /mydata/3308/ [root@nolinux ~]# ll -d /mydata/3308 drwxr-xr-x. 3 mysql mysql 4096 Jun 4 00:35 /mydata/3308
2、更改my.cnf文件为3308专用
[root@nolinux ~]# cp /mydata/3306/my.cnf /mydata/3308/ [root@nolinux ~]# sed -i 's#3306#3308#g' /mydata/3308/my.cnf
3、初始化端口为3308的mysql实例的数据文件
[root@nolinux ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata/3308/data --user=mysql WARNING: The host 'nolinux' could not be looked up with resolveip. This probably means that your libc libraries are not 100 % compatible with this binary MySQL version. The MySQL daemon, mysqld, should work normally with the exception that host name resolving will not work. This means that you should use IP addresses instead of hostnames when specifying MySQL privileges ! 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: /usr/local/mysql/bin/mysqladmin -u root password 'new-password' /usr/local/mysql/bin/mysqladmin -u root -h nolinux password 'new-password' Alternatively you can run: /usr/local/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 /usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl cd /usr/local/mysql/mysql-test ; perl mysql-test-run.pl Please report any problems at http://bugs.mysql.com/
4、启动测试
[root@nolinux ~]# /etc/init.d/mysqld start 3308 3308 start mysql instance [root@nolinux ~]# [ OK ] [root@nolinux ~]# lsof -i tcp:3308 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 25938 mysql 12u IPv4 76028 0t0 TCP *:tns-server (LISTEN) [root@nolinux ~]# netstat -lntup |grep 3308 tcp 0 0 0.0.0.0:3308 0.0.0.0:* LISTEN 25938/mysqld
5、配置多实例的开机自启动
[root@nolinux ~]# echo '# To start the mysql instance boot since 3308' >> /etc/rc.local [root@nolinux ~]# echo '/etc/init.d/mysqld start 3308' >> /etc/rc.local [root@nolinux ~]# tail -2 /etc/rc.local # To start the mysql instance boot since 3308 /etc/init.d/mysqld start 3308
6、安全优化
[root@nolinux ~]# mysqladmin -uroot password 'sunsky' -S /mydata/3308/mysql.sock [root@nolinux ~]# mysql -uroot -psunsky -S /mydata/3308/mysql.sock << EOF > drop database test; > show databases; > delete from mysql.user where user='root' and host='::1'; > delete from mysql.user where user='' and host='localhost'; > delete from mysql.user where user='' and host='nolinux'; > delete from mysql.user where user='root' and host='nolinux'; > select user,host from mysql.user; > EOF Database information_schema mysql performance_schema user host root 127.0.0.1 root localhost
OK!
上面就是在现有基于多配置文件的mysql多配置文件环境下,增加实例的操作!