上一篇博文mysql系列之多例項2—-基於多配置檔案介紹了,如何部署基於多配置檔案的mysql多例項,本篇博文將介紹基於mysql自帶的mysqld_multi工具來如何實現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 /etc/my.cnf
c、修改預設配置檔案,修改結果如下
[root@nolinux ~]# cat /etc/my.cnf [mysqld_multi] mysqld = /usr/local/mysql/bin/mysqld_safe mysqladmin = /usr/local/mysql/bin/mysqladmin user = mysql # 如果該地方不是root使用者,下面則需要在資料庫為該使用者新增shutdown許可權,不然mysqld_multi將無法關閉例項 password = sunsky [mysqld1] port = 3306 server-id = 1 socket = /mydata/3306/mysql.sock pid-file = /mydata/3306/mysql.pid datadir = /mydata/3306/data user = mysql skip-name-resolve lower_case_table_names=1 innodb_file_per_table=1 back_log = 50 max_connections = 300 max_connect_errors = 1000 table_open_cache = 2048 max_allowed_packet = 16M binlog_cache_size = 2M max_heap_table_size = 64M sort_buffer_size = 2M join_buffer_size = 2M thread_cache_size = 64 thread_concurrency = 8 query_cache_size = 64M query_cache_limit = 2M ft_min_word_len = 4 default-storage-engine = innodb thread_stack = 192K transaction_isolation = REPEATABLE-READ tmp_table_size = 64M log-bin=mysql-bin binlog_format=mixed slow_query_log long_query_time = 1 key_buffer_size = 8M read_buffer_size = 2M read_rnd_buffer_size = 2M bulk_insert_buffer_size = 64M myisam_sort_buffer_size = 128M myisam_max_sort_file_size = 10G myisam_repair_threads = 1 myisam_recover innodb_additional_mem_pool_size = 16M innodb_buffer_pool_size = 200M innodb_data_file_path = ibdata1:10M:autoextend innodb_file_io_threads = 8 innodb_thread_concurrency = 16 innodb_flush_log_at_trx_commit = 1 innodb_log_buffer_size = 16M innodb_log_file_size = 512M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 60 innodb_lock_wait_timeout = 120 [mysqld2] port = 3307 server-id = 2 socket = /mydata/3307/mysql.sock pid-file = /mydata/3307/mysql.pid datadir = /mydata/3307/data user = mysql skip-name-resolve lower_case_table_names=1 innodb_file_per_table=1 back_log = 50 max_connections = 300 max_connect_errors = 1000 table_open_cache = 2048 max_allowed_packet = 16M binlog_cache_size = 2M max_heap_table_size = 64M sort_buffer_size = 2M join_buffer_size = 2M thread_cache_size = 64 thread_concurrency = 8 query_cache_size = 64M query_cache_limit = 2M ft_min_word_len = 4 default-storage-engine = innodb thread_stack = 192K transaction_isolation = REPEATABLE-READ tmp_table_size = 64M log-bin=mysql-bin binlog_format=mixed slow_query_log long_query_time = 1 key_buffer_size = 8M read_buffer_size = 2M read_rnd_buffer_size = 2M bulk_insert_buffer_size = 64M myisam_sort_buffer_size = 128M myisam_max_sort_file_size = 10G myisam_repair_threads = 1 myisam_recover innodb_additional_mem_pool_size = 16M innodb_buffer_pool_size = 200M innodb_data_file_path = ibdata1:10M:autoextend innodb_file_io_threads = 8 innodb_thread_concurrency = 16 innodb_flush_log_at_trx_commit = 1 innodb_log_buffer_size = 16M innodb_log_file_size = 512M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 60 innodb_lock_wait_timeout = 120 [mysqldump] quick max_allowed_packet = 256M [mysql] no-auto-rehash prompt=\u@\d \R:\m> [myisamchk] key_buffer_size = 512M sort_buffer_size = 512M read_buffer = 8M write_buffer = 8M [mysqlhotcopy] interactive-timeout [mysqld_safe] open-files-limit = 8192
3、初始化各個mysql例項的資料檔案
[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/
4、多例項的啟動測試
[root@nolinux ~]# /usr/local/mysql/bin/mysqld_multi start # 預設start後面不跟數字,就表示全部例項都啟動 [root@nolinux ~]# lsof -i tcp:3306 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 31208 mysql 14u IPv4 81308 0t0 TCP *:mysql (LISTEN) [root@nolinux ~]# lsof -i tcp:3306 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 31208 mysql 14u IPv4 81308 0t0 TCP *:mysql (LISTEN) 補充: 僅啟動某一個 [root@nolinux ~]# /usr/local/mysql/bin/mysqld_multi start 1 # 這裡的1是我們在/etc/my.cnf中[mysqld1]標題中的數字,這個數字可以隨便起,但是最好保證,數字是越來越大的!
5、多例項的關閉
[root@nolinux ~]# /usr/local/mysql/bin/mysql -S /mydata/3306/mysql.sock -e "grant shutdown on *.* to `mysql`@`localhost` identified by `sunsky` [root@nolinux ~]# /usr/local/mysql/bin/mysql -S /mydata/3307/mysql.sock -e "grant shutdown on *.* to `mysql`@`localhost` identified by `sunsky` [root@nolinux ~]# /usr/local/mysql/bin/mysqld_multi stop [root@nolinux ~]# netstat -lntup | grep 330
6、配置多例項的開機自啟動
通過將 mysql 多例項的命令與對應的引數放入/etc/rc.local檔案,來實現mysql多例項的開機自啟動
[root@nolinux ~]# echo `# To start the mysql instance boot since 3306` >> /etc/rc.local [root@nolinux ~]# echo `/usr/local/mysql/bin/mysqld_multi start 1` >> /etc/rc.local [root@nolinux ~]# echo `# To start the mysql instance boot since 3307` >> /etc/rc.local [root@nolinux ~]# echo `/usr/local/mysql/bin/mysqld_multi start 2` >> /etc/rc.local [root@nolinux ~]# tail -4 /etc/rc.local # To start the mysql instance boot since 3306 /usr/local/mysql/bin/mysqld_multi start 1 # To start the mysql instance boot since 3307 /usr/local/mysql/bin/mysqld_multi start 2
7、mysql例項部署之後的安全優化
例項剛剛部署完之後的安全優化,主要有兩方面:
1、為root設定密碼,由於我們在前面已經設定過了,這裡就不再設定了
2、我們要檢視mysql預設的使用者,並將多餘賬戶刪除掉
這裡僅僅以,埠為3306的mysql例項安全優化為例,做演示:
[root@nolinux ~]# mysqladmin -uroot password `sunsky` -S /mydata/3308/mysql.sock [root@nolinux ~]# 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 Database information_schema mysql performance_schema user host root 127.0.0.1 mysql localhost root localhost
以上就是基於mysqld_multi方案的mysql多例項部署的整個完整操作了!希望能對大家有所幫助!下面補充一個,在現有基於mysqld_multi的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 ~]# cat >> /etc/my.cnf << EOF [mysqld3] port = 3308 server-id = 3 socket = /mydata/3308/mysql.sock pid-file = /mydata/3308/mysql.pid datadir = /mydata/3308/data user = mysql skip-name-resolve lower_case_table_names=1 innodb_file_per_table=1 back_log = 50 max_connections = 300 max_connect_errors = 1000 table_open_cache = 2048 max_allowed_packet = 16M binlog_cache_size = 2M max_heap_table_size = 64M sort_buffer_size = 2M join_buffer_size = 2M thread_cache_size = 64 thread_concurrency = 8 query_cache_size = 64M query_cache_limit = 2M ft_min_word_len = 4 default-storage-engine = innodb thread_stack = 192K transaction_isolation = REPEATABLE-READ tmp_table_size = 64M log-bin=mysql-bin binlog_format=mixed slow_query_log long_query_time = 1 key_buffer_size = 8M read_buffer_size = 2M read_rnd_buffer_size = 2M bulk_insert_buffer_size = 64M myisam_sort_buffer_size = 128M myisam_max_sort_file_size = 10G myisam_repair_threads = 1 myisam_recover innodb_additional_mem_pool_size = 16M innodb_buffer_pool_size = 200M innodb_data_file_path = ibdata1:10M:autoextend innodb_file_io_threads = 8 innodb_thread_concurrency = 16 innodb_flush_log_at_trx_commit = 1 innodb_log_buffer_size = 16M innodb_log_file_size = 512M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 60 innodb_lock_wait_timeout = 120 EOF
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 ~]# /usr/local/mysql/bin/mysqld_multi start 3 [root@nolinux ~]# lsof -i tcp:3308 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 38438 mysql 14u IPv4 86554 0t0 TCP *:mysql (LISTEN) [root@nolinux ~]# netstat -lntup |grep 3308 tcp 0 0 0.0.0.0:3308 0.0.0.0:* LISTEN 38438/mysqld
5、關閉測試
[root@nolinux ~]# /usr/local/mysql/bin/mysql -S /mydata/3308/mysql.sock -e "grant shutdown on *.* to `mysql`@`localhost` identified by `sunsky` [root@nolinux ~]# /usr/local/mysql/bin/mysqld_multi stop 3 [root@nolinux ~]# netstat -lntup | grep 3308
6、配置多例項的開機自啟動
[root@nolinux ~]# echo `# To start the mysql instance boot since 3308` >> /etc/rc.local [root@nolinux ~]# echo `/usr/local/mysql/bin/mysqld_multi start 3` >> /etc/rc.local [root@nolinux ~]# tail -2 /etc/rc.local # To start the mysql instance boot since 3308 /usr/local/mysql/bin/mysqld_multi start 3
7、安全優化
[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 mysql localhost root localhost
OK!
上面就是在現有基於mysqld_multi的mysql多配置檔案環境下,增加例項的操作!