오래전 이야기/Database

한대의 서버에 여러개의 mysql 설치하기

리눅스 엔지니어였던 2008. 9. 15. 17:08
 ※ 기존의 mysql이 설치되어 있다고 가정.

cp /etc/my.cnf /etc/my1.cnf
cp /etc/my.cnf /etc/my2.cnf

- port, socket, server-id 변경.

cp /opt/mysql/data /opt/mysql2/data
cp /opt/mysql/data /opt/mysql3/data

/usr/sbin/mysqld-max --defaults-file=/etc/my2.cnf --user=mysql --datadir=/home/mysql2/data --pid-file=/var/lib/mysql3onu.pid &

/usr/sbin/mysqld-max --defaults-file=/etc/my2.cnf --user=mysql --datadir=/home/mysql2/data --pid-file=/home/mysql2/data/sh-dbb-1.hhonline.com.cn.pid --skip-slave-start&


/usr/sbin/mysqld-max --defaults-file=/etc/my3.cnf --user=mysql --datadir=/home/mysql3/data --pid-file=/opt/mysql3/data/gonu.pid --skip-slave-start &

/usr/sbin/mysqld-max --defaults-file=/etc/my3.cnf --user=mysql --datadir=/home/mysql3/data --pid-file=/home/mysql3/data/gonu.pid &

/usr/sbin/mysqld-max --defaults-file=/etc/my2.cnf --user=mysql --datadir=/home/mysql2/data --pid-file=/home/mysql2/data/gonu.pid &

--client 접속

mysql -uroot -p --socket=/var/lib/mysql/mysql2.sock --port=3307

mysql -uroot -p --socket=/var/lib/mysql/mysql3.sock --port=3308



ex) my.cnf

[root@gonu data]# cat /etc/my.cnf
# Example mysql config file for large systems.
#
# This is for large system with memory = 512M where the system runs mainly
# MySQL.
#
# You can copy this file to
# /etc/my.cnf to set global options,
# mysql-data-dir/my.cnf to set server-specific options (in this
# installation this directory is /usr/local/mysql/var) or
# ~/.my.cnf to set user-specific options.
#
# One can in this file use all long options that the program supports.
# If you want to know which options a program support, run the program
# with --help option.

# The following options will be passed to all MySQL clients
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port = 3306
socket = /tmp/mysql.sock
skip-locking
set-variable = key_buffer=256M
set-variable = max_allowed_packet=1M
set-variable = table_cache=256
set-variable = sort_buffer=1M
set-variable = record_buffer=1M
set-variable = myisam_sort_buffer_size=64M
set-variable = thread_cache=8
# Try number of CPU's*2 for thread_concurrency
set-variable = thread_concurrency=8
log-bin
server-id = 2

#Uncomment the following if you are using Replication
master-host=192.168.0.1
master-user=
master-password=
master-port=3306
master-connect-retry=15
replicate-do-db=CharDev
#replicate-do-db=Account
#replicate-do-db=mysql
replicate-do-db=test
#replicate-do-db=NewWorld

# Uncomment the following if you are using BDB tables
#set-variable = bdb_cache_size=64M
#set-variable = bdb_max_lock=100000

# Uncomment the following if you are using InnoDB tables
innodb_data_home_dir = /opt/mysql/var/
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /opt/mysql/var/
innodb_log_arch_dir = /opt/mysql/var/
set-variable = innodb_buffer_pool_size=56M
set-variable = innodb_additional_mem_pool_size=20M
set-variable = innodb_log_file_size=5M
#set-variable = innodb_log_file_size=34M
set-variable = innodb_log_buffer_size=8M
innodb_flush_log_at_trx_commit=0
set-variable = innodb_lock_wait_timeout=50
set-variable = innodb_mirrored_log_groups=1
set-variable = innodb_log_files_in_group=3

# Point the following paths to different dedicated disks
#tmpdir = /tmp/
#log-update = /path-to-dedicated-directory/hostname

[mysqldump]
quick
set-variable = max_allowed_packet=16M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[isamchk]
set-variable = key_buffer=128M
set-variable = sort_buffer=128M
set-variable = read_buffer=2M
set-variable = write_buffer=2M

[myisamchk]
set-variable = key_buffer=128M
set-variable = sort_buffer=128M
set-variable = read_buffer=2M
set-variable = write_buffer=2M

[mysqlhotcopy]
interactive-timeout

ex) my1.cnf

[root@gonu data]# cat /etc/my1.cnf
# Example mysql config file for large systems.
#
# This is for large system with memory = 512M where the system runs mainly
# MySQL.
#
# You can copy this file to
# /etc/my.cnf to set global options,
# mysql-data-dir/my.cnf to set server-specific options (in this
# installation this directory is /usr/local/mysql/var) or
# ~/.my.cnf to set user-specific options.
#
# One can in this file use all long options that the program supports.
# If you want to know which options a program support, run the program
# with --help option.

