MySQL常用的安装方式有三种,yum源直接下载安装、编译好的二进制包安装、源码编译安装。无论生产还是测试,最常用的安装方式,基本上都是二进制包的方式,简单快捷,满足了多数需求。
MySQL官方下载地址:https://downloads.mysql.com/archives/community/
环境准备
1.安装MySQL需要的依赖,libaio,没有这个MySQL安装后就起不来了。
root # yum install -y libaio
2.创建相关目录和用户并授权
root # mkdir -p /data/mysql/data
root # mkdir -p /data/mysql/base
root # mkdir /data/mysql/etc
root # mkdir -p /data/mysql/log/binlog
root # mkdir /data/mysql/tmp
# 创建mysql用户,但不允许登录
root # groupadd mysql
root # useradd -r -g mysql -s /bin/false mysql
# 授权目录
chown -R mysql. /data/mysql
安装mysql
1.下载mysql二进制包,并解压到base目录下。
root # cd /opt
root # wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.44-el7-x86_64.tar.gz
root # tar xf mysql-5.7.44-el7-x86_64.tar.gz -C /data/mysql/base/
root # cd /data/mysql/base/
root # mv mysql-5.7.44-el7-x86_64/ 5.7.44
2.配置mysql的环境变量
root # vim /etc/profile
...
export MYSQL_HOME=/data/mysql/base/5.7.44
export PATH=$PATH:$MYSQL_HOME/bin
#使环境变量生效
root # source /etc/profile
3.在mysql的etc目录下添加my.cnf文件,并实际情况修改内容
root # vim /data/mysql/etc/my.cnf
[client]
port = 3306
user = root
socket = /data/mysql/data/mysql_3306.sock
[mysql]
prompt ='[\c][Conn:\C][\R:\\m:\\s][\U][\d]> '
default-character-set = utf8mb4
#prompt ='\U[\d]> '
tee = /data/mysql/data/mysql_operation.log
[mysqld]
# Base Config
server_id = 4673
basedir = /data/mysql/base/5.7.44
datadir = /data/mysql/data
tmpdir = /data/mysql/tmp
secure_file_priv = /data/mysql/tmp
pid-file = mysqld.pid
socket = /data/mysql/data/mysql_3306.sock
user = mysql
port = 3306
default_storage_engine = InnoDB
character_set_server = utf8mb4
skip_slave_start = 1
skip-name-resolve = 1
skip-external-locking = 1
lower_case_table_names = 1
query_cache_type = 0
query_cache_size = 0
max_connections = 1000
default-time-zone = '+8:00'
log_timestamps = SYSTEM
# InnoDB config
innodb_strict_mode = 1
innodb_file_per_table = 1
innodb_stats_on_metadata = 0
innodb_flush_method = O_DIRECT
innodb_log_files_in_group = 3
innodb_data_file_path = ibdata1:1G:autoextend
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
innodb_log_buffer_size = 32M
innodb_max_dirty_pages_pct = 60
innodb_io_capacity = 400
innodb_buffer_pool_instances = 8
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_undo_logs = 128
innodb_undo_tablespaces = 3
innodb_flush_neighbors = 1
# Cache config
key_buffer_size = 8M
tmp_table_size = 32M
max_heap_table_size = 32M
thread_cache_size = 1000
table_open_cache = 2048
open_files_limit = 65535
max_allowed_packet = 32M
# Log config
log_error = mysql-error.log
slow_query_log_file = mysql-slow.log
relay-log = mysql-relay
log-bin = mysql-bin
slow_query_log = 1
long_query_time = 0.2
#log_slow_admin_statements = 1
#log_slow_slave_statements = 1
# Semisync config
plugin-load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_slave_enabled = 1
# Replication config
slave-parallel-type = LOGICAL_CLOCK
slave-parallel-workers = 8
expire_logs_days = 14
binlog_format = row
log_slave_updates = ON
binlog_checksum = NONE
max_binlog_size = 250M
binlog_cache_size = 2M
sync_binlog = 1
innodb_flush_log_at_trx_commit = 1
relay-log-info-repository = TABLE
master_info_repository = TABLE
relay_log_recovery = 1
binlog_rows_query_log_events = 1
log_bin_trust_function_creators = 1
# GTID
gtid-mode = ON
enforce-gtid-consistency = 1
# Performance Schema
performance-schema-instrument = 'wait/lock/metadata/sql/mdl=ON'
4.初始化数据目录,初始化mysql数据目录有两种方式,一种带mysql管理用户root的随机密码,一种是空密码。
# 无密码
root # mysqld --defaults-file=/data/mysql/etc/my.cnf --initialize-insecure --user=mysql
# 有密码,密码在mysql data目录下的error log中。
mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql
启动mysql
1.配置mysql启动文件
root # vim /etc/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld
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
Type=forking
PIDFile=/data/mysql/data/mysqld.pid
TimeoutSec=0
ExecStart=/data/mysql/base/5.7.44/bin/mysqld --defaults-file=/data/mysql/etc/my.cnf --daemonize $MYSQLD_OPTS
EnvironmentFile=-/etc/sysconfig/mysql
LimitNOFILE = 65535
Restart=on-failure
RestartPreventExitStatus=1
Environment=MYSQLD_PARENT_PID=1
PrivateTmp=false
2.启动mysql,加入开机自启,并连接测试
root # systemctl start mysqld.service
root # systemctl enable mysqld
# 连接测试
[root hxy /root] # mysql -uroot -p -S /data/mysql/data/mysql_3306.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44-log MySQL Community Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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>