1. 下载官方安装包
进入MySQL官网下载页面
https://downloads.mysql.com/archives/community/
选择需要的版本,这里我们选择:5.7.36,Linux通用,64位。如下所示:
下载完成后将文件传输到linux服务器,准备开始安装
#2. 安装MySQL
注意:
- 本文安装步骤基于RedHat7/CentOS7,并可适用于Debian、Ubuntu等操作系统。
- 本文安装步骤基于3306端口,如果你想使用其他端口启动,手工把端口改为3307即可。
#2.1. 创建目录
#建立MySQL二进制包目录
mkdir -p /servers/packages/mysql5.7.39
#建立MySQL实例相关目录
mkdir -p /servers/data/mysql/3306/log
mkdir -p /servers/data/mysql/3306/binlog
mkdir -p /servers/data/mysql/3306/iblog
mkdir -p /servers/data/mysql/3306/tmpdir
mkdir -p /servers/data/mysql/3306/ibdata
mkdir -p /servers/data/mysql/3306/run/
chmod 755 /servers/data/mysql
#2.2. 解压安装包
tar -zxvf mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.39-linux-glibc2.12-x86_64/* /servers/packages/mysql5.7.39
#2.3. 建立mysql用户并授权
groupadd mysql
useradd -r -s /sbin/nologin -g mysql mysql
chown -R mysql.mysql /servers/data/mysql
#2.4 写初始化配置文件
#写初始配置文件
cat > /servers/data/mysql/3306/auto.cnf << EOF
[mysql]
prompt=\\u@\\v \\d \\r:\\m:\\s>
default-character-set=utf8mb4
[mysqldump]
quick
max_allowed_packet = 512M
[mysqld]
# 目录、端口根据实际情况调整
port = 3306
socket = /servers/data/mysql/3306/run/mysqld.sock
datadir = /servers/data/mysql/3306/data
pid_file = /servers/data/mysql/3306/run/mysqld.pid
log_error = /servers/data/mysql/3306/log/mysqld-err.log
log_bin = /servers/data/mysql/3306/binlog/mysql-bin
relay_log = /servers/data/mysql/3306/binlog/mysqld-relay-bin
innodb_log_group_home_dir = /servers/data/mysql/3306/iblog
tmpdir = /servers/data/mysql/3306/tmpdir
innodb_data_home_dir = /servers/data/mysql/3306/ibdata
slow_query_log_file = /servers/data/mysql/3306/log/mysqld-slow.log
innodb_undo_directory = /servers/data/mysql/3306/iblog
# 每个实例的server_id需要不同
server_id = 10001
# 是否区分大小写
lower_case_table_names = 1
# 字符集及排序规则
collation_server = utf8mb4_bin
character_set_server = utf8mb4
# 隔离级别
transaction-isolation = READ-COMMITTED
# size调整为${MEM} , instance根据size调整,保证每个instance最少1G
innodb_buffer_pool_size = 1500M
innodb_buffer_pool_instances = 1
# 读写线程调整为${CPU/2}
innodb_read_io_threads = 4
innodb_write_io_threads = 4
# 调整为${CPU} ,若你的机器MySQL专用,则调整为 0
innodb_thread_concurrency = 0
bind_address = 0.0.0.0
user = mysql
skip_external_locking
# 跳过域名解析
skip_name_resolve
# 连接相关参数
max_connections = 3000
max_user_connections = 0
max_connect_errors = 2000
# innodb
innodb_strict_mode = 1
innodb_buffer_pool_dump_at_shutdown = 0
innodb_buffer_pool_load_at_startup = 0
innodb_lock_wait_timeout = 20
innodb_sort_buffer_size = 1048576
innodb_stats_persistent_sample_pages = 64
innodb_online_alter_log_max_size = 4G
innodb_rollback_on_timeout = 1
innodb_page_cleaners = 8
innodb_undo_log_truncate = 1
innodb_sync_spin_loops = 30
innodb_spin_wait_delay = 6
innodb_autoinc_lock_mode = 2
innodb_monitor_enable = all
innodb_fill_factor = 70
innodb_flush_neighbors = 0
innodb_io_capacity = 10000
innodb_thread_sleep_delay = 5000
innodb_adaptive_max_sleep_delay = 100000
innodb_max_dirty_pages_pct = 70
innodb_max_purge_lag = 500000
innodb_purge_batch_size = 1000
innodb_adaptive_hash_index_parts = 16
innodb_data_file_path = ibdata1:2G:autoextend
innodb_large_prefix = 1
innodb_stats_auto_recalc = 1
innodb_stats_persistent = 1
innodb_flush_log_at_trx_commit = 1
innodb_buffer_pool_chunk_size = 32M
innodb_undo_tablespaces = 4
innodb_flush_method = O_DIRECT
innodb_log_compressed_pages = 0
innodb_print_all_deadlocks = 1
innodb_purge_threads = 16
innodb_sync_array_size = 64
innodb_open_files = 64535
innodb_log_file_size = 1024M
# replication
binlog_format = row
binlog_cache_size = 32768
max_binlog_size = 512M
max_binlog_cache_size = 4G
expire_logs_days = 10
log_slave_updates = 1
slave_parallel_type = logical_clock
slave_parallel_workers = 64
slave_pending_jobs_size_max = 512M
enforce_gtid_consistency = 1
master_info_repository = table
relay_log_info_repository = table
slave_net_timeout = 60
relay_log_recovery = 1
sync_binlog = 1
master_verify_checksum = 1
sync_master_info = 0
sync_relay_log = 0
sync_relay_log_info = 0
slave_rows_search_algorithms = "INDEX_SCAN,HASH_SCAN"
slave_skip_errors = 1397
table_open_cache = 20480
table_definition_cache = 10240
table_open_cache_instances = 32
open_files_limit = 655340
slow_query_log = 1
long_query_time = 0.1
lock_wait_timeout = 60
thread_stack = 192K
disabled_storage_engines = "MyISAM"
bulk_insert_buffer_size = 64M
performance_schema = 1
max_allowed_packet = 512M
innodb_log_buffer_size = 256M
max_prepared_stmt_count = 200000
gtid_mode = 1
log_timestamps = SYSTEM
thread_cache_size = 8
log_bin_trust_function_creators = 1
log_statements_unsafe_for_binlog = 0
max_heap_table_size = 33554432
max_relay_log_size = 104857600
metadata_locks_hash_instances = 128
tmp_table_size = 33554432
default_storage_engine = innodb
default_tmp_storage_engine = InnoDB
join_buffer_size = 2097152
read_rnd_buffer_size = 2097152
sort_buffer_size = 2097152
read_buffer_size = 2097152
innodb_log_files_in_group = 4
innodb_doublewrite = 1
innodb_lru_scan_depth = 1024
sync_frm = 1
query_alloc_block_size = 16384
query_cache_size = 0
query_prealloc_size = 24576
sql_mode = "NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLE"
local_infile = 0
secure_auth = 1
default_time_zone = SYSTEM
secure_file_priv = ""
skip_ssl = 1
read_only = 0
interactive_timeout = 28800
wait_timeout = 28800
connect_timeout = 10
query_cache_type = 0
log_queries_not_using_indexes = 0
log_slow_admin_statements = 1
log_slow_slave_statements = 1
log_throttle_queries_not_using_indexes = 10
auto_increment_increment = 1
auto_increment_offset = 1
back_log = 1024
EOF
chown -R mysql.mysql /servers/data/mysql
注:最后这步 chown 授权不要漏掉。不然初始化会报错。
#2.5. 初始化MySQL实例
echo "export PATH=$PATH:/servers/packages/mysql5.7.39/bin/" >> /etc/profile
source /etc/profile
mysqld --defaults-file=/servers/data/mysql/3306/auto.cnf --initialize --basedir=/servers/packages/mysql5.7.39
注:
1). 初始化若报错error while loading shared libraries: libnuma.so.1 执行 yum -y install numactl
2). 初始化若报错error while loading shared libraries: libaio.so.1
执行 yum -y install libaio*
3). 初始化完成的密码会输出到/servers/data/mysql/3306/log/mysqld-err.log