MySQL二进制安装
零、 环境
字段 | 位置 |
---|---|
ENV: |
vmware centos7.2 4c8g |
VER: |
mysql release: 5.7.34 |
BIN_PATH: |
/usr/local/mysql |
DATA-DIR: |
/data/mysql |
CONFIGURE: |
/etc/my.cnf |
一、安装前准备
1.0 安装依赖
yum install libaio
1.1 下载
1.2 解压二进制文件
tar -xzvf mysql-5.7.43-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.43-linux-glibc2.12-x86_64 /usr/local
ln -s /usr/local/mysql-5.7.43-linux-glibc2.12-x86_64 /usr/local/mysql
1.3 创建相关目录和用户及组
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
mkdir -p /data/mysql
mkdir -p /usr/local/mysql/tmp
chown -R mysql:mysql /data/mysql
chown -R mysql:mysql /usr/local/mysql
1.4 为MySQL实例创建配置文件
vim /etc/my.cnf
[mysqld] |
二、 安装
2.1 初始化实例
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
这里注意如下几个点:
- 保证
/usr/local/mysql
,/data/mysql
和/usr/local/mysql/tmp
的权限为mysql:mysql
- 保证
/data/mysql
目录为空
To initialize the data directory, invoke mysqld with the
--initialize
or--initialize-insecure
option, depending on whether you want the server to generate a random initial password for the'root'@'localhost'
account, or to create that account with no password:Use
--initialize
for “secure by default” installation (that is, including generation of a random initial root password). In this case, the password is marked as expired and you must choose a new one.
With
--initialize-insecure
, no root password is generated. This is insecure; it is assumed that you intend to assign a password to the account in a timely fashion before putting the server into production use.
可以使用 grep "password" /path to you mysql-err.log
找到使用--initialize
生成的随机密码
三、 管理 MySQL
3.1 传统方式管理
配置管理文件
cp -a /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
修改如下两行 (大约46/47两行)
vim /etc/rc.d/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql
管理 mysql
/etc/rc.d/init.d/mysql start / status / stop / reload / restart
3.2 使用 systemd 管理
完成传统方式管理的配置之后添加文件如下
vim /usr/lib/systemd/system/mysqld.service
# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Documentation=man:systemd-sysv-generator(8)
SourcePath=/etc/rc.d/init.d/mysqld
Description=LSB: start and stop MySQL
Before=runlevel2.target
Before=runlevel3.target
Before=runlevel4.target
Before=runlevel5.target
Before=shutdown.target
After=network-online.target
After=remote-fs.target
After=ypbind.service
After=nscd.service
After=ldap.service
After=ntpd.service
After=xntpd.service
After=network-online.target
After=sshd.service
Wants=network-online.target
Conflicts=shutdown.target
[Service]
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
ExecStart=/etc/rc.d/init.d/mysqld start
ExecStop=/etc/rc.d/init.d/mysqld stop
ExecReload=/etc/rc.d/init.d/mysqld reloadsystemctl daemon-reload
systemctl enable mysql --now
systemctl status mysql
四、 MySQL 配置文件参数优化
仅供参考:
[client] #客户端配置 |