MySQL 5.5.x single-host multi-instance configuration practices

Source: Internet
Author: User
Tags mysql backup
From: http://heylinux.com/archives/1689.html

Background/requirement:
Compile and install a MySQL database of Version 5.5 or above through source code on a server;
Store all configuration files and data in/opt/MySQL to facilitate fast migration, overall backup, and fast replication in the future;
Run two instances in the same MySQL instance, one bound to port 3306 and the other bound to port 3307;
BINLOG is not enabled for instances bound to port 3306, and data is stored in/opt/MySQL/data;
BINLOG is enabled for instances bound to port 3307 and data is stored in/opt/MySQL/data2;
Both instances adopt InnoDB as the default storage engine, and character encoding adopts UTF-8;
Both instances adopt the same performance optimization configuration parameters;

Practice/solution:
During compilation and installation, point the database configuration files my. CNF and Data Directories to the/opt/MySQL directory;
Use mysqld_multi to manage two different instances and use the same configuration file sharing performance to optimize configuration parameters;
In the same configuration file, use the [mysqld1] and [mysqld2] labels to implement differentiated configurations for different instances;

Configuration steps:
I. Compile and install MySQL
1. Install cmake

MySQL 5.5 and later versions have been replaced by the cmake tool./configure compilation and configuration method.
Therefore, we must first compile and install the cmake tool in the system source code.
# Wget http://www.cmake.org/files/v2.8/cmake-2.8.4.tar.gz

# Tar zxvf cmake-2.8.4.tar.gz

# Cd cmake-2.8.4

#./Configure
# Make
# Make install

2. Make sure that the following system software packages are installed:
Run the rpm-Qa | grep name command to verify whether all of the following software packages have been installed.

GCC * gcc-C ++ * Autoconf * automake * zlib * libxml * ncurses-devel * libgcrypt * libtool *

If the related software package is missing, you can install it online using Yum-y install, or find it directly from the system installation disc and install it using rpm-IVH.

3. system settings before installation
Create the MySQL installation directory and data storage directory
# Mkdir/opt/MySQL
# Mkdir/opt/MySQL/Data

Create users and user groups
# Groupadd MySQL
# Useradd-G MySQL

Grant the data storage directory permission
# Chown MYSQL: mysql-r/opt/MySQL/Data

4. start compiling and installing MySQL
Download the software package from http://www.mysql.com/downloads/mysqlofficial website or your sohuimage, such as the latest MySQL 5.5.20.
# Wget http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.20.tar.gz

# Tar zxvf mysql-5.5.20.tar.gz

# Cd mysql-5.5.20
# Cmake-dcmake_install_prefix =/opt/MySQL \
-Dsysconfdir =/opt/MySQL/etc \
-Dmysql_datadir =/opt/MySQL/data \
-Dmysql_tcp_port = 3306 \
-Dmysql_unix_addr =/tmp/mysqld. Sock \
-Dmysql_user = MySQL \
-Dextra_charsets = all \
-Dwith_readline = 1 \
-Dwith_ssl = System \
-Dwith_embedded_server = 1 \
-Denabled_local_infile = 1 \
-Dwith_innobase_storage_engine = 1

# Make
# Make install

The percentage of progress can be seen in make and make install, which is better than configure.

2. Create configuration files that support multiple instances
Go to the MySQL main directory
# Cd/opt/MySQL/

Delete the default data directory
# Rm-RF data

Create the required directory
# Mkdir etc TMP run log binlogs data data2
# Chown-r MYSQL: MySQL TMP run log binlogs data data2

Create My. CNF configuration file
# Vim ETC/My. CNF

