单台主机MySQL多实例部署

二进制安装mysql-5.7.26

[root@mysql ~]# cd /server/tools/
[root@mysql tools]# ll
total 629756
-rw-r--r-- 1 root root 644869837 Jul  4 11:26 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
[root@mysql tools]# tar xf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz 
[root@mysql tools]# mkdir -p /application
[root@mysql tools]# 
[root@mysql tools]# mv mysql-5.7.26-linux-glibc2.12-x86_64 /application/
[root@mysql tools]# cd /application/
[root@mysql application]# mv mysql-5.7.26-linux-glibc2.12-x86_64/ mysql-5.7.26
[root@mysql application]# ln -s mysql-5.7.26/ mysql
[root@mysql application]# ll
total 0
lrwxrwxrwx 1 root root  13 Aug 28 09:05 mysql -> mysql-5.7.26/
drwxr-xr-x 9 root root 129 Aug 28 09:02 mysql-5.7.26

用户创建处理原始环境

[root@mysql ~]# yum -y remove mariadb-libs-5.5.56-2.el7.x86_64
[root@mysql ~]# useradd -s /sbin/nologin mysql

设置环境变量

[root@mysql ~]# echo ‘export PATH=$PATH:/application/mysql/bin‘ >> /etc/profile
[root@mysql ~]# source /etc/profile
root@mysql ~]# mysql -V
mysql  Ver 14.14 Distrib 5.7.26, for linux-glibc2.12 (x86_64) using  EditLine wrapper

创建新的硬盘做数据盘

mysql数据库由两大部分组成:软件部分、数据部分
数据部分应该与软件部分、操作系统的根独立

[root@mysql ~]# fdisk -l
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@mysql ~]# mkfs.xfs /dev/sda
sda   sda1  sda2  sda3  
[root@mysql ~]# mkfs.xfs /dev/sdb
meta-data=/dev/sdb               isize=512    agcount=4, agsize=1310720 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=5242880, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@mysql ~]# mkdir /data
[root@mysql ~]# blkid 
/dev/sdb: UUID="986b42ee-540d-47f5-82a6-65a328dd20b4" TYPE="xfs" 
[root@mysql ~]# tail -1 /etc/fstab
UUID=986b42ee-540d-47f5-82a6-65a328dd20b4 /data			  xfs 	  defaults 	  0 0
[root@mysql ~]# 
[root@mysql ~]# mount -a

授权

[root@mysql ~]# chown -R mysql.mysql /application/*
[root@mysql ~]# chown -R mysql.mysql /data

初始化数据(创建系统数据)

5.6版本:初始化命令:/application/mysql/scripts/mysql_install_db
5.7版本:初始化命令:mysqld --initalize

有密码初始化

--initialize 参数:
对于密码复杂度进行定制:12位,4种方式组成
密码过期时间:180天
给root@localhost用户设置临时密码

[root@mysql ~]# mkdir -p /data/mysql/data
[root@mysql ~]# chown -R mysql.mysql /data/
[root@mysql ~]# yum -y install libaio-devel
[root@mysql ~]# mysqld --initialize --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data
2019-08-28T01:33:44.886913Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-08-28T01:33:45.393308Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-08-28T01:33:45.610222Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-08-28T01:33:45.681098Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: e0660f6c-c933-11e9-af51-000c29d70b6d.
2019-08-28T01:33:45.682790Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened.
2019-08-28T01:33:45.684184Z 1 [Note] A temporary password is generated for root@localhost: DI<-ZsDU=4.0

无密码初始化

--initialize-insecure 参数:
无限制,无临时密码
[root@mysql ~]# \rm -rf /data/mysql/data/*
[root@mysql ~]# mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data
2019-08-28T01:40:43.207923Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-08-28T01:40:43.482220Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-08-28T01:40:43.519569Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-08-28T01:40:43.606086Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: d980595d-c934-11e9-bbfc-000c29d70b6d.
2019-08-28T01:40:43.608963Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened.
2019-08-28T01:40:43.612502Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

设置配置文件

[root@mysql data]# cat >/etc/my.cnf<<EOF
[mysqld]
user=mysql
basedir=/application/mysql
datadir=/data/mysql/data
socket=/tmp/mysql.sock
server_id=6
port=3306
[mysql]
socket=/tmp/mysql.sock
EOF
[root@mysql data]# 

启动数据库

方法1:service

[root@mysql data]# cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@mysql data]# service mysqld restart
 ERROR! MySQL server PID file could not be found!
