MySQL5.7二进制安装(自定义目录)
#创建自定义目录
[root@db03 ~]# mkdir /service
[root@db03 ~]# cd /service
#卸载mariadb
[root@db03 /service]# yum remove -y mariadb-libs
#创建mysql用户
[root@db03 /service]# useradd mysql -s -M /sbin/nologin
#安装依赖
[root@db03 /service]# yum install -y ncurses-devel libaio-devel gcc gcc-c++ numactl libaio glibc cmake autoconf
#官网下载
#下载地址:https://downloads.mysql.com/archives/community/
[root@db03 /service]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz
#或者下载到本地上传
[root@db03 /service]# rz mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz
[root@db03 /service]# tar xf mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz
[root@db03 /service]# ll
total 535916
drwxr-xr-x 9 mysql mysql 151 Nov 30 2015 mysql-5.7.10-linux-glibc2.5-x86_64
-rw-r--r-- 1 mysql mysql 548774698 Sep 17 09:37 mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz
[root@db03 /service]# mv mysql-5.7.10-linux-glibc2.5-x86_64 mysql-5.7.10
[root@db03 /service]# ln -s /service/mysql-5.7.10 /service/mysql
[root@db03 /service]# ll
total 535916
lrwxrwxrwx 1 root root 21 Sep 17 09:46 mysql -> /service/mysql-5.7.10
drwxr-xr-x 9 mysql mysql 164 Sep 17 09:46 mysql-5.7.10
#创建数据存放目录
[root@db03 /service]# mkdir /mysql/data
#授权
[root@db03 /service]# chown -R mysql.mysql /service/mysql/
#初始化
[root@db03 /service]# ./mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/service/mysql --datadir=/service/mysql/data
参数介绍:
--initialize-insecure 不安全初始化,即没有密码。
--initialize 安全初始化,会自动生成临时密码。在log-error里,查询 temporary password
--user=mysql 初始化用户
--basedir=/service/mysql 软件安装目录
--datadir=/service/mysql/data 数据存放位置
#注:5.7和5.6版本初始化命令不一样
5.7:
/service/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/service/mysql --datadir=/service/mysql/data
5.6:
/service/mysql/scripts/mysql_install_db --user=mysql --basedir=/service/mysql --datadir=/service/mysql/data
#编辑MySQL配置文件
[root@db03 /service]# vim /etc/my.cnf
[mysqld]
basedir=/service/mysql
datadir=/service/mysql/data
port=3306
socket=/tmp/mysql.sock
character-set-server=utf8mb4
log-error=/var/log/mysqld.log
pid-file=/service/mysql/data/mysqld.pid
[mysql]
socket=/tmp/mysql.sock
[client]
socket=/tmp/mysql.sock
#生成启动脚本,并启动测试
[root@db03 /service]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@db03 /service]# chmod +x /etc/init.d/mysqld
[root@db03 /service]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/mysql_data
[root@db03 /service]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
#配置system启动
#注意关闭之前开启的mysql
[root@db03 /service]# /etc/init.d/mysqld stop
[root@db03 /service]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://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=/service/mysql/bin/mysqld --defaults-file=/etc/my.cnf
Restart=on-failure
RestartPreventExitStatus=1
TimeoutSec=0
PrivateTmp=false
LimitNOFILE = 5000
#设置环境变量
#[root@db03 /service]# vim /etc/profile.d/mysql.sh
#export PATH=/service/mysql/bin:$PATH
#[root@db03 /service]# source /etc/profile
#二进制的包是源码包已经生成编译安装完成的,在cmake阶段已经指定了所有的目录都是/usr/local/mysql,所以启动时所有程序都去找/usr/local/mysql目录,没有该目录,所以启动失败
#解决启动问题
1)方法一:做软连接
[root@db03 ~]# ln -s /service/mysql /usr/local/mysql
2)方法二:修改启动文件
[root@db03 ~]# vim /etc/init.d/mysqld
basedir=/service/mysql
datadir=/service/mysql/data
[root@db03 /service]# systemctl daemon-reload
[root@db03 /service]# systemctl start mysqld
多实例
[root@db03 /service]# mkdir -p 330{7,8,9}/{data,log}
#初始化
[root@db03 /service]# ./mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/service/mysql --datadir=/service/3309/data
[root@db03 /service]# ./mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/service/mysql --datadir=/service/3308/data
[root@db03 /service]# ./mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/service/mysql --datadir=/service/3307/data
[root@db03 /service]# vim 3307/my.cnf
[mysqld]
basedir=/service/mysql
datadir=/service/3307/data
port=3307
socket=/service/3307/data/mysql.sock
character-set-server=utf8mb4
log-error=/service/3307/log/mysqld.log
pid-file=/service/3307/mysqld.pid
[mysql]
socket=/service/3307/data/mysql.sock
[client]
socket=/service/3307/data/mysql.sock
[root@db03 /service]# vim 3308/my.cnf
[mysqld]
basedir=/service/mysql
datadir=/service/3308/data
port=3308
socket=/service/3308/data/mysql.sock
character-set-server=utf8mb4
log-error=/service/3308/log/mysqld.log
pid-file=/service/3308/mysqld.pid
[mysql]
socket=/service/3308/data/mysql.sock
[client]
socket=/service/3308/data/mysql.sock
[root@db03 /service]# vim 3309/my.cnf
[mysqld]
basedir=/service/mysql
datadir=/service/3309/data
port=3309
socket=/service/3309/data/mysql.sock
character-set-server=utf8mb4
log-error=/service/3309/log/mysqld.log
pid-file=/service/3309/mysqld.pid
[mysql]
socket=/service/3309/data/mysql.sock
[client]
socket=/service/3309/data/mysql.sock
#授权
[root@db03 /service]# chown -R mysql.mysql /service/
#启动
[root@db03 /service]# /service/mysql/bin/mysqld --defaults-file=/service/3307/my.cnf &
[root@db03 /service]# /service/mysql/bin/mysqld --defaults-file=/service/3308/my.cnf &
[root@db03 /service]# /service/mysql/bin/mysqld --defaults-file=/service/3309/my.cnf &
[root@db03 /service]# netstat -lntp
tcp6 0 0 :::3306 :::* LISTEN 9324/mysqld
tcp6 0 0 :::3307 :::* LISTEN 10621/mysqld
tcp6 0 0 :::3308 :::* LISTEN 10787/mysqld
tcp6 0 0 :::3309 :::* LISTEN 10951/mysqld
#停止多实例
[root@db03 /service]# mysqladmin -S /service/3307/data/mysql.sock shutdown
#配置system启动
[root@db03 /service]# vim /usr/lib/systemd/system/mysqld-1.service
[Unit]
Description=MySQL Server
After=network.target
After=syslog.target
[Service]
User=mysql
Group=mysql
ExecStart=/service/mysql/bin/mysqld --defaults-file=/service/3307/my.cnf
Restart=on-failure
RestartPreventExitStatus=1
TimeoutSec=0
PrivateTmp=false
[Install]
WantedBy=multi-user.target
[root@db03 /service]# vim /usr/lib/systemd/system/mysqld-2.service
[Unit]
Description=MySQL Server
After=network.target
After=syslog.target
[Service]
User=mysql
Group=mysql
PIDFile=/service/3308/mysqld.pid
ExecStart=/service/mysql/bin/mysqld --defaults-file=/service/3308/my.cnf
Restart=on-failure
RestartPreventExitStatus=1
TimeoutSec=0
PrivateTmp=false
[Install]
WantedBy=multi-user.target
#启动测试
[root@db03 /service]# systemctl daemon-reload
解读mysqld.service配置含义
[root@db03 /service]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://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=/service/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf
Restart=on-failure
RestartPreventExitStatus=1
TimeoutSec=0
PrivateTmp=false
LimitNOFILE = 5000
# 表示如果network.target或syslog.target需要启动,那么mysql服务应该在它们之后启动
After=network.target
After=syslog.target
# 定义运行用户和组
User=mysql
Group=mysql
# 定义启动进程时执行的命令
ExecStart=/service/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf
# Restart定义了此服务退出后Systemd的重启方式。
# on-failure表示非正常退出时(退出状态码非0),包括被信号终止和超时,才会重启。如正常退出停止(即systemctl stop)就不会重启。
Restart=on-failure
# 此配置定义了当符合某些退出状态(用exit code表示)时不要进行重启,1表示该程序被挂起。
RestartPreventExitStatus=1
# 此配置同时定义了TimeoutStartSec 和 TimeoutStopSec的值
• TimeoutStartSec: 配置等待启动的时间。如果守护程序服务未在配置的时间内指示启动完成,则该服
务将被视为失败,并将再次关闭。默认以秒为单位,或者 “5min 20s” 之类的时间跨度值。0表示禁用
• TimeoutStopSec: 此选项有两个目的。首先,它配置等待每个ExecStop的时间。如果其中任何一个超
时,则后续的ExecStop将被跳过,服务将被SIGTERM终止。如果未指定ExecStop,则该服务将立即获取SIGTERM。
其次,它配置等待服务本身停止的时间。如果未在指定时间内终止,它将被SIGKILL强制终止。默认以秒为单
位,或者 “5min 20s” 之类的时间跨度值。0表示禁用
TimeoutSec=0
# 是否使用私有的tmp目录,即产生的临时文件(如我们在my.cnf文件中配置的 "socket=/tmp/mysql.sock" )是否直接存放到/tmp 目录下
# 如果为true,临时文件一般会保存到systemd-private-XXX-mysql.service-XXX/tmp/下。
PrivateTmp=false
# 此配置和服务开机自启相关,表示该服务所在的Target。multi-user.target表示运行级别,为运行级别3,完全的多用户。
# 即当执行 "systemctl enable mysql"时, 会创建一个mysql.service的软链接到/etc/systemd/system/multi-user.target.wants目录下,此目录下服务都会开机自启。
WantedBy=multi-user.target