原创

MySQL 8.0 单机安装部署

环境要求

操作系统:centos7

mysql 8.0安装包:下载地址

jdk: jdk1.8

安装

创建工作组

groupadd mysql

创建用户

useradd -r -g mysql -s /bin/false mysql

进入安装目录

cd /usr/local

下载压缩包

wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.27-el7-x86_64.tar.gz

解压

tar xvf mysql-8.0.27-el7-x86_64.tar.gz

创建mysql目录

mkdir mysql

将解压文件移动到mysql目录下

cp mysql-8.0.27-el7-x86_64/* mysql

改变目录的操作用户

cd mysql
chown mysql:mysql mysql-files

给指定目录授权

chmod 750 mysql-files

初始化并获取初始密码

bin/mysqld --initialize --user=mysql

ssl支持安装

bin/mysql_ssl_rsa_setup

添加启动关闭指令

cp support-files/mysql.server /etc/init.d/mysql.server
systemctl enable mysql

配置

配置mysql配置文件

 vi /etc/my.cnf

配置文件内容

[mysqld]
server-id=1
port=3306 #端口
user=mysql
bind-address=0.0.0.0 #访问ip控制


log-bin=mysql-bin # 开启 binlog
binlog-format=ROW # 选择 ROW 模式

启动

nohup /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf > /dev/null 2>&1 &

后续改变配置并重启

vi /etc/my.cnf
systemctl restart mysql

注意事项

  1. 启动成功外部ip无法访问?

    如果是自己学习使用的话,直接把防火墙关闭即可。

    停止防火墙

    systemctl stop firewalld
    

    禁用防火墙

    systemctl disable firewalld
    

    如果是生产中,建议在防火墙配置暴漏端口(3306端口)

    开放3306端口

    firewall-cmd --zone=public --add-port=3306/tcp --permanent
    

    更新规则

    firewall-cmd --reload
    

    如果对防火墙相关指令不熟悉的话 可以参考 防火墙相关指令

  2. 更新完my.cnf 启动失败?

    多半是配置错误的问题,这里提供一个参考模板

    ```
    [client]
    default-character-set = utf8mb4
    port = PORT
    socket = /srv/myPORT/run/mysql.sock

    [mysqld]
    user = mysql
    port = PORT
    extra-port = 1PORT
    character_set_server = utf8mb4
    server_id = SERVER_ID
    socket = /srv/myPORT/run/mysql.sock
    basedir = /opt/percona-server
    datadir = /srv/myPORT/data
    log-error = /srv/myPORT/log/alert.log
    pid-file = /srv/myPORT/run/mysql.pid

    --------------二进制日志相关-----------------------------------------
    log_slave_updates = 1
    log-bin = /srv/myPORT/log/mysql-bin
    binlog_format = row
    binlog_cache_size = 2M
    max_binlog_cache_size = 4G
    max_binlog_size = 1G
    expire_logs_days = 7
    --------------中继日志相关------------------------------------------
    relay-log = /srv/myPORT/log/relay-bin
    relay-log-info-file = /srv/myPORT/log/relay-log.info
    relay-log-index = /srv/myPORT/log/relay-log.index
    master-info-file = /srv/myPORT/log/master.info
    relay-log-recovery = true
    sync-relay-log-info = 1

    --------------全局缓存----------------------------------------------
    key_buffer_size = 2M
    query_cache_size = 1M
    query_cache_limit = 1M
    query_cache_min_res_unit = 2k
    table_open_cache = 2048

    --------------线程缓存----------------------------------------------
    sort_buffer_size = 2M
    read_buffer_size = 2M
    read_rnd_buffer_size = 16M
    join_buffer_size = 2M
    thread_cache_size = 1024
    tmp_table_size = 16M
    thread_stack = 192k
    bulk_insert_buffer_size = 64M
    max_heap_table_size = 64M

--------------连接相关-----------------------------------------------
back_log = 3000
max_connections = 4000
max_user_connections = 2500
max_connect_errors = 100

--------------慢查询相关--------------------------------------------
long_query_time = 0.5
slow_query_log = on
slow_query_log_file = /srv/myPORT/log/slow.log

--------------系统级别参数-------------------------------------------
skip-name-resolve
explicit_defaults_for_timestamp
open_files_limit = 65535
max_allowed_packet = 512M
default-storage-engine = INNODB
transaction_isolation = READ-COMMITTED

----------------Innodb相关--------------------------------------------
innodb_buffer_pool_size = 10G
innodb_data_file_path = ibdata1:2G:autoextend
innodb_file_io_threads = 24
innodb_purge_threads = 12
innodb_read_io_threads = 24
innodb_write_io_threads = 24
innodb_thread_concurrency = 24
innodb_buffer_pool_instances = 8
innodb_flush_log_at_trx_commit= 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 2G
innodb_log_files_in_group = 2
innodb_max_dirty_pages_pct = 75
innodb_lock_wait_timeout = 50
innodb_file_per_table = 1

----------------线程相关------------------------------------------------
thread_handling = pool-of-threads
thread_pool_idle_timeout = 180
thread_pool_max_threads = 5000
thread_pool_size = 24
thread_pool_oversubscribe = 10
thread_pool_stall_limit = 500

[mysqldump]
quick
max_allow_packet = 64M

[mysql]
no-auto-rehash

[mysqlhotcopy]
interactive-timeout
```

正文到此结束