AlmaLinux 单机部署Mysql 8.0

下载并安装Jemalloc

wget https://github.com/jemalloc/jemalloc/archive/5.3.0.tar.gz -O jemalloc-5.3.0.tar.gz
tar zxvf jemalloc-5.3.0.tar.gz
cd jemalloc-5.3.0/
./autogen.sh
make
make install
cd ../
rm -rf jemalloc-5.3.0
ln -s /usr/local/lib/libjemalloc.so.2 /usr/lib64/libjemalloc.so.2

下载Percona Server 8.0

wget https://downloads.percona.com/downloads/Percona-Server-8.0/Percona-Server-8.0.36-28/binary/tarball/Percona-Server-8.0.36-28-Linux.x86_64.glibc2.34-minimal.tar.gz

创建mysql用户与组

/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql

安装Percona Server 8.0

tar zxvf Percona-Server-8.0.36-28-Linux.x86_64.glibc2.34-minimal.tar.gz
mv Percona-Server-8.0.36-28-Linux.x86_64.glibc2.34-minimal /usr/local/mysql
ln -s /usr/local/mysql/lib/libperconaserverclient.so.21 /usr/lib/libperconaserverclient.so.21
echo -e '\n\nexport PATH=/usr/local/mysql/bin:$PATH\n' >> /etc/profile && source /etc/profile

创建数据库存放目录

mkdir -p /data0/mysql/3306/data/
mkdir -p /data0/mysql/3306/binlog/
chown -R mysql:mysql /data0/mysql/

创建my.cnf配置文件

vi /usr/local/mysql/my.cnf

输入以下内容,示例配置适合2G内存的服务器

[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8mb4

[mysql]
prompt="Percona [\\d]> "
no-auto-rehash
#safe-updates

[mysqld]
port = 3306
socket = /tmp/mysql.sock
authentication_policy = mysql_native_password

basedir = /usr/local/mysql
datadir = /data0/mysql/3306/data
pid-file = /data0/mysql/3306/mysqld.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1

log_error = /data0/mysql/3306/mysql-error.log
log_timestamps = SYSTEM
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data1/mysql/3306/mysql-slow.log

#sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#transaction_isolation = REPEATABLE-READ

init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
collation-server = utf8mb4_0900_ai_ci

skip-name-resolve
#skip-networking
back_log = 300

max_connections = 2000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 256
max_allowed_packet = 1G
max_heap_table_size = 32M
tmp_table_size = 32M

read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 16M

thread_cache_size = 16
thread_stack = 512K

ft_min_word_len = 4

#disable-log-bin
log_bin = /data0/mysql/3306/binlog/mysql-bin
binlog_cache_size = 1M
binlog_expire_logs_seconds = 604800
max_binlog_cache_size = 10240M
max_binlog_size = 512M

#log_replica_updates = 1
#gtid_mode = 3
#enforce_gtid_consistency = 1

performance_schema = 0
explicit_defaults_for_timestamp

#lower_case_table_names = 1

skip-external-locking

default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_open_files = 512
innodb_buffer_pool_size = 1G
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_redo_log_capacity = 500M
innodb_log_buffer_size = 2M
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120

bulk_insert_buffer_size = 16M
myisam_sort_buffer_size = 16M
myisam_max_sort_file_size = 10G

interactive_timeout = 28800
wait_timeout = 28800

mysqlx = 0
mysqlx_port = 33060
mysqlx_socket = /tmp/mysqlx.sock
mysqlx_bind_address = 127.0.0.1,::1

[mysqldump]
quick
max_allowed_packet = 1G

[myisamchk]
key_buffer_size = 16M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M

如何根据内存大小调整配置文件数值?

一般情况下,max_connections为内存1~0.5倍即可,例如内存是2048M,那么max_connections设置为1000~2000即可

当内存大小在1500到2500(可能的单位是MB)之间时,设置以下参数:
thread_cache_size = 16
myisam_sort_buffer_size = 16M
key_buffer_size = 16M
innodb_buffer_pool_size = 128M
tmp_table_size = 32M
table_open_cache = 256

当内存大小在2500到3500之间时,设置以下参数:
thread_cache_size = 32
myisam_sort_buffer_size = 32M
key_buffer_size = 64M
innodb_buffer_pool_size = 512M
tmp_table_size = 64M
table_open_cache = 512

当内存大小超过3500时,设置以下参数:
thread_cache_size = 64
myisam_sort_buffer_size = 64M
key_buffer_size = 256M
innodb_buffer_pool_size = 1024M
tmp_table_size = 128M
table_open_cache = 1024

(内存超过8G自己研究,有这个需求的人也不会看这个教程的)

初始化数据库

/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data0/mysql/3306/data

创建服务脚本

vi /usr/lib/systemd/system/mysqld.service

输入以下内容

[Unit]
Description=MySQL Server
Documentation=man:mysqld(7)
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=/data0/mysql/3306/mysqld.pid

# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0

# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/my.cnf --daemonize $MYSQLD_OPTS

# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql

# Sets open_files_limit
LimitNOFILE=65535

# Sets core_file_limit
LimitCORE=65535

Restart=on-failure

RestartPreventExitStatus=1

PrivateTmp=false

加载Jemalloc动态库

echo "LD_PRELOAD=/usr/local/lib/libjemalloc.so.2" > /etc/sysconfig/mysql

启动服务

systemctl enable mysqld
systemctl start mysqld

设置数据库root用户密码

#友情提醒:最后的密码就不要跟着复制粘贴了
/usr/local/webserver/mysql/bin/mysqladmin password 12345678

至此Mysql 8.0单机部署完成