码迷,mamicode.com
首页 > 数据库 > 详细

MySQL多实例学习笔记

时间:2016-08-06 14:34:13      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:mysql多实例 主从复制   数据库基础操作

MySQL多实例

简单的说就是在一台机器上开启多个不同的服务端口(如3306,3307等)运行多个MySQL服务进程,这些服务进程通过不同个socket监听不同的服务端口来提供各自的服务


多实例的作用与问题

1、有效利用服务器

2、节约服务器资源

3、资源互相抢占问题



多实例应用场景

1、资金紧张型公司

2、并发访问不是特别大的业务

3、门户网站应用MySQL多实例场景


1、创建MySQL多实例的数据文件目录

先关掉我们之前启动的单实例

[root@centos03 ~]# /etc/init.d/mysqld stop

[root@centos03 ~]# mv /etc/init.d/mysqld /etc/init.d/dan.mysqld


[root@centos03 ~]#  mkdir -p /data/{3306,3307}/data

[root@centos03 ~]# tree /data         

/data

|-- 3306

|   `-- data

`-- 3307

    `-- data


4 directories, 0 files


多实例的是在我们之前上面文章操作单实例的基础上进行的,只是在单实例进行到

ln -s /application/mysql-5.5.32/ /application/mysql  这一步时候就不再往下面操作了,这个不清楚的话可以参考上篇文章安装,这里就不再重复操作了


2、创建MySQL多实例的配置文件

[root@centos03 ~]# vim /data/3306/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
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_martin3306.err
pid-file=/data/3306/mysqld.pid
vim /data/3307/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
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_martin3307.err
pid-file=/data/3307/mysqld.pid


3、创建多实例的启动脚本

[root@centos03 ~]# vim /data/3306/mysql
#!/bin/sh


#init
port=3306
mysql_user="root"
mysql_pwd="123456"
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





[root@centos03 ~]# vim /data/3307/mysql
#!/bin/sh

#init
port=3307
mysql_user="root"
mysql_pwd="123456"
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



[root@centos03 ~]# tree /data
/data
|-- 3306
|   |-- data
|   |-- my.cnf
|   `-- mysql
`-- 3307
    |-- data
    |-- my.cnf
    `-- mysql

4 directories, 4 files


[root@centos03 ~]# chown -R mysql.mysql /data


[root@centos03 ~]# find /data -type f -name "mysql"|xargs ls -l
-rw-r--r--. 1 mysql mysql 1018 Jun 14 01:04 /data/3306/mysql
-rw-r--r--. 1 mysql mysql 1017 Jun 14 01:01 /data/3307/mysql
[root@centos03 ~]# find /data -type f -name "mysql"|xargs chmod 700 
[root@centos03 ~]# find /data -type f -name "mysql"|xargs ls -l     
-rwx------. 1 mysql mysql 1018 Jun 14 01:04 /data/3306/mysql
-rwx------. 1 mysql mysql 1017 Jun 14 01:01 /data/3307/mysql


4、初始化数据库

[root@example mysql-5.5.32]# cd /application/mysql/scripts/
[root@example scripts]# ./mysql_install_db --basedir=/application/mysql/  --datadir=/data/3306/data/ --user=mysql

[root@example scripts]# ./mysql_install_db --basedir=/application/mysql/  --datadir=/data/3307/data/ --user=mysql


5、启动数据库

[root@centos03 scripts]# /data/3306/mysql start
Starting MySQL...
[root@centos03 scripts]# /data/3307/mysql start 
Starting MySQL...

[root@centos03 scripts]# ss -lantup|grep 330
tcp    LISTEN     0      128                    *:3306                  *:*      users:(("mysqld",20906,12))
tcp    LISTEN     0      128                    *:3307                  *:*      users:(("mysqld",21624,11))


6、登录数据库测试

[root@centos03 scripts]# mysql -S /data/3306/mysql.sock 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.32-log Source distribution

Copyright (c) 2000, 2013, 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> 

[root@centos03 scripts]# mysql -S /data/3307/mysql.sock  
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.32 Source distribution

Copyright (c) 2000, 2013, 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.


技术分享


7、两个数据库都成功了,大家可以创建一个实例3308,这里就不再操作了,简单看下最后的效果

技术分享

本文出自 “厚德载物” 博客,谢绝转载!

MySQL多实例学习笔记

标签:mysql多实例 主从复制   数据库基础操作

原文地址:http://huaxin.blog.51cto.com/903026/1834977

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!