# The following options will be passed to all MySQL clients
[client]
#password = your_password
port = 3307
socket = /tmp/mysql1.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port = 3307
socket = /tmp/mysql1.sock
skip-locking
set-variable = key_buffer=256M
set-variable = max_allowed_packet=1M
set-variable = table_cache=256
set-variable = sort_buffer=1M
set-variable = record_buffer=1M
set-variable = myisam_sort_buffer_size=64M
set-variable = thread_cache=8
# Try number of CPU's*2 for thread_concurrency
set-variable = thread_concurrency=8
log-bin
server-id = 2

#Uncomment the following if you are using Replication
#master-host=192.168.0.1
#master-user=
#master-password=
#master-port=3306
#master-connect-retry=15
#replicate-do-db=CharDev
#replicate-do-db=Account
#replicate-do-db=mysql
#replicate-do-db=test
#replicate-do-db=NewWorld

# Uncomment the following if you are using BDB tables
#set-variable = bdb_cache_size=64M
#set-variable = bdb_max_lock=100000

# Uncomment the following if you are using InnoDB tables
innodb_data_home_dir = /opt/mysql2/var/
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /opt/mysql2/var/
innodb_log_arch_dir = /opt/mysql2/var/
set-variable = innodb_buffer_pool_size=25M
set-variable = innodb_additional_mem_pool_size=20M
set-variable = innodb_log_file_size=5M
#set-variable = innodb_log_file_size=54M
set-variable = innodb_log_buffer_size=8M
innodb_flush_log_at_trx_commit=0
set-variable = innodb_lock_wait_timeout=50
set-variable = innodb_mirrored_log_groups=1
set-variable = innodb_log_files_in_group=3

# Point the following paths to different dedicated disks
#tmpdir = /tmp/
#log-update = /path-to-dedicated-directory/hostname

[mysqldump]
quick
set-variable = max_allowed_packet=16M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[isamchk]
set-variable = key_buffer=128M
set-variable = sort_buffer=128M
set-variable = read_buffer=2M
set-variable = write_buffer=2M

[myisamchk]
set-variable = key_buffer=128M
set-variable = sort_buffer=128M
set-variable = read_buffer=2M
set-variable = write_buffer=2M

[mysqlhotcopy]
interactive-timeout

ex) my2.cnf

[root@gonu data]# cat /etc/my2.cnf
# Example mysql config file for large systems.
#
# This is for large system with memory = 512M where the system runs mainly
# MySQL.
#
# You can copy this file to
# /etc/my.cnf to set global options,
# mysql-data-dir/my.cnf to set server-specific options (in this
# installation this directory is /usr/local/mysql/var) or
# ~/.my.cnf to set user-specific options.
#
# One can in this file use all long options that the program supports.
# If you want to know which options a program support, run the program
# with --help option.

# The following options will be passed to all MySQL clients
[client]
#password = your_password
port = 3308
socket = /tmp/mysql2.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port = 3308
socket = /tmp/mysql2.sock
skip-locking
set-variable = key_buffer=256M
set-variable = max_allowed_packet=1M
set-variable = table_cache=256
set-variable = sort_buffer=1M
set-variable = record_buffer=1M
set-variable = myisam_sort_buffer_size=64M
set-variable = thread_cache=8
# Try number of CPU's*2 for thread_concurrency
set-variable = thread_concurrency=8
log-bin
server-id = 3

#Uncomment the following if you are using Replication
#master-host=192.168.0.1
#master-user=
#master-password=
#master-port=3306
#master-connect-retry=15
#replicate-do-db=CharDev
#replicate-do-db=Account
#replicate-do-db=mysql
#replicate-do-db=test
#replicate-do-db=NewWorld

# Uncomment the following if you are using BDB tables
#set-variable = bdb_cache_size=64M
#set-variable = bdb_max_lock=100000

# Uncomment the following if you are using InnoDB tables
innodb_data_home_dir = /opt/mysql3/var/
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /opt/mysql3/var/
innodb_log_arch_dir = /opt/mysql3/var/
set-variable = innodb_buffer_pool_size=56M
set-variable = innodb_additional_mem_pool_size=20M
set-variable = innodb_log_file_size=5M
#set-variable = innodb_log_file_size=34M
set-variable = innodb_log_buffer_size=8M
innodb_flush_log_at_trx_commit=0
set-variable = innodb_lock_wait_timeout=50
set-variable = innodb_mirrored_log_groups=1
set-variable = innodb_log_files_in_group=3

# Point the following paths to different dedicated disks
#tmpdir = /tmp/
#log-update = /path-to-dedicated-directory/hostname

[mysqldump]
quick
set-variable = max_allowed_packet=16M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[isamchk]
set-variable = key_buffer=128M
set-variable = sort_buffer=128M
set-variable = read_buffer=2M
set-variable = write_buffer=2M

