1、下载Mysql二进制报(选择Linux的通用版本)
mysql官网地址:https://downloads.mysql.com/archives/community/
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.26-linux-glibc2.12-x86_64.tar
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.18-linux-glibc2.12-x86_64.tar
2、解压,注意需要解压二次
tar xvf /data/db/mysql-5.7.26-linux-glibc2.12-x86_64.tar
tar xvf /data/db/mysql-8.0.18-linux-glibc2.12-x86_64.tar
tar xvf /data/db/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
tar xvf /data/db/mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz
ansible 解压必须指定目录,如果不指定,会默认解压在root的家目录下。
ansible db -m shell -a 'tar xvf /data/db/mysql-5.7.26-linux-glibc2.12-x86_64.tar -C /data/db/'
ansible db -m shell -a 'tar xvf /data/db/mysql-8.0.18-linux-glibc2.12-x86_64.tar -C /data/db/'
ansible db -m shell -a 'tar xvf /data/db/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz -C /data/db/'
ansible db -m shell -a 'tar xvf /data/db/mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz -C /data/db/'
3、重名,方便记忆是识别
ansible db -m shell -a 'mv /data/db/mysql-5.7.26-linux-glibc2.12-x86_64 /data/db/mysql-5.7'
ansible db -m shell -a 'mv /data/db/mysql-8.0.18-linux-glibc2.12-x86_64 /data/db/mysql-8.0'
4. 创建用户组
groupdel mysql #删除组
groupadd mysql #添加组
5.创建用户
userdel mysql #删除用户
useradd -r -g mysql -s /bin/false mysql #添加用户 -g mysql 是指定用户组 -s /bin/false 是指定用户所使用的shell 最后mysql是用户名称
6. 指定mysql文件夹为mysql用户所有
chown mysql ./mysql -R
7.在data目录下创建mysql相关文件夹文件夹
mkdir /data/mysql
mkdir /data/mysql/logs
mkdir /data/mysql/conf
mkdir /data/mysql/data
8.安装相关依赖
apt-get install libaio1 libaio-dev
sudo apt-get install libncurses5-dev
9、创建my.cnf 文件
[mysql]
#设置mysql客户端默认字符集
default-character-set=utf8mb4
[mysqld]
skip-name-resolve
user=mysql
ngram_token_size=2
server-id=1
default_password_lifetime=0
port=3306
#设置安装目录
basedir=/opt/mysql
#数据存放目录
datadir=/data/mysql/data
log-error=/data/mysql/logs/err.log
#允许最大连接数
max_connections=1000
#服务端默认使用的字符集
character-set-server=utf8mb4
#创捷新表时默认的储存引擎
default-storage-engine=INNODB
#忘记密码时使用
#skip-grant-tables
#不区分大小写
lower_case_table_names=1
#认证方式
default_authentication_plugin=mysql_native_password
max_allowed_packet=500M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
wait_timeout=28800
interactive_timeout=28800
max_connect_errors=100
max_user_connections=0
#日志文件大小
max_binlog_size=100M
10、安装启动
指定配置文件安装
./mysqld --defaults-file=/data/mysql/conf/my.cnf --initialize
指定配置文件启动
./mysqld --defaults-file=/data/mysql/conf/my.cnf &
11、创建文件 mysql.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
# Have mysqld write its state to the systemd notify socket
Type=notify
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0
# Start main service
ExecStart=/opt/mysql/bin/mysqld --defaults-file=/data/mysql/conf/my.cnf $MYSQLD_OPTS
# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql
# Sets open_files_limit
LimitNOFILE = 10000
Restart=on-failure
RestartPreventExitStatus=1
# Set environment variable MYSQLD_PARENT_PID. This is required for restart.
Environment=MYSQLD_PARENT_PID=1
PrivateTmp=false
12、在把 mysql.service 放到 /usr/lib/systemd/system 目录下
systemctl start mysql #启动
systemctl stop mysql #停止
systemctl restart mysql #重启
systemctl enable mysql #开机自启