NO.A.0005——mysql/mariadb-多实例

mysql/Mariadb多实例教程:

一、概述关系型数据库常见组件:
数据库:database
表:table
行:row
列:column
索引:index
视图:view
用户:user
权限privilege
存储过程:procedure,无返回值
存储函数:function,有返回值
触发器:trigger
时间调度器:event scheduler,任务计划

二、创建多实例:
1、设计规划;环境检查:
在一个服务器上跑多个实例的话需要用到多个端口号;我们以端口号定义实例的名称。以yum安装环境实现多实例。

2、环境说明:
centos版本:7.6.1810
mariadb版本:10.2.35

3、多实例规划:(以端口号来区分)

mysql-3306
mysql-3307
mysql-3308

4、实现多实例的官方工具:/usr/bin/mysqld_multi:
可以配置多实例,缺点是只能针对同一个版本实现多实例;依赖于当前包的版本。

三、创建多实例:
1、部署mariadb 10.2.35版本环境的多实例:安装本机mariadb、准备各自的目录创建用户,生成各自的数据库文件

//1、安装mariadb 10.2.35,安装完已经生成一个实例;
[[email protected] ~]# yum install mariadb-server -y         //安装本机的mariadb数据库程序
[[email protected] ~]# rpm -ql mariadb-server |grep multi   //多实例工具
/usr/bin/mysqld_multi
/usr/share/man/man1/mysqld_multi.1.gz

//2、规划目录:创建多实例文件夹,并把它所需要的文件夹规划在当前目录下:
[[email protected] ~]# mkdir /mysqldb/{3306,3307,3308}/{etc,socket,pid,log,data,bin} -pv
[[email protected] ~]# tree /mysqldb/
/mysqldb/
├── 3306                    //3306实例文件夹
│   ├── data                //数据库数据存放目录
│   ├── etc                 //配置文件存放目录
│   ├── log                 //日志文件存放目录
│   ├── pid                 //pid文件存放目录
│   └── socket              //socket文件存放目录
    └── bin                 //启动脚本存放目录
├── 3307                    //3307实例文件夹
│   ├── data
│   ├── etc
│   ├── log
│   ├── pid
│   └── socket
    └── bin
└── 3308                    //3308实例文件夹
    ├── data
    ├── etc
    ├── log
    ├── pid
    └── socket
    └── bin

//3、创建mysql用户:    
[[email protected] ~]#  useradd -r -s /bin/nologin -m -d /var/lib/mysql  mysql  //创建mysql用户
[[email protected] ~]# getent passwd mysql
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
[[email protected] ~]# chown -R mysql.mysql /mysqldb/    //给mysqldb文件夹授予mysql用户的所有权限

//4、生成多实例的数据库(生成三个数据库文件,各自放在各自的文件夹里)
MariaDB [(none)]> show variables like '%basedir%';   //在本机mysql数据库中查找变量basedir路径:
| basedir       | /usr  |           /basedir路径默认指定到/usr下

[[email protected] ~]# mysql_install_db --datadir=/mysqldb/3306/data --user=mysql  --basedir=/usr
    //生成数据库;  --datadir指定生成的目录地址;--basedir指定basedir路径,需要在mysql数据库中查找;
[[email protected] ~]# mysql_install_db --datadir=/mysqldb/3307/data --user=mysql  --basedir=/usr/
[[email protected] ~]# mysql_install_db --datadir=/mysqldb/3308/data --user=mysql  --basedir=/usr/

2、准备配置文件

//5、把本机的配置文件,cp到各自配置文件目录之下:
[[email protected] ~]# cp /etc/my.cnf   /mysqldb/3306/etc/
[[email protected] ~]# cp /etc/my.cnf   /mysqldb/3307/etc/
[[email protected] ~]# cp /etc/my.cnf   /mysqldb/3308/etc/
[[email protected] ~]# vim /mysqldb/3306/etc/my.cnf
port=3306                                           //端口号:3306可以不加端口号,其它的必须加
datadir=/mysqldb/3306/data                          //数据库文件地址
socket=/mysqldb/3306/socket/mysql.sock              //mysql.sock文件地址
log-error=/mysqldb/3306/log/mariadb.log             //日志文件地址
pid-file=/mysqldb/3306/pid/mariadb.pid              //pid文件地址
# !includedir /etc/my.cnf.d                         //注销、重新配置地址
[[email protected] ~]# vim /mysqldb/3306/etc/my.cnf
port=3307                                           //端口号
datadir=/mysqldb/3307/data                          //数据库文件地址
socket=/mysqldb/3307/socket/mysql.sock              //mysql.sock文件地址
log-error=/mysqldb/3307/log/mariadb.log             //日志文件地址
pid-file=/mysqldb/3307/pid/mariadb.pid              //pid文件地址
# !includedir /etc/my.cnf.d                         //注销
[[email protected] ~]# vim /mysqldb/3308/etc/my.cnf
port=3308                                           //端口号
datadir=/mysqldb/3308/data                          //数据库文件地址
socket=/mysqldb/3308/socket/mysql.sock              //mysql.sock文件地址
log-error=/mysqldb/3308/log/mariadb.log             //日志文件地址
pid-file=/mysqldb/3308/pid/mariadb.pid              //pid文件地址
# !includedir /etc/my.cnf.d                         //注销