[myisamchk]
set-variable = key_buffer=128M
set-variable = sort_buffer=128M
set-variable = read_buffer=2M
set-variable = write_buffer=2M

[mysqlhotcopy]
interactive-timeout

ex) ps -ef |grep mysqld

[root@gonu data]# ps -ef |grep mysqld
root 6692 1 0 15:01 pts/2 00:00:00 /bin/sh /usr/bin/safe_mysqld --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/gonu.pid
mysql 6715 6692 0 15:01 pts/2 00:00:00 /usr/sbin/mysqld-max --basedir=/ --datadir=/var/lib/mysql --user=mysql --pid-file=/var/lib/mysql/gonu.pid --skip-locking
mysql 6816 5543 0 15:18 pts/2 00:00:00 /usr/sbin/mysqld-max --defaults-file=/etc/my1.cnf --user=mysql --datadir=/opt/mysql2/data --pid-file=/opt/mysql2/data/gonu.pid
mysql 6841 5543 0 15:19 pts/2 00:00:00 /usr/sbin/mysqld-max --defaults-file=/etc/my2.cnf --user=mysql --datadir=/opt/mysql3/data --pid-file=/opt/mysql3/data/gonu.pid
root 6890 6168 0 16:19 pts/0 00:00:00 grep mysqld

ex)

[root@gonu data]# netstat -nap |grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 6715/mysqld-max
[root@gonu data]# netstat -nap |grep 3307
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 6816/mysqld-max
[root@gonu data]# netstat -nap |grep 3308
tcp 0 0 0.0.0.0:3308 0.0.0.0:* LISTEN 6841/mysqld-max
[root@gonu data]#


ex)

[root@gonu opt]# ls mysql/data/
CharDev gonu-bin.003 gonu-bin.007 gonu-bin.011 gonu-bin.015 gonu-bin.019 gonu-bin.index mysql
char.sql gonu-bin.004 gonu-bin.008 gonu-bin.012 gonu-bin.016 gonu-bin.020 gonu.err mysql-old
gonu-bin.001 gonu-bin.005 gonu-bin.009 gonu-bin.013 gonu-bin.017 gonu-bin.021 gonu.pid test
gonu-bin.002 gonu-bin.006 gonu-bin.010 gonu-bin.014 gonu-bin.018 gonu-bin.022 master.info

[root@gonu opt]# ls mysql2/data/
CharDev-2 gonu-bin.001 gonu-bin.003 gonu-bin.005 gonu-bin.007 gonu-bin.009 gonu-bin.011 gonu.err mysql test
char.sql gonu-bin.002 gonu-bin.004 gonu-bin.006 gonu-bin.008 gonu-bin.010 gonu-bin.index gonu.pid mysql-old

[root@gonu opt]# ls mysql3/data/
CharDev-3 gonu-bin.003 gonu-bin.007 gonu-bin.011 gonu-bin.015 gonu-bin.019 gonu-bin.023 gonu.pid test
char.sql gonu-bin.004 gonu-bin.008 gonu-bin.012 gonu-bin.016 gonu-bin.020 gonu-bin.024 master.info
gonu-bin.001 gonu-bin.005 gonu-bin.009 gonu-bin.013 gonu-bin.017 gonu-bin.021 gonu-bin.index mysql
gonu-bin.002 gonu-bin.006 gonu-bin.010 gonu-bin.014 gonu-bin.018 gonu-bin.022 gonu.err mysql-old


ex) 각 DB의 차별을 위하여 &mysql/data/ 의 CharDev를 CharDev-1(mysql2), CharDev-2(mysql3)으로 변경.

[root@gonu opt]# mysql -uroot -p --socket=/tmp/mysql2.sock --port=3307
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4 to server version: 3.23.57-Max-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> show databases;
+-----------+
| Database |
+-----------+
| CharDev-3 |
| mysql |
| mysql-old |
| test |
+-----------+
4 rows in set (0.00 sec)

mysql> exit
Bye
[root@gonu opt]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5 to server version: 3.23.57-Max-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> show databases;
+-----------+
| Database |
+-----------+
| CharDev |
| mysql |
| mysql-old |
| test |
+-----------+
4 rows in set (0.00 sec)

mysql> exit
Bye

[root@gonu opt]# mysql -uroot -p --socket=/tmp/mysql1.sock --port=3308
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 3.23.57-Max-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> show databases;
+-----------+
| Database |
+-----------+
| CharDev-2 |
| mysql |
| mysql-old |
| test |
+-----------+
4 rows in set (0.00 sec)

※ 무거운 query를 날리지 않고 메모리만 빵빵하다면 몇개라도 띄울수 있습니다.
물론 mysql server에 접속하실때는 port나 socket을 제대로 지정하지 않으면 엄한 DB의 data를 건드릴수 있으니
주의 하셔야 합니다.