Mac下MySQL单机多实例部署过程记录(超详细)

虽然是Mac版,Windows配置亦是同理

单机多实例部署网上一般有两种:

1、一个配置文件管理多个MySQL实例

2、每个实例都有自己的配置文件(my.cnf)。

部署情况:

官方的文档是第一种的部署形式,详细见官方单机多实例部署文档,里面给出了一个配置文件的例子。

大致就是只需要在my.cnf的配置文件中配置每一个mysql实例的数据库文件目录、端口、socket、用户等,以下信息来自于MySQL官方文档

# This is an example of a my.cnf file for mysqld_multi.
# Usually this file is located in home dir ~/.my.cnf or /etc/my.cnf

[mysqld_multi]
mysqld     = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
user       = multi_admin
password   = my_password

[mysqld2]
socket     = /tmp/mysql.sock2
port       = 3307
pid-file   = /usr/local/mysql/data2/hostname.pid2
datadir    = /usr/local/mysql/data2
language   = /usr/local/mysql/share/mysql/english
user       = unix_user1

[mysqld3]
mysqld     = /path/to/mysqld_safe
ledir      = /path/to/mysqld-binary/
mysqladmin = /path/to/mysqladmin
socket     = /tmp/mysql.sock3
port       = 3308
pid-file   = /usr/local/mysql/data3/hostname.pid3
datadir    = /usr/local/mysql/data3
language   = /usr/local/mysql/share/mysql/swedish
user       = unix_user2

[mysqld4]
socket     = /tmp/mysql.sock4
port       = 3309
pid-file   = /usr/local/mysql/data4/hostname.pid4
datadir    = /usr/local/mysql/data4
language   = /usr/local/mysql/share/mysql/estonia
user       = unix_user3

[mysqld6]
socket     = /tmp/mysql.sock6
port       = 3311
pid-file   = /usr/local/mysql/data6/hostname.pid6
datadir    = /usr/local/mysql/data6
language   = /usr/local/mysql/share/mysql/japanese
user       = unix_user4

并且在运行MySQL实例之前如何初始化每一个实例的数据库文件件以及root用户的密码,详细地址见初始化MySQLdata文件夹

大致为

1、创建MySQL实例的data文件夹,并且给该文件夹读、写、执行的不同权限

2、开始初始化数据库,并且MySQL会生成一个随机密码给你

问题

按照官方文档操作一步一步下来,应该不会出错。到最后一步,启动多实例的时候,一直在报实例的文件夹不可写(not writable),判断是没有给权限,但是我将权限放到最大,chmod -R 777 实例的文件夹路径,还是报不可写,没有写的权限,网上找了很久的答案,官网的倒是有人提出过这个问题,但是没人回答。最终我不得不放弃了第一种

方案。如果哪位同学知道,可以留言告诉下,多谢。我后来判断会不会是用户组的问题,但是我给的权限已经是777了

第二种方案就很简单了,举例说明:源实例-mysql3306,目标实例-mysql3309

部署情况如下:

准备工作:将现有的mysql3306实例的配置文件做少许的改动,

my.cnf配置文件改动如下:

[client]
port=3306
socket=/tmp/mysql.sock1
password=12345

[mysqld]
port=3306
socket=/tmp/mysql.sock1
key_buffer_size=16M
max_allowed_packet=8M
datadir=/usr/local/mysql3306/data
basedir=/usr/local/mysql3306
pid-file = /usr/local/mysql3306/data/3306.pid

[mysql]
no-auto-rehash
connect_timeout=2

[mysqlhotcopy]
interactive-timeout


[mysqldump]
quick

主要改动为:

1.在client和mysqld下都加入了socket选项并指定了其位置,因为,多个MySQL实例所用的sock不同,所以要配置不同的socket

2.在mysqld下配置datadir、basedir、pid-file的路径,不同的MySQL实例这三个参数一定是不同的

 mysql启动文件mysql.server(位置在${MySQL的安装路径}/support-file/下)改动如下:

在Set some defaults这一行下面有几个XXX路径(basedir、bindir、sbindir、libexecdir)这几个

配置成上面的配置文件对应的路径即可,如有疑问,可参考下面的配置文件

#!/bin/sh
# Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
# This file is public domain and comes with NO WARRANTY of any kind

# MySQL daemon start/stop script.

# Usually this is put in /etc/init.d (at least on machines SYSV R4 based
# systems) and linked to /etc/rc3.d/S99mysql and /etc/rc0.d/K01mysql.
# When this is done the mysql server will be started when the machine is
# started and shut down when the systems goes down.

# Comments to support chkconfig on RedHat Linux
# chkconfig: 2345 64 36
# description: A very fast and reliable SQL database engine.