//vim下全部替换3306为3307(vim下批量替换字段)
 %s/3306/3307

3、配置启动脚本放到各自实例的bin目录下(启动脚本在单独准备):

//6、把服务脚本cp到指定目录下,并配置端口号及相应信息;
[[email protected] ~]# cp mysqld /mysqldb/3306/bin/
[[email protected] ~]# chmod 700 /mysqldb/3306/bin/mysqld.sh 
[[email protected] ~]# ll /mysqldb/3306/mysqld.sh  
                                    //给mysqld文件可执行权限,为了安全只有root账号才有权限
-rwx------ 1 root root 1002 Nov  7 21:17 /mysqldb/3306/mysqld

[[email protected] ~]# cp mysqld /mysqldb/3307/bin/
[[email protected] ~]# chmod 700 /mysqldb/3307/bin/mysqld.sh 
[[email protected] ~]# ll /mysqldb/3307/mysqld.sh 
-rwx------ 1 root root 1002 Nov  7 21:20 /mysqldb/3307/mysqld

[[email protected] ~]# cp mysqld /mysqldb/3308/bin/
[[email protected] ~]# chmod 700 /mysqldb/3308/bin/mysqld.sh
[[email protected] ~]# ll /mysqldb/3308/mysqld.sh 
-rwx------ 1 root root 1002 Nov  7 21:20 /mysqldb/3308/mysqld  

//执行启动脚本stop的时候,需要口令,默认脚本配置是有一个口令参数-p${mysql_pwd}:
                                            直接把该参数注销掉;就可以实现不需要口令直接启动
  ${cmd_path}/mysqladmin -u ${mysql_user}  -S ${mysql_sock} shutdown
# ${cmd_path}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown

多实例mysql启动脚本:

#!/bin/bash

port=3306
mysql_user="root"
mysql_pwd=""
cmd_path="/usr/bin"
mysql_basedir="/mysqldb"
mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock"

function_start_mysql()
{
    if [ ! -e "$mysql_sock" ];then
      printf "Starting MySQL...\n"
      ${cmd_path}/mysqld_safe --defaults-file=${mysql_basedir}/${port}/etc/my.cnf  
    &> /dev/null  &
    else
      printf "MySQL is running...\n"
      exit
    fi
}

function_stop_mysql()
{
    if [ ! -e "$mysql_sock" ];then
       printf "MySQL is stopped...\n"
       exit
    else
       printf "Stoping MySQL...\n"
       ${cmd_path}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown
   fi
}

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: ${mysql_basedir}/${port}/bin/mysqld {start|stop|restart}\n"
esac

4、启动多实例程序并设置root账户口令:

//7、多实例启动配置,并设置root账户口令

//7.1、多实例3306启动配置,并设置root账户口令
[[email protected] ~]# /mysqldb/3306/bin/mysqld.sh start
 [[email protected] ~]# ss -tnl
 tcp    LISTEN     0      50      *:3306      *:*

[[email protected] ~]# mysql -S /mysqldb/3306/socket/mysql.sock //连接3306实例:
MariaDB [(none)]> show variables like '%port%';    //确定自己是连在了3306数据库程序上。
| port                                | 3306
[[email protected] ~]# /mysqldb/3306/bin/mysqld.sh stop
Stoping MySQL...
Enter password:     //需要输入口令   //可以直接更改启动脚本,去掉该参数-p${mysql_pwd}

//为3306程序创建root账户口令(设置root)口令后把启动脚本口令参数改回来)
MariaDB [(none)]> select user,host,password from mysql.user; //查询到3306当前是没有口令的
| root | localhost.localdomain |                             //空   |
MariaDB [(none)]> update mysql.user set password=password("123456") where user='root'; 
                                                            //为root账号创建密码
Query OK, 4 rows affected (0.00 sec)
MariaDB [(none)]> select user,host,password from mysql.user;
+------+-----------------------+-------------------------------------------+
| user | host                  | password                                  |
+------+-----------------------+-------------------------------------------+
| root | localhost             | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | localhost.localdomain | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |

//多实例程序3306带密码登录:
[[email protected] ~]# mysql -S /mysqldb/3306/socket/mysql.sock  -uroot -p123456

//7.2、多实例3307启动配置,并设置root账户口令
[[email protected] ~]# /mysqldb/3307/bin/mysqld.sh start
 [[email protected] ~]# ss -tnl
 tcp    LISTEN     0      50   *:3307                                   *:*

