MySQL 的 三种安装方式:包安装,二进制安装,源码编译安装。
2.5.1)实战案例:通用 二进制格式安装 MySQL 5.6( 手动安装 )
下载链接:MySQL :: Download MySQL Community Server (Archived Versions)
// 创建用户组与用户groupadd -r -g 306 mysqluseradd -r -g 306 -u 306 -d /data/mysql mysql
// 可选做, 后面的脚本 mysql_install_db 可自动生成此目录// 创建目录mkdir /data/mysql -pchown mysql:mysql /data/mysql
官方:MySQL :: Download MySQL Community Server (Archived Versions)
国内:Index of /MySQL/Downloads/MySQL-5.6/
国内:Index of mysql-local/Downloads/MySQL-5.6
// 例如: mysql-5.6.50-linux-glibc2.12-x86_64.tar.gz// 解压压缩包tar xf mysql-VERSION-linux-glibc2.12-x86_64.tar.gz -C /usr/local// 创建软链接cd /usr/local && ln -sv mysql-VERSION mysqlchown -R root:root /usr/local/mysql/
// 拷贝模板配置文件cd /usr/local/mysqlcp -b support-files/my-default.cnf /etc/my.cnfvim /etc/my.cnf// mysql 语句块中添加以下三个选项[mysqld]datadir = /data/mysql # 数据存放目录innodb_file_per_table = on # 在 mariadb5.5 以上版的是默认值, 可不加skip_name_resolve = on # 禁止主机名解析, 建议使用
// "报错解决"yum install perl-Data-Dumper.x86_64 -ycd /usr/local/mysql/// 执行脚本 ( "可能会出现报错情况" )./scripts/mysql_install_db --datadir=/data/mysql --user=mysql// 验证[root@centos7 mysql] ls /data/mysql/ -l ( 默认存放在 /var/lib/mysql 目录 )total 110604-rw-rw---- 1 mysql mysql 12582912 Jun 1 16:44 ibdata1-rw-rw---- 1 mysql mysql 50331648 Jun 1 16:44 ib_logfile0-rw-rw---- 1 mysql mysql 50331648 Jun 1 16:44 ib_logfile1drwx------ 2 mysql mysql 4096 Jun 1 16:44 mysqldrwx------ 2 mysql mysql 4096 Jun 1 16:44 performance_schemadrwx------ 2 mysql mysql 4096 Jun 1 16:44 test
// 配置开机自启服务cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqldchkconfig --add mysqldservice mysqld start// 如果有对应的 service 文件可以执行下面cp /usr/local/mysql/support-files/systemd/mariadb.service/usr/lib/systemd/system/systemctl daemon-reloadsystemctl enable --now mariadb
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.shbash /etc/profile.d/mysql.sh// "推荐"echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profilesource /etc/profile
mysql_secure_installation回车y输入密码重复密码yyyymysql -uroot -pP@ssw0rd123456
2.5.2)实战案例:一键安装 mysql-5.6 二进制包 脚本
2.5.2.1)( 在线安装 ) mysql-5.6 二进制包 脚本
// 1. 编写脚本[root@centos7 ~] vim install_mysql5.6.sh#!/bin/bash. /etc/init.d/functionsDIR=`pwd`MYSQL_VERSION=5.6.51NAME="mysql-${MYSQL_VERSION}-linux-glibc2.12-x86_64.tar.gz"FULL_NAME=${DIR}/${NAME}# URL=http://mirrors.163.com/mysql/Downloads/MySQL-5.6URL=https://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-5.6/# URL=https://mirrors.huaweicloud.com/mysql/Downloads/MySQL-5.6/DATA_DIR="/data/mysql"rpm -q wget || yum -y -q install wgetwget $URL/$NAME || { action "下载失败,异常退出" false;exit 10; }yum install -y -q libaio perl-Data-Dumper autoconfif [ -f ${FULL_NAME} ];thenaction "安装文件存在"elseaction "安装文件不存在" falseexit 3fiif [ -e /usr/local/mysql ];thenaction "Mysql 已经安装" falseexit 3elsetar xf ${FULL_NAME} -C /usr/local/srcln -sv /usr/local/src/mysql-${MYSQL_VERSION}-linux-glibc2.12-x86_64 /usr/local/mysqlif id mysql;thenaction "mysql 用户已经存在,跳过创建用户过程"elseuseradd -r -s /sbin/nologin mysqlfiif id mysql;thenchown -R mysql.mysql /usr/local/mysql/*if [ ! -d /data/mysql ];thenmkdir -pv /data/mysql && chown -R mysql.mysql /data /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql/cp /usr/local/src/mysql-${MYSQL_VERSION}-linux-glibc2.12-x86_64/support-files/mysql.server /etc/init.d/mysqldchmod a+x /etc/init.d/mysqldcat > /etc/my.cnf <<EOF[mysqld]socket=/data/mysql/mysql.sockuser=mysqlsymbolic-links=0datadir=/data/mysqlinnodb_file_per_table=1[client]port=3306socket=/data/mysql/mysql.sock[mysqld_safe]log-error=/var/log/mysqld.logpid-file=/tmp/mysql.sockEOFln -sv /usr/local/mysql/bin/mysql /usr/bin/mysql/etc/init.d/mysqld startchkconfig --add mysqldelseaction "MySQL数据目录已经存在" falseexit 3fififi// 2. 运行脚本[root@centos7 ~] bash install_mysql5.6.sh// 3. 验证[root@centos7 ~] cat /etc/my.cnf[mysqld]socket=/data/mysql/mysql.sockuser=mysqlsymbolic-links=0datadir=/data/mysqlinnodb_file_per_table=1[client]port=3306socket=/data/mysql/mysql.sock[mysqld_safe]log-error=/var/log/mysqld.logpid-file=/tmp/mysql.sock[root@centos7 ~] lsinstall_mysql5.6.sh my.cnf mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
// 验证服务netstat -nltp// 登录mysql
2.5.2.2)离线安装 mysql-5.6 二进制包的脚本
// 准备安装包, 脚本和 my.cnf 配置文件, "放在同一目录下"[root@centos8 ~] vim install_mysql-5.6.sh#!/bin/bash# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.6.50-linux-glibc2.12-x86_64.tar.gzDIR=`pwd`NAME="/mysql-5.6.50-linux-glibc2.12-x86_64.tar.gz"FULL_NAME=${DIR}/${NAME}DATA_DIR="/data/mysql"yum install libaio perl-devel ncurses-compat-libs -y# CentOS8 需要安装 ncurses-compat-libs 包提供 libncurses.so.5 库if [ -f ${FULL_NAME} ];thenecho "install file is exist"elseecho "install file isn't exist"exit 3fiif [ -h /usr/local/mysql ];thenecho "MySQL already install"exit 3elsetar xvf ${FULL_NAME} -C /usr/local/src/ln -sv /usr/local/src/mysql-5.6.50-linux-glibc2.12-x86_64 /usr/local/mysqlif id mysql;thenecho "mysql user is exist,skip create user"elseuseradd -r -u 306 -d /data/mysql mysqlfiif id mysql;thenchown -R mysql.mysql /usr/local/mysql/if [ ! -d /data/mysql ];thenmkdir -pv /data/mysql && chown -R mysql.mysql /data/mysql//usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql -basedir=/usr/local/mysqlcp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqldchmod a+x /etc/init.d/mysqldcp ${DIR}/my.cnf /etc/my.cnfln -sv /usr/local/mysql/bin/mysql /usr/bin/mysql/etc/init.d/mysqld startchkconfig --add mysqldelseecho "MySQL data dir already exist"exit 3fififi[root@centos8 ~] vim my.cnf[mysqld]socket=/data/mysql/mysql.sockuser=mysqlsymbolic-links=0datadir=/data/mysqlinnodb_file_per_table=1[client]port=3306socket=/data/mysql/mysql.sock[mysqld_safe]log-error=/var/log/mysqld.logpid-file=/tmp/mysql.sock[root@centos8 ~] ls[root@centos8 ~] bash install_mysql-5.6.sh
2.5.3)实战案例:通用 二进制安装 MySQL 5.7 和 MySQL8.0
报错解决:mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file-CSDN博客
yum install libncurses* -yum install libaio numactl-libs -y
useradd -r -g mysql -s /bin/false mysql
国内:mysql-MySQL-5.7安装包下载_开源镜像站-阿里云
// 下载 MySQL 二进制包wget https://mirrors.aliyun.com/mysql/MySQL-5.7/mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz// 解压tar xf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz -C /usr/local// 软链接cd /usr/local/ && ln -s mysql-5.7.36-linux-glibc2.12-x86_64/ mysqlchown -R root.root /usr/local/mysql/
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.shbash /etc/profile.d/mysql.sh"推荐"echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profilesource /etc/profile
cp /etc/my.cnf{,.bak}vim /etc/my.cnf[mysqld]datadir=/data/mysqlskip_name_resolve=1socket=/data/mysql/mysql.sock log-error=/data/mysql/mysql.logpid-file=/data/mysql/mysql.pid[client]socket=/data/mysql/mysql.sock
mkdir /data/mysql -pmysqld --initialize --user=mysql --datadir=/data/mysqlgrep password /data/mysql/mysql.log2023-12-30T09:40:49.213204Z 1 [Note] A temporary password is generated for root@localhost: Q.xer?jWC8;O // 注意生成 root 的初始密码awk '/temporary password/{print $NF}' /data/mysql/mysql.logQ.xer?jWC8;O
2.5.3.6.2)方式 2:生成 root 空密码( 省略 )
mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
service mysqld start
// 修改前面生成的随机密码为指定密码awk '/temporary password/{print $NF}' /data/mysql/mysql.logQ.xer?jWC8;Omysqladmin -uroot -p'Q.xer?jWC8;O' password P@ssw0rd123456# 修改前面生成的空密码为指定密码mysqladmin -uroot password P@ssw0rd123456
报错解决:mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file-CSDN博客
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
// mysql -uroot -pP@ssw0rd123456
2.5.4)实战案例:一键安装 MySQL5.7 和 MySQL8.0 二进制包的脚本
( 离线安装 ) MySQL5.7 和 MySQL8.0 二进制包的脚本
#!/bin/bash# MySQL Download URL:# https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz#h ttp://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz# http://mirrors.163.com/mysql/Downloads/MySQL-8.0/mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz. /etc/init.d/functionsSRC_DIR=`pwd`MYSQL='mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz'#MYSQL='mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz'MYSQL_ROOT_PASSWORD=123456COLOR='echo -e \E[01;31m'END='\E[0m'check (){if [ $UID -ne 0 ]; thenaction "当前用户不是root,安装失败" falseexit 1ficd $SRC_DIRif [ ! -e $MYSQL ];then$COLOR"缺少${MYSQL}文件"$END$COLOR"请将相关软件放在${SRC_DIR}目录下"$ENDexitelif [ -e /usr/local/mysql ];thenaction "数据库已存在,安装失败" falseexitelsereturnfi}install_mysql(){$COLOR"开始安装MySQL数据库..."$ENDyum -y -q install libaio numactl-libstar xf $MYSQL -C /usr/local/MYSQL_DIR=`echo $MYSQL| sed -nr 's/^(.*[0-9]).*/\1/p'`ln -s /usr/local/$MYSQL_DIR /usr/local/mysqlchown -R root.root /usr/local/mysql/id mysql &> /dev/null || { useradd -s /sbin/nologin -r mysql ; action "创建mysql用户"; }echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh. /etc/profile.d/mysql.shln -s /usr/local/mysql/bin/* /usr/bin/cat > /etc/my.cnf <<-EOF[mysqld]server-id=`hostname -I|cut -d. -f4`log-bindatadir=/data/mysqlsocket=/data/mysql/mysql.sock log-error=/data/mysql/mysql.logpid-file=/data/mysql/mysql.pid[client]socket=/data/mysql/mysql.sockEOF[ -d /data ] || mkdir /datamysqld --initialize-insecure --user=mysql --datadir=/data/mysqlcp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqldchkconfig --add mysqldchkconfig mysqld onservice mysqld start[ $? -ne 0 ] && { $COLOR"数据库启动失败,退出!"$END;exit; }# MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}' /data/mysql/mysql.log`# mysqladmin -uroot -p$MYSQL_OLDPASSWORD password $MYSQL_ROOT_PASSWORD &>/dev/nullsleep 3mysqladmin -uroot password $MYSQL_ROOT_PASSWORD &>/dev/nullaction "数据库安装完成"}checkinstall_mysql
[root@centos7 ~] cat install_online_mysql5.7or8.0_for_centos.sh#!/bin/bash. /etc/init.d/functionsSRC_DIR=`pwd`MYSQL='mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz'# URL=http://mirrors.163.com/mysql/Downloads/MySQL-5.7URL=https://repo.huaweicloud.com/mysql/Downloads/MySQL-5.7/# MYSQL='mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz'# URL=http://mirrors.163.com/mysql/Downloads/MySQL-8.0COLOR='echo -e \E[01;31m'END='\E[0m'MYSQL_ROOT_PASSWORD=mageducheck (){if [ $UID -ne 0 ]; thenaction "当前用户不是root,安装失败" falseexit 1ficd $SRC_DIRrpm -q wget || yum -y -q install wgetwget $URL/$MYSQLif [ ! -e $MYSQL ];then$COLOR"缺少${MYSQL}文件"$END$COLOR"请将相关软件放在${SRC_DIR}目录下"$ENDexitelif [ -e /usr/local/mysql ];thenaction "数据库已存在,安装失败" falseexitelsereturnfi}mkdir /data/mysql -pinstall_mysql(){$COLOR"开始安装MySQL数据库..."$ENDyum -y -q install libaio numactl-libs libaio &> /dev/nullcd $SRC_DIRtar xf $MYSQL -C /usr/local/MYSQL_DIR=`echo $MYSQL| sed -nr 's/^(.*[0-9]).*/\1/p'`ln -s /usr/local/$MYSQL_DIR /usr/local/mysqlchown -R root.root /usr/local/mysql/id mysql &> /dev/null || { useradd -s /sbin/nologin -r mysql ; action "创建 mysql 用户"; }echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh. /etc/profile.d/mysql.shln -s /usr/local/mysql/bin/* /usr/bin/cat > /etc/my.cnf <<-EOF[mysqld]server-id=`hostname -I|cut -d. -f4`log-bindatadir=/data/mysqlsocket=/data/mysql/mysql.sock log-error=/data/mysql/mysql.logpid-file=/data/mysql/mysql.pid[client]socket=/data/mysql/mysql.sockEOFmysqld --initialize --user=mysql --datadir=/data/mysqlcp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqldchkconfig --add mysqldchkconfig mysqld onservice mysqld start[ $? -ne 0 ] && { $COLOR"数据库启动失败,退出!"$END;exit; }MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}' /data/mysql/mysql.log`mysqladmin -uroot -p$MYSQL_OLDPASSWORD password $MYSQL_ROOT_PASSWORD &>/dev/nullaction "数据库安装完成"}checkinstall_mysql
yum install gcc gcc-c++ cmake bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel perl-Data-Dumper -y
useradd -r -s /sbin/nologin -d /data/mysql mysql
chown mysql.mysql /data/mysql
cmake 的 重要特性 之一是其独立于源码(out-of-source)的编译功能,即编译工作可以在另一个指定的目录中而非源码目录中进行,这可以保证源码目录不受任何一次编译的影响,因此在同一个源码树上可以进行多次不同的编译,如针对于不同平台编译。
https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html
在官网 MySQL :: Download MySQL Community Server (Archived Versions) 下载源码包
上面我们下载的是编译后的二进制包。( 二进制安装使用的更为广泛一些 )
tar xvf mysql-5.6.51.tar.gz -C /usr/local/src
# mariadb-10.2.18.tar.gz
提示:如果出错,执行 rm -rf CMakeCache.txt
// 切换目录cd /usr/local/srcdu -sh mysql-5.6.51// 使用 cmake 命令进行编译cd mysql-5.6.51/cmake . \-DCMAKE_INSTALL_PREFIX=/apps/mysql \-DMYSQL_DATADIR=/data/mysql/ \-DSYSCONFDIR=/etc/ \-DMYSQL_USER=mysql \-DWITH_INNOBASE_STORAGE_ENGINE=1 \-DWITH_ARCHIVE_STORAGE_ENGINE=1 \-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \-DWITH_PARTITION_STORAGE_ENGINE=1 \-DWITHOUT_MROONGA_STORAGE_ENGINE=1 \-DWITH_DEBUG=0 \-DWITH_READLINE=1 \-DWITH_SSL=system \-DWITH_ZLIB=system \-DWITH_LIBWRAP=0 \-DENABLED_LOCAL_INFILE=1 \-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \-DDEFAULT_CHARSET=utf8 \-DDEFAULT_COLLATION=utf8_general_ci// 编译安装 ( 注意: 主机 CPU 核数 )lscpu | grep Socketmake -j 8 && make install// 验证编译安装目录 ( 注意: /usr/local/src 为源码存放目录 )ll /apps/mysql ( 程序最终存放目录 )
echo 'PATH=/apps/mysql/bin:$PATH' > /etc/profile.d/mysql.shbash /etc/profile.d/mysql.sh'推荐'echo 'PATH=/apps/mysql/bin:$PATH' > /etc/profilesource /etc/profile
// 切换目录cd /apps/mysql/// 必须在 /apps/mysql 目录下执行 mysql_install_db 脚本 ( 否则会报错 )scripts/mysql_install_db --datadir=/data/mysql/ --user=mysql'验证'll /data/mysql/
// 复制模板配置文件 ( -b 参数会备份被覆盖的文件 )cp -b /apps/mysql/support-files/my-default.cnf /etc/my.cnf// 针对旧版本或 mariadb-10.2.18.tar.gzcp /apps/mysql/support-files/my-huge.cnf /etc/my.cnf
// 复制模板文件cp /apps/mysql/support-files/mysql.server /etc/init.d/mysqld// 设置开机自启 chkconfig --add mysqldservice mysqld start// 验证netstat -nltp
回车
y
输入密码
重复密码
y
y
y
y
// 1) 低版本 Docker 安装yum install docker -y----// < '推荐' >// 2) 高版本 Docker 安装curl -fsSL https://get.docker.com/ | sh----// 关闭防火墙systemctl disable --now firewalldsetenforce 0// 启用 Dockersystemctl enable --now docker----// 3) 安装 Docker-Composecurl -L https://github.com/docker/compose/releases/download/v2.2.2/docker-compose-linux-x86_64 -o /usr/local/bin/docker-composechmod +x /usr/local/bin/docker-composedocker-compose -v[root@centos8 ~] docker run --name mysql -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.30[root@centos8 ~] docker images[root@centos8 ~] docker ps -a
[root@centos8 ~] yum install mysql -y
[root@centos8 ~] mysql -uroot -p123456 -h127.0.0.1
参考:https://www.cnblogs.com/yangliheng/p/5871215.html
简单地说,Mysql 多实例就是 在一台服务器上同时开启多个不同的服务端口(3306、3307),同时运行多个 Mysql 服务进程,这些服务进程通过不同的 Socket 监听不同的服务端口来提供服务。
这些 Mysql 多实例公用一套 Mysql 安装程序,一般使用不同的 my.cnf(也可以相同)配置文件,启动程序和数据文件。在提供服务时,多实例 Mysql 在逻辑上看来是 各自独立 的,它们根据配置文件的对应设定值,获得服务器相应数量的硬件资源。
打个比方,Mysql 多实例就相当于房子的多个卧室,每个实例可以看作一间卧室,整个服务器就是一套房子,服务器的硬件资源(cpu、mem、disk)、软件资源(centos操作系统)可以看作房子的卫生间、客厅,是房子的公用资源。
多实例 类似微信多开,多实例的不同端口号 类比微信多开的不同微信账号,数据库类比聊天窗口,表类比聊天记录。
MySQL 多实例 就是在一台服务器上同时开启多个不同的服务端口(如:3306、3307 等),同时运行多个 MySQL 服务进程,这些服务进程通过不同的 Socket 监听不同的服务端口来提供服务。
多实例可能是 MySQL 的不同版本,也可能是 MySQL 的同一版本实现。
可有效利用服务器资源。当单个服务器资源有剩余时,可以充分利用剩余资源提供更多的服务,且可以实现资源的逻辑隔离节约服务器资源。例如公司服务器资源紧张,但是数据库又需要各自尽量独立的提供服务,并且还需要到主从复制等技术,多实例就是最佳选择。
存在资源互相抢占的问题。比如:当某个数据库实例并发很高或者 SQL 查询慢时,整个实例会消耗大量的 CPU、磁盘I/O 等资源,导致服务器上面其他的数据库实例在提供服务的质量也会下降,所以具体的需求要根据自己的实际情况而定。
MySQL 官方文档提到的单一配置文件、单一启动程序多实例部署方式
多配置文件、多启动程序部署方式是针对每个实例都有独立的配置文件和目录,管理灵活,此方案耦合度较低
2.8.2)实战案例 :CentOS 8 实现 MariaDB Yum 安装 多实例实现
CentOS 8 Yum 安装 MariaDB-10.3.17 并实现三个实例
yum install ntpdate -y && ntpdate ntp.aliyun.com
systemctl disable --now firewalld
sed -ri.bak 's/^(SELINUX=).*/\1disabled/' /etc/selinux/config
setenforce 0
[root@centos8 ~] yum install mariadb-server -y
// 创建各个实例独自的目录[root@centos8 ~] mkdir -pv /mysql/{3306,3307,3308}/{data,etc,socket,log,bin,pid}// 授权目录 ( 都基于 mysql 用户运行 )[root@centos8 ~] chown -R mysql.mysql /mysql[root@centos8 ~] tree -d /mysql/
[root@centos8 ~] mysql_install_db --user=mysql --datadir=/mysql/3306/data
[root@centos8 ~] mysql_install_db --user=mysql --datadir=/mysql/3307/data
[root@centos8 ~] mysql_install_db --user=mysql --datadir=/mysql/3308/data
[root@centos8 ~] vim /mysql/3306/etc/my.cnf[mysqld]port=3306datadir=/mysql/3306/datasocket=/mysql/3306/socket/mysql.socklog-error=/mysql/3306/log/mysql.logpid-file=/mysql/3306/pid/mysql.pid// 重复上面步骤配置 3307, 3308 的配置文件[root@centos8 ~] sed 's/3306/3307/' /mysql/3306/etc/my.cnf > /mysql/3307/etc/my.cnf[root@centos8 ~] sed 's/3306/3308/' /mysql/3306/etc/my.cnf > /mysql/3308/etc/my.cnf'验证'[root@centos8 ~] cat /mysql/3307/etc/my.cnf[root@centos8 ~] cat /mysql/3308/etc/my.cnf
[root@centos8 ~] vim /mysql/3306/bin/mysqld#!/bin/bashport=3306mysql_user="root"mysql_pwd=""cmd_path="/usr/bin"mysql_basedir="/mysql"mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock"function_start_mysql(){if [ ! -e "$mysql_sock" ];thenprintf "Starting MySQL...\n"${cmd_path}/mysqld_safe --defaults-file=${mysql_basedir}/${port}/etc/my.cnf &> /dev/null &elseprintf "MySQL is running...\n"exitfi}function_stop_mysql(){if [ ! -e "$mysql_sock" ];thenprintf "MySQL is stopped...\n"exitelseprintf "Stoping MySQL...\n"${cmd_path}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdownfi}function_restart_mysql(){printf "Restarting MySQL...\n"function_stop_mysqlsleep 2function_start_mysql}case $1 instart)function_start_mysql;;stop)function_stop_mysql;;restart)function_restart_mysql;;*)printf "Usage: ${mysql_basedir}/${port}/bin/mysqld {start|stop|restart}\n"esac// 重复上述过程, 分别建立 3307, 3308 的启动脚本 [root@centos8 ~] sed 's/3306/3307/' /mysql/3306/bin/mysqld > /mysql/3307/bin/mysqld[root@centos8 ~] sed 's/3306/3308/' /mysql/3306/bin/mysqld > /mysql/3308/bin/mysqld'授权'[root@centos8 ~] chmod +x /mysql/3306/bin/mysqld[root@centos8 ~] chmod +x /mysql/3307/bin/mysqld[root@centos8 ~] chmod +x /mysql/3308/bin/mysqld
[root@centos8 ~] /mysql/3306/bin/mysqld start
[root@centos8 ~] /mysql/3307/bin/mysqld start
[root@centos8 ~] /mysql/3308/bin/mysqld start
[root@centos8 ~] netstat -nltp
[root@centos8 ~] /mysql/3308/bin/mysqld start'两种连接方法'[root@centos8 ~] mysql -h127.0.0.1 -P3308[root@centos8 ~] mysql -uroot -S /mysql/3308/socket/mysql.sock// 确认连接的端口MariaDB [(none)]> show variables like 'port';+---------------+-------+| Variable_name | Value |+---------------+-------+| port | 3308 |+---------------+-------+1 row in set (0.001 sec)MariaDB [(none)]>// '测试多实例效果'MariaDB [(none)]> create database test3308;Query OK, 1 row affected (0.001 sec)MariaDB [(none)]> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || test || test3308 |+--------------------+5 rows in set (0.001 sec)// 我们在 3308 实例创建的数据库// 其他实例是看不到的, '数据库实例间相互独立.'[root@centos8 ~] mysql -uroot -S /mysql/3307/socket/mysql.sockMariaDB [(none)]> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || test |+--------------------+4 rows in set (0.001 sec)
// 加上 root 的口令[root@centos8 ~] mysqladmin -uroot -S /mysql/3306/socket/mysql.sock password 'P@ssw0rd'[root@centos8 ~] mysqladmin -uroot -S /mysql/3307/socket/mysql.sock password 'P@ssw0rd'[root@centos8 ~] mysqladmin -uroot -S /mysql/3308/socket/mysql.sock password 'P@ssw0rd'// '或者' 登录 mysql, 执行如下 SQL 语句也可以Mariadb> update mysql.user set password=password("P@ssw0rd") where user='root';Mariadb> flush privileges;// 重复步骤, 分别修改别外两个实例 3307, 3308 对应 root 口令---// '关闭实例', 需要手动输入 root 的密码[root@centos8 ~] /mysql/3308/bin/mysqld stopStoping MySQL...Enter password:// 当然, 我们也可以将密码写入到 mysqld 服务文件中 ( 不安全 )vim /mysql/3308/bin/mysqldmysql_pwd="P@ssw0rd"// 重复上述过程, 分别在 3306, 3307 的启动脚本中填写密码信息[root@centos8 ~] /mysql/3308/bin/mysqld startMySQL is running...// 再次关闭实例, '无需再手动输入密码啦'[root@centos8 ~] /mysql/3308/bin/mysqld stopStoping MySQL...
[root@centos8 ~] mysql -uroot -p -S /mysql/3306/socket/mysql.sock
Enter password:
[root@centos8 ~] vim /etc/rc.d/rc.local// 在最后一行加下面内容for i in {3306..3308};do /mysql/$i/bin/mysqld start;done'授权'[root@centos8 ~] chmod +x /etc/rc.d/rc.local'验证'[root@centos8 ~] bash /etc/rc.d/rc.local[root@centos8 ~] netstat -nltp'重启验证'[root@centos8 ~] reboot[root@centos8 ~] netstat -nltp