# Comments to support LSB init script conventions
### BEGIN INIT INFO
# Provides: mysql
# Required-Start: $local_fs $network $remote_fs
# Should-Start: ypbind nscd ldap ntpd xntpd
# Required-Stop: $local_fs $network $remote_fs
# Default-Start:  2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop MySQL
# Description: MySQL is a very fast and reliable SQL database engine.
### END INIT INFO
 
# If you install MySQL on some other places than /usr/local/mysql, then you
# have to do one of the following things for this script to work:
#
# - Run this script from within the MySQL installation directory
# - Create a /etc/my.cnf file with the following information:
#   [mysqld]
#   basedir=<path-to-mysql-installation-directory>
# - Add the above to any other configuration file (for example ~/.my.ini)
#   and copy my_print_defaults to /usr/bin
# - Add the path to the mysql-installation-directory to the basedir variable
#   below.
#
# If you want to affect other MySQL variables, you should make your changes
# in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.

# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.

basedir=
datadir=

# Default value, in seconds, afterwhich the script should timeout waiting
# for server start. 
# Value here is overriden by value in my.cnf. 
# 0 means don't wait at all
# Negative numbers mean to wait indefinitely
service_startup_timeout=900

# Lock directory for RedHat / SuSE.
lockdir='/var/lock/subsys'
lock_file_path="$lockdir/mysql"

# The following variables are only set for letting mysql.server find things.

# Set some defaults
mysqld_pid_file_path=
if test -z "$basedir"
then
  basedir=/usr/local/mysql3306
  bindir=/usr/local/mysql3306/bin
  if test -z "$datadir"
  then
    datadir=/usr/local/mysql3306/data
  fi
  sbindir=/usr/local/mysql3306/bin
  libexecdir=/usr/local/mysql3306/bin
else
  bindir="$basedir/bin"
  if test -z "$datadir"
  then
    datadir="$basedir/data"
  fi
  sbindir="$basedir/sbin"
  libexecdir="$basedir/libexec"
fi

# datadir_set is used to determine if datadir was set (and so should be
# *not* set inside of the --basedir= handler.)
datadir_set=

#
# Use LSB init script functions for printing messages, if possible
#
lsb_functions="/lib/lsb/init-functions"
if test -f $lsb_functions ; then
  . $lsb_functions
else
  log_success_msg()
  {
    echo " SUCCESS! $@"
  }
  log_failure_msg()
  {
    echo " ERROR! $@"
  }
fi

PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin"
export PATH

mode=$1    # start or stop

[ $# -ge 1 ] && shift


other_args="$*"   # uncommon, but needed when called from an RPM upgrade action
           # Expected: "--skip-networking --skip-grant-tables"
           # They are not checked here, intentionally, as it is the resposibility
           # of the "spec" file author to give correct arguments only.

case `echo "testing\c"`,`echo -n testing` in
    *c*,-n*) echo_n=   echo_c=     ;;
    *c*,*)   echo_n=-n echo_c=     ;;
    *)       echo_n=   echo_c='\c' ;;
esac

parse_server_arguments() {
  for arg do
    case "$arg" in
      --basedir=*)  basedir=`echo "$arg" | sed -e 's/^[^=]*=//'`
                    bindir="$basedir/bin"
		    if test -z "$datadir_set"; then
		      datadir="$basedir/data"
		    fi
		    sbindir="$basedir/sbin"
		    libexecdir="$basedir/libexec"
        ;;
      --datadir=*)  datadir=`echo "$arg" | sed -e 's/^[^=]*=//'`
		    datadir_set=1
	;;
      --pid-file=*) mysqld_pid_file_path=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
      --service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
    esac
  done
}

wait_for_pid () {
  verb="$1"           # created | removed
  pid="$2"            # process ID of the program operating on the pid-file
  pid_file_path="$3" # path to the PID file.

  i=0
  avoid_race_condition="by checking again"

  while test $i -ne $service_startup_timeout ; do

    case "$verb" in
      'created')
        # wait for a PID-file to pop into existence.
        test -s "$pid_file_path" && i='' && break
        ;;
      'removed')
        # wait for this PID-file to disappear
        test ! -s "$pid_file_path" && i='' && break
        ;;
      *)
        echo "wait_for_pid () usage: wait_for_pid created|removed pid pid_file_path"
        exit 1
        ;;
    esac

    # if server isn't running, then pid-file will never be updated
    if test -n "$pid"; then
      if kill -0 "$pid" 2>/dev/null; then
        :  # the server still runs
      else
        # The server may have exited between the last pid-file check and now.  
        if test -n "$avoid_race_condition"; then
          avoid_race_condition=""
          continue  # Check again.
        fi

        # there's nothing that will affect the file.
        log_failure_msg "The server quit without updating PID file ($pid_file_path)."
        return 1  # not waiting any more.
      fi
    fi

    echo $echo_n ".$echo_c"
    i=`expr $i + 1`
    sleep 1

  done

  if test -z "$i" ; then
    log_success_msg
    return 0
  else
    log_failure_msg
    return 1
  fi
}