[[email protected] ~]# mysql -S /mysqldb/3307/socket/mysql.sock   //连接3307实例:
MariaDB [(none)]> show variables like '%port%';             //确定自己是连在了3307数据库程序上。
| port                                | 3307
[[email protected] ~]# /mysqldb/3307/bin/mysqld.sh stop
Stoping MySQL...
Enter password:      //需要输入口令 //可以直接更改启动脚本,去掉该参数-p${mysql_pwd}

//为3307程序创建root账户口令
MariaDB [(none)]> select user,host,password from mysql.user; //查询到3307当前是没有口令的
| root | localhost.localdomain |      空   |
MariaDB [(none)]> update mysql.user set password=password("123456") where user='root';  
                                                            //为root用户创建账号密码
Query OK, 4 rows affected (0.00 sec)
MariaDB [(none)]> select user,host,password from mysql.user;
+------+-----------------------+-------------------------------------------+
| user | host                  | password                                  |
+------+-----------------------+-------------------------------------------+
| root | localhost             | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | localhost.localdomain | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |

//多实例程序3307带密码登录:
[[email protected] ~]# mysql -S /mysqldb/3307/socket/mysql.sock  -uroot -p123456

//7.3、多实例3308启动配置,并设置root账户口令
[[email protected] ~]# /mysqldb/3308/bin/mysqld.sh start
 [[email protected] ~]# ss -tnl
 tcp    LISTEN     0      50   *:3307          *:*

[[email protected] ~]# mysql -S /mysqldb/3308/socket/mysql.sock    //连接3308实例:
MariaDB [(none)]> show variables like '%port%';     //确定自己是连在了3308数据库程序上。
| port                                | 3308
[[email protected] ~]# /mysqldb/3308/bin/mysqld.sh stop
Stoping MySQL...
Enter password:         //需要输入口令  //可以直接更改启动脚本,去掉该参数-p${mysql_pwd}

//为3308程序创建root账户口令
MariaDB [(none)]> select user,host,password from mysql.user;  //查询到3308当前是没有口令的
| root | localhost.localdomain |      空   |
MariaDB [(none)]> update mysql.user set password=password("123456") where user='root';  
                                                                //为root用户创建账号密码
Query OK, 4 rows affected (0.00 sec)
MariaDB [(none)]> select user,host,password from mysql.user;
+------+-----------------------+-------------------------------------------+
| user | host                  | password                                  |
+------+-----------------------+-------------------------------------------+
| root | localhost             | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | localhost.localdomain | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |

//多实例程序3308带密码登录:
[[email protected] ~]# mysql -S /mysqldb/3308/socket/mysql.sock  -uroot -p123456

5、配置mysqlrunaall.sh并设置开机自启动:

//8、编辑自启动脚本调用启动文件:

[[email protected] ~]# vim /mysqldb/mysqlrunall.sh
#!/bin/bash
# chkconfig: 2345 10 90
# description: 启动mysql     //这两行必须添加,没有这两行无法加入开机自启动。
/mysqldb/3306/bin/mysqld.sh   start 3306
/mysqldb/3307/bin/mysqld.sh   start 3307
/mysqldb/3308/bin/mysqld.sh   start 3308
[[email protected] ~]# chmod +x /mysqldb/mysqlrunall.sh 
[[email protected] ~]# /mysqldb/mysqlrunall.sh start
[[email protected] ~]# /mysqldb/mysqlrunall.sh  stop     //停止的时候输入密码即可

//此时你可以带密码登录数据库中了
[[email protected] ~]# mysql -S /mysqldb/3306/socket/mysql.sock  -uroot -p123456

//8.1、将启动脚本加入到开机自启动中:
[[email protected] ~]# cp /mysqldb/mysqlrunall.sh /etc/init.d/mysqlrunall.sh   
                                        //将自启动脚本cp到/etc/init.d/并命名为mysqlrunall.sh
[[email protected] ~]# chmod +x /etc/init.d/mysqlrunall.sh         //为该脚本赋予可执行权限
[[email protected] ~]# chkconfig --add mysqlrunall.sh              //增加mysqlrunall为系统内服务;
[[email protected] ~]# chkconfig mysqlrunall.sh    on              //设定mysql的各等级为on
[[email protected] ~]# chkconfig --list
mysqlrunall.sh  0:off   1:off   2:on    3:on    4:on    5:on    6:off
//等级0表示:表示关机
//等级1表示:单用户模式
//等级2表示:无网络连接的多用户命令行模式
//等级3表示:有网络连接的多用户命令行模式
//等级4表示:不可用
//等级5表示:带图形界面的多用户模式
//等级6表示:重新启动

猜你喜欢

转载自blog.51cto.com/15005403/2552284