## This server may run 2+ separate instances## So we use mysqld_multi to manage their services[mysqld_multi]mysqld = /opt/mysql/bin/mysqld_safemysqladmin = /opt/mysql/bin/mysqladminlog = /opt/mysql/log/mysqld_multi.loguser = root ## Used for stopping the server via mysqladmin#password = ## This is the general purpose database## The locations are default# They are left in [mysqld] in case the server is started normally instead of by mysqld_multi[mysqld1]socket = /opt/mysql/run/mysqld.sockport = 3306pid-file = /opt/mysql/run/mysqld.piddatadir = /opt/mysql/datalc-messages-dir = /opt/mysql/share/english## These support master - master replication#auto-increment-increment = 4#auto-increment-offset = 1  ## Since it is master 1#log-bin = /opt/mysql/binlogs/bin-log-mysqld1#log-bin-index = /opt/mysql/binlogs/bin-log-mysqld1.index#binlog-do-db = ## Leave this blank if you want to control it on slave#max_binlog_size = 1024M## This is exlusively for mysqld2## It is on 3307 with data directory /opt/mysqld/data2[mysqld2]socket = /opt/mysql/run/mysqld.sock2port = 3307pid-file = /opt/mysql/run/mysqld.pid2datadir = /opt/mysql/data2lc-messages-dir = /opt/mysql/share/english## Disable DNS lookups#skip-name-resolve## These support master - slave replicationlog-bin = /opt/mysql/binlogs/bin-log-mysqld2log-bin-index = /opt/mysql/binlogs/bin-log-mysqld2.index#binlog-do-db =  ## Leave this blank if you want to control it on slavemax_binlog_size = 1024M## Relay log settings#relay-log = /opt/mysql/log/relay-log-mysqld2#relay-log-index = /opt/mysql/log/relay-log-mysqld2.index#relay-log-space-limit = 4G## Slow query log settings#log-slow-queries = /opt/mysql/log/slow-log-mysqld2#long_query_time = 2#log-queries-not-using-indexes## The rest of the my.cnf is shared## Here follows entries for some specific programs## The MySQL server[mysqld]basedir = /opt/mysqltmpdir = /opt/mysql/tmpsocket = /opt/mysql/run/mysqld.sockport = 3306pid-file = /opt/mysql/run/mysqld.piddatadir = /opt/mysql/datalc-messages-dir = /opt/mysql/share/englishskip-external-lockingkey_buffer_size = 16Kmax_allowed_packet = 1Mtable_open_cache = 4sort_buffer_size = 64Kread_buffer_size = 256Kread_rnd_buffer_size = 256Knet_buffer_length = 2Kthread_stack = 128K## Increase the max connectionsmax_connections = 200## The expiration time for logs, including binlogsexpire_logs_days = 14## Set the character as utf8character-set-server = utf8collation-server = utf8_unicode_ci## This is usually only needed when setting up chained replication#log-slave-updates## Enable this to make replication more resilient against server crashes and restarts## but can cause higher I/O on the server#sync_binlog = 1## The server id, should be unique in same networkserver-id = 1## Set this to force MySQL to use a particular engine/table-type for new tables## This setting can still be overridden by specifying the engine explicitly## in the CREATE TABLE statementdefault-storage-engine = INNODB## Uncomment the following if you are using InnoDB tables#innodb_data_home_dir = /opt/mysql/data#innodb_data_file_path = ibdata1:10M:autoextend#innodb_log_group_home_dir = /opt/mysql/data## You can set .._buffer_pool_size up to 50 - 80 % of RAM## but beware of setting memory usage too highinnodb_buffer_pool_size = 16Minnodb_additional_mem_pool_size = 2M## Set .._log_file_size to 25 % of buffer pool sizeinnodb_log_file_size = 5Minnodb_log_buffer_size = 8Minnodb_flush_log_at_trx_commit = 1innodb_lock_wait_timeout = 50[mysqldump]quickmax_allowed_packet = 16M[mysql]no-auto-rehash[myisamchk]key_buffer_size = 8Msort_buffer_size = 8M[mysqlhotcopy]interactive-timeout[mysql.server]user = mysql[mysqld_safe]log-error = /opt/mysql/log/mysqld.logpid-file = /opt/mysql/run/mysqld.pidopen-files-limit = 8192[client]default-character-set = utf8

Modify the read and write permissions of my. CNF to prevent normal users from obtaining the MySQL password.
# Chown-r root: Root/opt/MySQL/etc
# Chmod 600/opt/MySQL/etc/My. CNF

3. initialize the database
Switch to MySQL user
# Su-MySQL

Go to the MySQL main directory
# Cd/opt/MySQL/

Initialize an instance [mysqld1]
# Scripts/mysql_install_db -- basedir =/opt/MySQL -- user = MySQL -- datadir =/opt/MySQL/data/

Initialize an instance [mysqld2]
# Scripts/mysql_install_db -- basedir =/opt/MySQL -- user = MySQL -- datadir =/opt/MySQL/data2/

Return to root
# Exit

Create mysqld_multi.server script
# Cp support-files/mysqld_multi.server/opt/MySQL/init. d/

# Vim/opt/MySQL/init. d/mysqld_multi.server