# Get arguments from the my.cnf file,
# the only group, which is read from now on is [mysqld]
if test -x "$bindir/my_print_defaults";  then
  print_defaults="$bindir/my_print_defaults"
else
  # Try to find basedir in /etc/my.cnf
  conf=/etc/my.cnf
  print_defaults=
  if test -r $conf
  then
    subpat='^[^=]*basedir[^=]*=\(.*\)$'
    dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf`
    for d in $dirs
    do
      d=`echo $d | sed -e 's/[ 	]//g'`
      if test -x "$d/bin/my_print_defaults"
      then
        print_defaults="$d/bin/my_print_defaults"
        break
      fi
    done
  fi

  # Hope it's in the PATH ... but I doubt it
  test -z "$print_defaults" && print_defaults="my_print_defaults"
fi

#
# Read defaults file from 'basedir'.   If there is no defaults file there
# check if it's in the old (depricated) place (datadir) and read it from there
#

extra_args=""
if test -r "$basedir/my.cnf"
then
  extra_args="-e $basedir/my.cnf"
fi

parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server`

#
# Set pid file if not given
#
if test -z "$mysqld_pid_file_path"
then
  mysqld_pid_file_path=$datadir/`hostname`.pid
else
  case "$mysqld_pid_file_path" in
    /* ) ;;
    * )  mysqld_pid_file_path="$datadir/$mysqld_pid_file_path" ;;
  esac
fi

case "$mode" in
  'start')
    # Start daemon

    # Safeguard (relative paths, core dumps..)
    cd $basedir

    echo $echo_n "Starting MySQL"
    if test -x $bindir/mysqld_safe
    then
      # Give extra arguments to mysqld with the my.cnf file. This script
      # may be overwritten at next upgrade.
      $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &
      wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$?

      # Make lock for RedHat / SuSE
      if test -w "$lockdir"
      then
        touch "$lock_file_path"
      fi

      exit $return_value
    else
      log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)"
    fi
    ;;

  'stop')
    # Stop daemon. We use a signal here to avoid having to know the
    # root password.

    if test -s "$mysqld_pid_file_path"
    then
      # signal mysqld_safe that it needs to stop
      touch "$mysqld_pid_file_path.shutdown"

      mysqld_pid=`cat "$mysqld_pid_file_path"`

      if (kill -0 $mysqld_pid 2>/dev/null)
      then
        echo $echo_n "Shutting down MySQL"
        kill $mysqld_pid
        # mysqld should remove the pid file when it exits, so wait for it.
        wait_for_pid removed "$mysqld_pid" "$mysqld_pid_file_path"; return_value=$?
      else
        log_failure_msg "MySQL server process #$mysqld_pid is not running!"
        rm "$mysqld_pid_file_path"
      fi

      # Delete lock for RedHat / SuSE
      if test -f "$lock_file_path"
      then
        rm -f "$lock_file_path"
      fi
      exit $return_value
    else
      log_failure_msg "MySQL server PID file could not be found!"
    fi
    ;;

  'restart')
    # Stop the service and regardless of whether it was
    # running or not, start it again.
    if $0 stop  $other_args; then
      $0 start $other_args
    else
      log_failure_msg "Failed to stop running server, so refusing to try to start."
      exit 1
    fi
    ;;

  'reload'|'force-reload')
    if test -s "$mysqld_pid_file_path" ; then
      read mysqld_pid <  "$mysqld_pid_file_path"
      kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL"
      touch "$mysqld_pid_file_path"
    else
      log_failure_msg "MySQL PID file could not be found!"
      exit 1
    fi
    ;;
  'status')
    # First, check to see if pid file exists
    if test -s "$mysqld_pid_file_path" ; then 
      read mysqld_pid < "$mysqld_pid_file_path"
      if kill -0 $mysqld_pid 2>/dev/null ; then 
        log_success_msg "MySQL running ($mysqld_pid)"
        exit 0
      else
        log_failure_msg "MySQL is not running, but PID file exists"
        exit 1
      fi
    else
      # Try to find appropriate mysqld process
      mysqld_pid=`pgrep -d' ' -f $libexecdir/mysqld`

      # test if multiple pids exist
      pid_count=`echo $mysqld_pid | wc -w`
      if test $pid_count -gt 1 ; then
        log_failure_msg "Multiple MySQL running but PID file could not be found ($mysqld_pid)"
        exit 5
      elif test -z $mysqld_pid ; then 
        if test -f "$lock_file_path" ; then 
          log_failure_msg "MySQL is not running, but lock file ($lock_file_path) exists"
          exit 2
        fi 
        log_failure_msg "MySQL is not running"
        exit 3
      else
        log_failure_msg "MySQL is running but PID file could not be found"
        exit 4
      fi
    fi
    ;;
    *)
      # usage
      basename=`basename "$0"`
      echo "Usage: $basename  {start|stop|restart|reload|force-reload|status}  [ MySQL server options ]"
      exit 1
    ;;
esac

exit 0

 

接下来开始复制3306实例:

1、新建一个MySQL实例的主文件夹(就叫mysql3309)

2、将当前的MySQL实例文件夹通过cp -pr命令复制给新的MySQL实例(mysql3309)中,如下:

sudo cp -pr mysql3306 mysql3309

 其中参数p是将源文件(3306的文件夹)的访问权限也复制过来,r是复制3306实例的所有子目录和文件

 

如下图

3、复制完成之后可以删除data文件件重新init一个新的data文件夹(init步骤在第一种方案中的第二步),并获取该实例新的root用户以及密码。

具体命令如下:

sudo rm -rf data
sudo mkdir data

接下来初始化data文件夹,

cd /usr/local/mysql3309
bin/mysqld --defaults-file=/usr/local/mysql3309/my.cnf --initialize --user=root

 执行命令的时候可能会提示data文件夹不可写入,给予权限即可

sudo chmod 777 data

重新执行上面的初始化命令即可初始化成功,成功之后的提示信息如下:

2020-02-16T08:57:49.367713Z 0 [System] [MY-013169] [Server] /usr/local/mysql3309/bin/mysqld (mysqld 8.0.18) initializing of server in progress as process 18043
2020-02-16T08:57:49.370075Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /usr/local/mysql3309/data/ is case insensitive
2020-02-16T08:57:49.370560Z 0 [Warning] [MY-010122] [Server] One can only use the --user switch if running as root
2020-02-16T08:57:50.926640Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: x1yDK_kTl%%5

 最下面有一个登陆新的3309实例的临时密码,要记住。

4、修改新的MySQL实例的my.cnf配置文件,主要是修改端口号以及其他信息,实例如下

[client]
port=3309
socket=/tmp/mysql.sock2
password=12345

[mysqld]
port=3309
socket=/tmp/mysql.sock2
key_buffer_size=16M
max_allowed_packet=8M
datadir=/usr/local/mysql3309/data
basedir=/usr/local/mysql3309
pid-file = /usr/local/mysql3309/data/3309.pid

[mysql]
no-auto-rehash
connect_timeout=2

[mysqlhotcopy]
interactive-timeout

[mysqldump]
quick

5、这样完成之后,如果你使用 sudo /usr/local/mysql3309/support-files/mysql.server start 命令启动不了该实例,这时候就要去修改一下/usr/local/mysql3309/support-files/mysql.server这个东东,上面的准备工作已经提到过了,你可以直接把3306的复制文件copy过来,修改一下即可。

6.接下来就可以启动3309实例了,命令如下:

cd /usr/local/mysql3309
cd support-files
sudo mysql.server start

这时会出现启动不起来的情况,是因为data文件夹的子目录没有写入的权限,给予权限即可:

sudo chmod -R 777 /usr/local/mysql3309/data

再执行启动操作即可将3309实例启动起来。

启动之后可以/tmp文件下找到设置的socket

如下图

7.连接3309实例,这里要注意了,连接是要指定socket,就是你上面配置的,

mysql -uroot -p -S /tmp/mysql.sock2

 输入刚才的临时密码即可登录成功,然后修改密码(下面的代码来自于MySQL官方文档)

ALTER USER 'root'@'localhost' IDENTIFIED BY 'root-password';

 这里有一个小坑,当修改完密码之后,我们可以在命令行模式下登录MySQL3309实例,但是,如果在Navicat中进行连接会报出如下错误

Authentication plugin 'caching_sha2_password' cannot be loaded

这是由于MySQL8改了加密规则,我们可以将加密规则改回到MySQL8之前,连接上MySQL,执行如下三条命令

ALTER USER 'root'@'localhost' IDENTIFIED BY '12345' PASSWORD EXPIRE NEVER;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '12345';
FLUSH PRIVILEGES;

其中12345部分为你自己设置的密码,我这里设置成12345,改完之后,Navicat即可连接上了。

到此为止,在Mac上单机部署多个MySQL实例就完成了。

最后附详细过程图一张

如图

终极大招:如果上面的步骤你嫌弃太繁琐 

你可以尝试将当前的MySQL安装执行程序的路径改为你想要新的实例的路径,点击init让MySQL安装程序帮你初始化。如下图:

下图为MySQL8的配置台

 

这样的话,如果你想部署几个,你就循环上面的步骤即可。这样下来,单机多实例就部署完成了。启动的时候,按照启动单实例的命令进入每一个MySQL实例的文件夹执行命令即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值