Mysql5.7.33安装

Mysql5.7

安装部署文档

(V1.0)

 

 

1.用户创建

groupadd mysql

useradd -g mysql mysql

2.创建目录

创建软件及data目录

mkdir -p /u01

chown mysql:mysql /u01

(可与根目录一起)

mkdir -p /data/mysql3306

chown -R mysql:mysql /data

(单独划盘)

3.上传软件

建议使用5.7.33最高版本

如果业务中需要使用分析函数及大表关联查询[hash join]等可以使用mysql8最高版本

上传二进制安装包:

tar zxvf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz

mv mysql-5.7.33-linux-glibc2.12-x86_64/ mysql/

chown mysq:mysql mysql/

  1. 操作系统限制

    cat >> /etc/security/limits.conf <<EOF

    #use for mysql

    mysql soft nproc 65536

    mysql hard nproc 65536

    mysql soft nofile 65536

    mysql hard nofile 65536

    EOF

    5.配置Mysql用户环境变量

    su - mysql

    vi .bash_profile

    export PATH

    export MYSQL_HOME=/u01/mysql

    export PATH=$MYSQL_HOME/bin:$PATH

  2. 配置mysql参数文件

    vi /etc/my.cnf

    [client]

    port    = 3306

    socket    = /data/mysql3306/mysql.sock

    [mysqld]

    user    = mysql

    port    = 3306

    basedir    = /u01/mysql

    datadir    = /data/mysql3306

    socket    = /data/mysql3306/mysql.sock

    pid-file = .pid

    character-set-server = utf8mb4

    skip_name_resolve = 1

    default_time_zone = "+8:00"

    server-id = 3306

    lower_case_table_names=1

    open_files_limit = 6553

    back_log = 1024

    max_connections = 800

    max_connect_errors = 1000000

    table_open_cache = 1024

    table_definition_cache = 1024

    table_open_cache_instances = 64

    thread_stack = 512K

    external-locking = FALSE

    max_allowed_packet = 32M

    sort_buffer_size = 16M

    join_buffer_size = 16M

    thread_cache_size = 768

    interactive_timeout = 1800

    wait_timeout = 1800

    tmp_table_size = 96M

    max_heap_table_size = 96M

    key_buffer_size = 32M

    read_buffer_size = 8M

    read_rnd_buffer_size = 16M

    bulk_insert_buffer_size = 64M

    myisam_sort_buffer_size = 128M

    myisam_max_sort_file_size = 10G

    myisam_repair_threads = 1

    lock_wait_timeout = 300

    explicit_defaults_for_timestamp = 1

    ##日志##

    slow_query_log = 1

    log_timestamps = SYSTEM

    slow_query_log_file = /data/mysql3306/slow.log

    log-error = /data/mysql3306/error.log

    long_query_time = 5

    log_queries_not_using_indexes =0

    log_throttle_queries_not_using_indexes = 60

    min_examined_row_limit = 100

    log_slow_admin_statements = 1

    log_slow_slave_statements = 1

    ##binlog设置##

    log-bin = /data/mysql3306/mysql-bin

    sync_binlog = 1

    binlog_cache_size = 4M

    max_binlog_cache_size = 2G

    max_binlog_size = 1G

    binlog_format = row

    binlog_checksum = 1

    binlog_rows_query_log_events = 1

    expire_logs_days = 5

    ##开启GTID##

    gtid_mode = on

    enforce_gtid_consistency = 1

    ##slave相关设置##

    master_info_repository = TABLE

    relay_log_info_repository = TABLE

    log_slave_updates

    slave-rows-search-algorithms = 'INDEX_SCAN,HASH_SCAN'

    relay_log_recovery = 1

    relay-log-purge = 1

    ##innodb相关配置##

    innodb_thread_concurrency = 0

    transaction_isolation = READ-COMMITTED

    ##按需分配操作系统内存得60%##

    innodb_buffer_pool_size = 1G

    innodb_buffer_pool_instances = 4

    innodb_buffer_pool_load_at_startup = 1

    innodb_buffer_pool_dump_at_shutdown = 1

    innodb_data_file_path = ibdata1:1G:autoextend

    innodb_flush_log_at_trx_commit = 1

    innodb_log_buffer_size = 32M

    innodb_log_file_size = 2G

    innodb_log_files_in_group = 3

    innodb_max_undo_log_size = 4G

    innodb_undo_tablespaces = 5

    innodb_undo_log_truncate = 1

    innodb_undo_logs = 128

    ##普通磁盘##

    innodb_io_capacity = 4000

    innodb_io_capacity_max = 8000

    innodb_flush_sync = 0

    innodb_flush_neighbors = 0

    innodb_write_io_threads = 8

    innodb_read_io_threads = 8

    innodb_purge_threads = 4

    innodb_page_cleaners = 4

    innodb_open_files = 65535

    innodb_max_dirty_pages_pct = 75

    innodb_flush_method = O_DIRECT

    innodb_lru_scan_depth = 4000

    innodb_checksum_algorithm = crc32

    innodb_lock_wait_timeout = 10

    innodb_rollback_on_timeout = 1

    innodb_print_all_deadlocks = 1

    innodb_file_per_table = 1

    innodb_online_alter_log_max_size = 4G

    innodb_stats_on_metadata = 0

    internal_tmp_disk_storage_engine = InnoDB

    innodb_checksums = 1

    ##关闭查询缓存##

    query_cache_size = 0

    query_cache_type = 0

    innodb_status_file = 1

    innodb_status_output = 0

    innodb_status_output_locks = 0

    innodb_sort_buffer_size = 67108864

    innodb_autoinc_lock_mode = 1

    performance_schema = 1

    [mysqldump]

    quick

    max_allowed_packet = 32M

  3. 初始化数据库

    ##initialize-insecure参数作用:初始化数据库root密码为空

    cd /u01/mysql/bin

    mysqld --defaults-file=/etc/my.cnf --initialize-insecure --user=mysql

    启动数据库:

    /etc/init.d/mysqld start

    OR

    mysqld_safe --defaults-file=/etc/my.cnf&

    复制数据库启动脚本

    cp /u01/mysql/support-files/mysql.server /etc/init.d/mysqld

    编辑启动脚本:

    sed -i '46s/basedir=/basedir=/u01/mysql/' /etc/init.d/mysqld

    sed -i '47s/datadir=/datadir=/data/mysql3306/' /etc/init.d/mysqld

    添加开机启动项:

    chkconfig --add mysqld

    chkconfig --level 35 mysqld on

  4. xtrabackup备份数据库

    上传二进制安装包

    tar zxvf percona-xtrabackup-2.4.8-Linux-x86_64.tar.gz

    mv percona-xtrabackup-2.4.8-Linux-x86_64/ percona-xtrabackup/

    配置环境变量:

    vi .bash_profile

    export PATH

    export MYSQL_HOME=/u01/mysql

    export XTRABACKUP_HOME=/u01/percona-xtrabackup

    export PATH=$MYSQL_HOME/bin:$XTRABACKUP_HOME/bin:$PATH

    创建备份脚本:

    touch /home/mysql/script/backup.sh

    本地磁盘备份:

    #!/bin/bash

    source /home/mysql/.bash_profile

    day=$(date +%Y%m%d)

    dir=/data/backup

    find $dir -mtime +2 |xargs rm -rf

    mkdir -p $dir/$day

    xtrabackup --user=root --password=monitor  --backup --parallel=4   --compress --compress-threads=4 --target-dir=$dir/$day

    远程备份:

    #!/bin/sh

    source /root/.bash_profile

    day=$(date +%Y%m%d)

    dir=/usr/backup/xx

    ssh root@10.50.13.53 "find $dir -mtime +2 |xargs rm -rf"

    ssh root@10.50.13.53 "mkdir -p $dir/$day"

    xtrabackup --user=root --password=root151 --stream=xbstream --kill-long-query-type=select --kill-long-queries-timeout=1000 --backup --parallel=6 --compress --compress-threads=6 | ssh root@10.50.13.53 "xbstream -x -C $dir/$day"

    Cron自动执行:

    echo "30 1 * * * /bin/sh /home/mysql/script/backup.sh > /home/mysql/script/backup.log" >> /var/spool/cron/mysql

原文地址:https://www.cnblogs.com/muzisanshi/p/14922640.html