What is multi-instance of Mysql?
Simply put, Mysql multi-instance is to open multiple different service ports (such as 3306,3307) simultaneously run multiple Mysql service process, the service process through different listening socket to provide services of different service ports on a single server.
Multi-instance Msyql features:
These share a Mysql Mysql installer, without using the configuration file (the my.cnf) start the program and data files. In providing the service, the multi-instance Mysql seems logical separate, they are in accordance with the set value corresponding to the configuration file, the server response to obtain the number of hardware resources.
Added:
In fact, many web servers are configurable multi-instance, such as Nginx Apache Redis Memcache can configure multiple instances.
mysql multi-role instance:
1) efficient use of server resources when a single server is the time remaining resources, the remaining resources can take advantage of providing more services, and resources can be achieved logical isolation. 2) saving server resources when the company financial strain, but requires its own database and try to provide services independently, but need a master-slave replication, multi-instance, all the better. But there are drawbacks mysql multi-instance, when a high concurrent database instances or full SQL query, there will be problems to seize the resources of each other.
Mysql production scenarios plurality example
1. financial strain selection companies
such as: 3 9-15 servers deployed instances, separated from the cross shots copy, and write data backup, so that each of the servers can reach 9-15 only install a database have effect. The so-called independence is relative.
2. concurrent access is not a big business
when business volume is not too large, the server's resources are basically a waste, then it is suitable for the application of multi-instance, if the optimization of SQL statements to do better, Mysql and more examples would be a very worthwhile use of technology can greatly complicated by rational allocation of system resources, and with good service, there will not be a big problem.
3. Portal Mysql multi-instance application scenarios
such as: Baidu company, a department in IBM server 48-core CPU, memory 96GB. Such configuration server may run instances 3-4; sina multiple instances networks are used, the memory is about 48G.
Added: sina single database network database instance 1-4 majority, the majority of which up to 1-2, as the big business takes more than the machine. DELL R510 server is the majority, CPU E5210 48G memory drives do 12 * 300G SAS RAID10; In addition, sina website install the database, commonly used to compile installation installed and customized to make the rpm package after optimization, in order to harmonize use.
Examples of multi-configuration scheme Mysql
1. The single configuration file, start the program more than a single instance deployment scenarios
Mysql official document mentioned in a single configuration file, start the program single instance multi-instance deployment is not recommended for this program, referred to herein as knowledge points. Official my.cnf configuration file example configuration is as follows:
[mysqld_multil] mysqld = /usr/bin/mysqld_safe mysqladmin = /usr/bin/mysqladmin user = mysql [mysqld1] socket = /var/lib/mysql/mysql.sock port = 3306 pid-file = /var/lib/mysql/mysql.pid datadir = /var/lib/mysql/ user = mysql [mysqld2] socket =/mnt/data/db1/mysql.sock port = 3302 pid-file = /mnt/data/db1/mysql.pid datadir = /mnt/data/db1/ user = mysql skip-name-resolve server-id=10 dafault-storage-engine=innodb innodb_buffer_pool_size=512M innodb_additional_mem_pool=10M default_character_set=utf8 character_set_server=utf8 #read-only relay-log-space-limit=3G expire_logs_day=20
Start command is as follows: mysqld_multi --config-file=/data/mysql/my_multi.cnf start 1,2
For this scheme, the disadvantage is the coupling is too high, a configuration file, bad management, work to develop uniform principles and operation and maintenance, and reduce the coupling .
2. Multi-profile multi-boot deployment plan
multiple configuration files, multi-start deployment plan, is the recommended solution. The following configuration example of view:
[@ Mysql the root /] Tree / Data / Data | - 3306 | | --data "3306 examples of the data file == | | --my.cnf" == 3306 example of a configuration file | | --mysql "= = 3306 instance startup file --3,307 | | --data "== 3307 instance data files | | --my.cnf" == 3307 instance configuration | | --mysql "== 3307 instance startup file
Mysql multi-instance configuration process:
1. Install Msyql required dependencies software compiler 2. Mysql installation need 3. The installation Mysql 4. configure multiple instances 5. Initialize Mysql database 6. Start Mysql 7. The log MySQL 8. The troubleshooting instructions 9. boot from the start
Mysql multi-instance database installation
1. Install Msyql required dependencies
yum install ncurses-devel libaio-devel -y rpm -qa ncurses-devel libaio-devel libaio-devel-0.3.107-10.e16x86_64 ncurses-devel-5.7-3.20090208.c16x86_64
database:
IP address of the server name Host C3-Mysql database 172.16.1.52/24 db02
2. Mysql compile and install the required software
ls -lh cmake-2.8.8.tar.gz tar xf cmake-2.8.8.tar.gz cd cmake-2.8.8 ./configure gmake gmake install which cmake /usr/local/bin/cmake cd ../
Or you can use yum install (yum install recommended) yum install cmake -y
3. Start the installation Mysql
# Create mysql user useradd mysql -s / sbin / nologin -M the above mentioned id mysql # to enter the download path cd / Home / Oldboy / Tools wget http://downloads.mysql.com/archives/get/file/mysql-5.5. 32.tar.gz
Mysql source packages and binary package name
Mysql Software name Msyql source installation package mysql-5.5.32.tar.gz (As used herein, the installation package) Mysql binary packages mysql-5.5.32-linux-x86_64.tar.gz
# Extract the installation package
tar xf mysql-5.5.32.tar.gz cd mysql-5.5.32 [root@db02 mysql-5.5.32]# cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.32 \ -DMYSQL_DATADIR=/application/mysql-5.5.32/data \ -DMYSQL_UNIX_ADDR=/application/mysql-5.5.32/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@db02 mysql-5.5.32]# make && make install [root@db02 mysql-5.5.32]# ln -s /application/mysql-5.5.32/ /application/mysql [root@db02 mysql-5.5.32]# cd /application/mysql [root@db02 mysql]# ls bin data include lib mysql-test scripts sql-bench COPYING docs INSTALL-BINARY man README share support-files
Configuring multiple instances
[root@db02 mysql]# mkdir -p /data/{3306,3307}/data [root@db02 ~]# tree /data/ /data/ ├── 3306 │ ├── data │ ├── my.cnf │ └── mysql └── 3307 ├── data ├── my.cnf └── mysql 4 directories, 4 files
3306 Folder Description:
#### vim my.cnf ####
[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
####vim mysql####
#!/bin/sh ################################################ #this scripts is created by liuliya at 2016-12-09 #oldboy QQ:865205026 #site:http://www.liuliya.com ################################################ #init port=3306 mysql_user="root" mysql_pwd="oldboy" CmdPath="/application/mysql/bin" mysql_sock="/data/${port}/mysql.sock" #startup function function_start_mysql() { if [ ! -e "$mysql_sock" ];then printf "Starting MySQL...\n" /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${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 /data/${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: /data/${port}/mysql {start|stop|restart}\n" esac
3307 Folder Description:
#### vim my.cnf
[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
####vim mysql####
#!/bin/sh ################################################ #this scripts is created by liuliya at 2016-12-09 #oldboy QQ:865205026 #site:http://www.liuliya.com ################################################ #init port=3307 mysql_user="root" mysql_pwd="oldboy" CmdPath="/application/mysql/bin" mysql_sock="/data/${port}/mysql.sock" #startup function function_start_mysql() { if [ ! -e "$mysql_sock" ];then printf "Starting MySQL...\n" /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${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 /data/${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: /data/${port}/mysql {start|stop|restart}\n" esac
# Mysql start command to increase the execution permission find /data -type f -name "mysql"|xargs chmod +x
# Change is a group / data directory chwon -R mysql.mysql /data/
5. initialize Mysql database (mysql load the library and its management table)
[root@db02 ~]# cd /application/mysql/scripts/ [root@db02 scripts]# ls mysql_install_db
# 3306 Initializes an instance of [root@db02 scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql
return 2 OK proven successful
# 3307 instance initialization [root@db02 scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql
returns two proven successful OK
6. Start Mysql
[root@db02 scripts]# /data/3306/mysql start [root@db02 scripts]# /data/3307/mysql start
# View port netstat -lntup|grep 330
# mysql configuration environment variable cp /application/mysql/bin/* /usr/local/sbin/
# View environment variables echo $PATH
7. Log mysql (need to develop sock)
mysql -S /data/3306/mysql.sock mysql -S /data/3307/mysql.sock
8. Troubleshooting Instructions
1. View Log cat mysql_oldboy3306.err
2. If it is found to start the corresponding port Msyql example not shown, please wait for a few seconds in a little inspection, service start Mysql slower than some other Web.
PS: Always check the services running log is a good habit, but also the only way to become a master.
9. boot from the start
specific commands:
echo "//mysql mutil instances" >>/etc/rc.local echo "/data/3306/mysql start" >>/etc/rc.local echo "/data/3307/mysql start" >>/etc/rc.local
How to add a mysql instance
specific set of commands:
mkdir -p /data/3308/data \cp /data/3306/my.cnf /data/3308/ \cp /data/3306/mysql /data/3308/ cd 3308/ sed -i 's/3306/3308/g' /data/3308/my.cnf sed -i 's/server-id = 1/server-id = 8/g' /data/3308/my.cnf sed -i 's/3306/3308/g' /data/3308/mysql chown -R mysql.mysql /data/3308 chmod 700 /data/3308/mysql
# Mysql database instance initialization 3308
[root@db02 ~]# cd /application/mysql/scripts/
# 3306 Initializes an instance of [root@db02 scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3308/data --user=mysql
# 3308 to start the database instance/data/3308/mysql start