#!/bin/sh## A simple startup script for mysqld_multi by Tim Smith and Jani Tolonen.# This script assumes that my.cnf file exists either in /etc/my.cnf or# /root/.my.cnf and has groups [mysqld_multi] and [mysqldN]. See the# mysqld_multi documentation for detailed instructions.## This script can be used as /etc/init.d/mysql.server## Comments to support chkconfig on RedHat Linux# chkconfig: 2345 64 36# description: A very fast and reliable SQL database engine.## Version 1.0#basedir=/opt/mysqlbindir=/opt/mysql/binconf=/opt/mysql/etc/my.cnfexport PATH=$PATH:$bindirif test -x $bindir/mysqld_multithen  mysqld_multi="$bindir/mysqld_multi";else  echo "Can't execute $bindir/mysqld_multi from dir $basedir";  exit;ficase "$1" in    'start' )        "$mysqld_multi" --defaults-extra-file=$conf start $2        ;;    'stop' )        "$mysqld_multi" --defaults-extra-file=$conf stop $2        ;;    'report' )        "$mysqld_multi" --defaults-extra-file=$conf report $2        ;;    'restart' )        "$mysqld_multi" --defaults-extra-file=$conf stop $2        "$mysqld_multi" --defaults-extra-file=$conf start $2        ;;    *)        echo "Usage: $0 {start|stop|report|restart}" >&2        ;;esac

 Iv. Overall MySQL backup
# Cd/opt/
# Tar czvf mysql_5.5.20_full.tar.gz MySQL/

After the dig command is complete, directly remove mysql_5.5.20_full.tar.gz to other servers. After decompression, you can enable it directly.

V. Manage MySQL instances
Start/Close the instance [mysqld1] and [mysqld2] at the same time:
#/Opt/MySQL/init. d/mysqld_multi.server start 1, 2

Then, we can see that both MySQL instances have been successfully started.
# Netstat-lntp | grep mysqld

 
1 tcp  0  0  0.0.0.0:3306  0.0.0.0:*  LISTEN 2850/mysqld
2 tcp  0  0  0.0.0.0:3307  0.0.0.0:*  LISTEN 2946/mysqld

Disable both the instance [mysqld1] and [mysqld2]:
#/Opt/MySQL/init. d/mysqld_multi.server stop 1, 2

Start/close an instance only [mysqld1]:
#/Opt/MySQL/init. d/mysqld_multi.server start 1
#/Opt/MySQL/init. d/mysqld_multi.server stop 1

6. log on to the MySQL instance
After the instance [mysqld1] and [mysqld2] are started, log on to different instances as follows:

Log on to [mysqld1]:
#/Opt/MySQL/bin/MySQL-uroot-h127.0.0.1-p3306-P

Log on to [mysqld2]:
#/Opt/MySQL/bin/MySQL-uroot-h127.0.0.1-p3307-P

VII. Other initialization settings
1. Set the initial password for the root account of MySQL
#/Opt/MySQL/bin/mysqladmin-u root-h127.0.0.1-p3306 password 'new-password'
#/Opt/MySQL/bin/mysqladmin-u root-h127.0.0.1-p3307 password 'new-password'

2. Modify the MySQL Root Account password in the my. CNF configuration file.
# Vim/opt/MySQL/etc/My. CNF

 
1 user = root ## Used for stopping the server via mysqladmin
2 password = new-password

3. delete an empty password account for an anonymous connection
Log on to the instance [mysqld1] and [mysqld2] respectively, and run the following command:
Mysql> use MySQL; // select the system database MySQL
Mysql> select host, user, password from user; // view all users
Mysql> Delete from user where Password = "";
Mysql> flush privileges; // refresh the permission
Mysql> select host, user, password from user; // check whether all users whose passwords are empty have been deleted.
Mysql> exit;

VIII. Experience Summary
1. using the source code to compile and install MySQL may take a long time for the first time, but it is very worthwhile because we can organize all the mysql-related files by ourselves; mySQL after source code compilation and installation can be directly copied to other servers for running, greatly facilitating our future migration, backup, and new server configuration;

2. this article only uses two instances [mysqld1] and [mysqld2] For example. In fact, we can implement [mysqld3], [mysqld4], [mysqld5]... and other instances;

3. mySQL comes with several different configuration files placed under the/opt/MySQL/support-Files directory, which are my-huge.cnf, my-innodb-heavy-4G.cnf, my-large.cnf, my-medium.cnf, my-small.cnf, through the name we can intuitively understand that they are for different server configuration, this article only a little about InnoDB configuration, is taken from the my-small.cnf, because I set it on a virtual machine; in a production environment, we can optimize the server by referring to some of the parameter configurations in the my-huge.cnf or my-innodb-heavy-4G.cnf;

4. MySQL cache parameter optimization, mainly used to improve the I/O capability, can refer to here: http://heylinux.com/archives/1389.html

5. when running multiple instances on a single machine, do not use MySQL-hlocalhost or directly ignore the-H parameter to log on to the server. This is a MySQL bug, that is, if you use localhost or ignore the-H parameter, instead of specifying 127.0.0.1, even if the selected port is 3307, it will still log on to 3306. Therefore, try to avoid such confusion, use 127.0.0.1 to bind a port or socket to log on;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.