mysql学习-- mysql安装--多实例安装

一、简介

多实例就是在一台服务器上开启多个不同的mysql服务端口,运行多个mysql服务进程。这些进程通过不同的socket监听不同的服务端口,以此来提供不同的服务。

多个实例公用一套安装程序,但有各自的配置文件my.conf 、数据文件、进程及日志文件等
在这里插入图片描述

上图很形象的描述了多实例,一台服务器部署多个mysql数据库,多个mysql数据库共享系统以及硬件资源等。

多实例的优缺点:
有点: 有效的利用服务器资源
当单个服务器资源有剩余时,可以利用多实例来充分利用服务器的资源来提供更多的服务
缺点:会出现资源互相抢占的现象
当某个实例并发量很高,或者有慢查询时,会消耗服务器更多的cpu、内存、硬盘io等资源,这时就会影响其他实例提供的服务,访问质量下降

二、多实例部署:

通过配置多个配置文件及多个启动程序来实现多实例的方案

1、部署环境

单机版环境:(单机版安装如上节)

  #杀掉进程,避免冲突,删掉启动命令。
pkill mysqld
ps -ef|grep mysql
rm -f /etc/init.d/mysqld

2、创建多实例的数据目录及授权

mkdir -p /data/{3306,3307}/data
chown -R mysql.mysql /data/
tree /data/
    /data/      #总的多实例根目录
├── 3306        #3306实例的目录
│   └── data    #3306实例的数据文件目录
└── 3307        #3307实例的目录
    └── data    #3307实例的数据文件目录
    4 directories, 0 files

3、多实例配置文件cp到相应的目录中

3.1 配置文件

 ## vim3306_my.conf
[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/mysql3306.err
pid-file=/data/3306/mysqld.pid
vim 3306_mysql
 #!/bin/sh
#Nick Suo
#email:630571017@qq.com
#blog:http://www.cnblogs.com/suoning


#init
port=3306
mysql_user="root"
mysql_pwd="suoning"
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
vim 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
#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/mysql3307.err
pid-file=/data/3307/mysqld.pid
vim 3307_mysqld
#!/bin/sh
#Nick Suo
#email:630571017@qq.com
#blog:http://www.cnblogs.com/suoning

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

3.2上传配置文件到相应目录并授权

cp 3306_my.cnf /data/3306/my.cnf
cp 3307_my.cnf /data/3307/my.cnf

拷贝启动文件,加执行权限:

cp 3306_mysql /data/3306/mysql
cp 3307_mysql /data/3307/mysql
chown -R mysql:mysql /data/
chmod +x /data/3306/mysql
chmod +x /data/330/mysql
##也可以使用 
find /data/ -type f -name "mysql" |xargs ls -l
find /data/ -type f -name "mysql" |xargs chmod +x 

配置结果::在这里插入图片描述

4、初始化、启动、登录

#多实例启动文件的启动mysql服务实质:
mysqld_safe --defaults-file=/data/3306/my.cnf 2>&1 > /dev/null &
mysqld_safe --defaults-file=/data/3307/my.cnf 2>&1 > /dev/null &
多实例启动文件的停止mysql服务实质:
mysqladmin -u root -poldsuo -S /data/3306/mysql.sock shutdown
mysqladmin -u root -poldsuo -S /data/3307/mysql.sock shutdown

4.1 初始化

cd /application/mysql/scripts/
./mysql_install_db --basedir=/application/mysql/ --datadir=/data/3306/data --user=mysql
./mysql_install_db --basedir=/application/mysql/ --datadir=/data/3307/data --user=mysql

4.2 启动数据库

 #启动mysql,并检查端口
/data/3306/mysql start
/data/3307/mysql start
netstat -lntup|grep 330
    tcp        0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      27896/mysqld        
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      27174/mysqld

4.3多实例登录数据库

多实例登录 :指定sock登录
mysql -root -S /data/3306/mysql.sock -p
mysql -root -S /data/3307/mysql.sock -p

4.4多实例更改密码

多实例 更改密码:
mysqladmin -u root -S /data/3306/mysql.sock password ‘ 新密码’

mysqladmin -u root -S /data/3307/mysql.sock password ‘新密码’

4.5 多实例关闭数据库时

在这里插入图片描述
mysql 脚本中有账号,更改后的密码写入脚本中在这里插入图片描述
修改后就可以关闭,为防止别人修改密码 ,太危险,, 降低脚本权限

find /data/ -type f -name "mysql"| xargs chmod 700
find /data/ -type f -name "mysql"| xargs chown  root:root
pkill mysqld

##关闭数据库使用
/data/3307/mysql stop
/data/3306/mysql stop

修改后如图:
在这里插入图片描述

4.6 mysql多实例 故障排错

a、如果发现没有显示mysql 端口 ,请稍微等几秒再看看,mysql服务启动有点慢
b、如果还不行,请查看错误日志,错误日志路径在my.cnf 配置最下面

root@mysql1 scripts]# grep log-error /data/3306/my.cnf 
                  #log-error = /data/3306/error.log
                  #log-error=/data/3306/mysql3306.err
        错误日志如下:
root@mysql1 scripts]# tail /data/3306/mysql3306.err
190620 22:33:12 InnoDB: 5.5.32 started; log sequence number 0
190620 22:33:12 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
190620 22:33:12 [Note]   - '0.0.0.0' resolves to '0.0.0.0';
190620 22:33:12 [Note] Server socket created on IP: '0.0.0.0'.
190620 22:33:12 [Warning] 'user' entry 'root@mysql1' ignored in --skip-name-resolve mode.
190620 22:33:12 [Warning] 'user' entry '@mysql1' ignored in --skip-name-resolve mode.
190620 22:33:12 [Warning] 'proxies_priv' entry '@ root@mysql1' ignored in --skip-name-resolve mode.
190620 22:33:12 [Note] Event Scheduler: Loaded 0 events
190620 22:33:12 [Note] /application/mysql-5.5.32/bin/mysqld: ready for connections.
Version: '5.5.32-log'  socket: '/data/3306/mysql.sock'  port: 3306  Source distribution

c、细看所有执行命令返回的屏幕输出,不要忽略关键的输出内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值