Mariadb单机多实例部署

2022-10-27 14:49:32 浏览数 (1)

安装mariadb

代码语言:javascript复制
yum install mariadb mariadb-server mariadb-devel

创建多实例目录

创建多实例配置文件

vim /etc/my_10306.cnf

代码语言:javascript复制
[mysqld] 
user=mysql 
port=10306 
datadir=/data/10306/ 
socket=/data/10306/10306.sock

vim /etc/my_10307.cnf

代码语言:javascript复制
[mysqld] 
user=mysql 
port=10307 
datadir=/data/10307/ 
socket=/data/10307/10307.sock

生成数据文件

代码语言:javascript复制
mysql_install_db --basedir=/usr --datadir=/data/10306/ --user=mysql
mysql_install_db --basedir=/usr --datadir=/data/10307/ --user=mysql

创建启动脚本

代码语言:javascript复制
#!/bin/sh
 
basedir=
datadir=/data/10306            # 修改实例的数据目录
conf=/etc/my_10306.cnf         # 修改实例的配置文件
service_startup_timeout=900
 
lockdir='/var/lock/subsys'
lock_file_path="$lockdir/mysql"
 
mysqld_pid_file_path=
if test -z "$basedir"
then
  basedir=/usr
  bindir=/usr/bin
  if test -z "$datadir"
  then
    datadir=/var/lib/mysql
  fi
  sbindir=/usr/sbin
  libexecdir=/usr/sbin
else
  bindir="$basedir/bin"
  if test -z "$datadir"
  then
    datadir="$basedir/data"
  fi
  sbindir="$basedir/sbin"
  libexecdir="$basedir/libexec"
fi
 
datadir_set=
 
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 "testingc"`,`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 test -n "$pid"; then
      if kill -0 "$pid" 2>/dev/null; then
        :  # the server still runs
      else
        if test -n "$avoid_race_condition"; then
          avoid_race_condition=""
          continue  # Check again.
        fi
 
        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
}
 
if test -x ./bin/my_print_defaults
then
  print_defaults="./bin/my_print_defaults"
elif test -x $bindir/my_print_defaults
then
  print_defaults="$bindir/my_print_defaults"
elif test -x $bindir/mysql_print_defaults
then
  print_defaults="$bindir/mysql_print_defaults"
else
  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
      if test -x "$d/bin/mysql_print_defaults"
      then
        print_defaults="$d/bin/mysql_print_defaults"
        break
      fi
    done
  fi
 
  test -z "$print_defaults" && print_defaults="my_print_defaults"
fi
 
extra_args=""
if test -r "$basedir/my.cnf"
then
  extra_args="-e $basedir/my.cnf"
else
  if test -r "$datadir/my.cnf"
  then
    extra_args="-e $datadir/my.cnf"
  fi
fi
 
parse_server_arguments `$print_defaults $extra_args --defaults-file="$conf" mysqld server mysql_server mysql.server`
 
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')
 
    cd $basedir
 
    echo $echo_n "Starting MySQL (Percona Server)"
    if test -x $bindir/mysqld_safe
    then
      $bindir/mysqld_safe --defaults-file="$conf" --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>&1 &
      wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$?
 
      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')
 
    if test -s "$mysqld_pid_file_path"
    then
      mysqld_pid=`cat "$mysqld_pid_file_path"`
 
      if (kill -0 $mysqld_pid 2>/dev/null)
      then
        echo $echo_n "Shutting down MySQL (Percona Server)"
        kill $mysqld_pid
        wait_for_pid removed "$mysqld_pid" "$mysqld_pid_file_path"; return_value=$?
      else
        log_failure_msg "MySQL (Percona Server) server process #$mysqld_pid is not running!"
        rm "$mysqld_pid_file_path"
      fi
 
      if test -f "$lock_file_path"
      then
        rm -f "$lock_file_path"
      fi
      exit $return_value
    else
      log_failure_msg "MySQL (Percona Server) PID file could not be found!"
    fi
    ;;
 
  'restart')
    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 (Percona Server)"
      touch "$mysqld_pid_file_path"
    else
      log_failure_msg "MySQL (Percona Server) PID file could not be found!"
      exit 1
    fi
    ;;
  'status')
    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 (Percona Server) running ($mysqld_pid)"
        exit 0
      else
        log_failure_msg "MySQL (Percona Server) is not running, but PID file exists"
        exit 1
      fi
    else
      mysqld_pid=`pidof $libexecdir/mysqld`
 
      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 (Percona Server) is not running, but lock file ($lock_file_path) exists"
          exit 2
        fi 
        log_failure_msg "MySQL (Percona Server) is not running"
        exit 3
      else
        log_failure_msg "MySQL (Percona Server) is running but PID file could not be found"
        exit 4
      fi
    fi
    ;;
    *)
      basename=`basename "$0"`
      echo "Usage: $basename  {start|stop|restart|reload|force-reload|status}  [ MySQL (Percona Server) options ]"
      exit 1
    ;;
esac
exit

修改权限并启动服务

代码语言:javascript复制
chmod  x /etc/init.d/mysql_10306
chmod  x /etc/init.d/mysql_10307
/etc/init.d/mysql_10306 start
/etc/init.d/mysql_10307 start

查看状态

进入命令行,配置root密码

0 人点赞