Starting MySQL.Logging to ‘/data/mysql/data/mysql.err‘.
. SUCCESS! 
[root@mysql data]# 
[root@mysql data]# netstat -lntup|grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      2575/mysqld         
[root@mysql data]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS! 
[root@mysql data]# 

方法2:systemd

[root@mysql data]# cat /etc/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
[root@mysql data]# 
[root@mysql data]# systemctl start mysqld
[root@mysql data]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/etc/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2019-08-28 09:53:46 CST; 4s ago

方法3:命令行启动

[root@mysql data]# /application/mysql/bin/mysqld --defaults-file=/etc/my.cnf

密码设置

[root@mysql ~]# mysqladmin -uroot password 123456

数据库密码忘记解决方法

--skip-grant-tables	#跳过授权表
--skip-networking	#跳过远程登录

#启动数据库到维护模式
[root@mysql ~]# mysqld_safe --skip-grant-tables --skip-networking &
[root@mysql ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> 
mysql> flush privileges;
mysql> grant all on *.* to root@‘localhost‘ identified by ‘1‘;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> 
[root@mysql ~]# /etc/init.d/mysqld restart


以上是单实例的部署,以下是多实例的部署


准备多个目录

[root@mysql ~]# mkdir -p /data/330{7..9}/data

准备配置文件

[root@mysql ~]# cat /data/3307/my.cnf
[mysqld]
basedir=/application/mysql
datadir=/data/3307/data
socket=/data/3307/mysql.sock
log_error=/data/3307/mysql.log
port=3307
server_id=7
log_bin=/data/3307/mysql-bin

[root@mysql ~]# cat /data/3308/my.cnf
[mysqld]
basedir=/application/mysql
datadir=/data/3308/data
socket=/data/3308/mysql.sock
log_error=/data/3308/mysql.log
port=3308
server_id=8
log_bin=/data/3308/mysql-bin

[root@mysql ~]# cat /data/3309/my.cnf
[mysqld]
basedir=/application/mysql
datadir=/data/3309/data
socket=/data/3309/mysql.sock
log_error=/data/3309/mysql.log
port=3309
server_id=9
log_bin=/data/3309/mysql-bin
[root@mysql ~]# 

授权

[root@mysql system]# chown -R mysql.mysql /data/*

初始化数据

[root@mysql ~]#cp /etc/my.cnf /etc/my.cnf.bak
[root@mysql ~]#mysqld --initialize-insecure  --user=mysql --datadir=/data/3307/data --basedir=/application/mysql
[root@mysql ~]#mysqld --initialize-insecure  --user=mysql --datadir=/data/3308/data --basedir=/application/mysql
[root@mysql ~]#mysqld --initialize-insecure  --user=mysql --datadir=/data/3309/data --basedir=/application/mysql

systemd管理多实例

[root@mysql ~]# cd /etc/systemd/system/
[root@mysql system]# cp mysqld.service mysqld3307.service 
[root@mysql system]# cp mysqld.service mysqld3308.service 
[root@mysql system]# cp mysqld.service mysqld3309.service 
[root@mysql system]# sed -i ‘s#--defaults-file=/etc/my.cnf#--defaults-file=/data/3307/my.cnf#g‘ mysqld3307.service
[root@mysql system]# sed -i ‘s#--defaults-file=/etc/my.cnf#--defaults-file=/data/3308/my.cnf#g‘ mysqld3308.service
[root@mysql system]# sed -i ‘s#--defaults-file=/etc/my.cnf#--defaults-file=/data/3309/my.cnf#g‘ mysqld3309.service

启动

[root@mysql system]# systemctl start mysqld3307.service 
[root@mysql system]# systemctl start mysqld3308.service 
[root@mysql system]# systemctl start mysqld3309.service 

验证多实例

[root@mysql system]# netstat -lntup |grep mysqld
tcp6       0      0 :::3307                 :::*                    LISTEN      11319/mysqld        
tcp6       0      0 :::3308                 :::*                    LISTEN      11773/mysqld        
tcp6       0      0 :::3309                 :::*                    LISTEN      11821/mysqld        
tcp6       0      0 :::3306                 :::*                    LISTEN      3558/mysqld         
[root@mysql system]# 
[root@mysql system]# mysql -S /data/3307/mysql.sock  # 登陆进3307端口的数据库

单台主机MySQL多实例部署

上一篇:Oracle Database字符集(2)--基本概念


下一篇:SQL92标准