• Mysql 5.7 安装及备份


    > 环境 centos 7.5

    1. 下载 Mysql 5.7 和 boost 源码包

    ```bash
    # 创建工作目录
    mkdir -p /data/mysql/build && cd /data/mysql/build

    # 下载 mysql 5.7 和 boost 源码
    yum install -y wget
    wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.23.tar.gz
    wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz

    ```

    2. 系统更新以及编译依赖

    ```bash
    yum update -y && yum -y install gcc gcc-c++ ncurses ncurses-devel cmake bison
    ```

    3. 创建 mysql 系统用户组

    ```bash
    groupadd -r mysql && useradd -r -g mysql -s /sbin/nologin -M mysql
    ```

    4. 预编译 mysql

    ```bash
    tar zxvf mysql-5.7.23.tar.gz && tar zxvf boost_1_59_0.tar.gz
    cd mysql-5.7.23
    # 开始预编译
    cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql
    -DMYSQL_DATADIR=/data/mysql
    -DDOWNLOAD_BOOST=1
    -DWITH_BOOST=../boost_1_59_0
    -DSYSCONFDIR=/etc
    -DWITH_INNOBASE_STORAGE_ENGINE=1
    -DWITH_PARTITION_STORAGE_ENGINE=1
    -DWITH_FEDERATED_STORAGE_ENGINE=1
    -DWITH_BLACKHOLE_STORAGE_ENGINE=1
    -DWITH_MYISAM_STORAGE_ENGINE=1
    -DENABLED_LOCAL_INFILE=1
    -DENABLE_DTRACE=0
    -DDEFAULT_CHARSET=utf8mb4
    -DDEFAULT_COLLATION=utf8mb4_unicode_ci
    -DWITH_EMBEDDED_SERVER=1
    ```

    5. 编译安装

    ```bash
    # 这里使用多核心编译,提高编译速度
    make -j `grep processor /proc/cpuinfo | wc -l`
    make install
    ```

    6. 创建 mysql.cnf 配置文件

    ```shell
    # 此配置情况修改
    cat > /etc/my.cnf << EOF
    [client]
    port = 3306
    socket = /tmp/mysql.sock
    default-character-set = utf8mb4

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

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

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

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

    max_connections = 1000
    max_connect_errors = 6000
    open_files_limit = 65535
    table_open_cache = 128
    max_allowed_packet = 4M
    binlog_cache_size = 8M
    max_binlog_cache_size=512M
    max_binlog_size=512M
    max_heap_table_size = 8M
    tmp_table_size = 16M

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

    thread_cache_size = 8

    query_cache_type = 1
    query_cache_size = 8M
    query_cache_limit = 2M

    ft_min_word_len = 4
    log_bin = /data/mysql/logbin.log
    log_bin_index=/data/mysql/logindex
    binlog_format = ROW
    expire_logs_days = 30

    log_error = /data/mysql/mysql-error.log
    slow_query_log = 1
    long_query_time = 1
    slow_query_log_file = /data/mysql/mysql-slow.log

    performance_schema = 0
    explicit_defaults_for_timestamp

    #lower_case_table_names = 1
    skip-external-locking

    default_storage_engine = InnoDB
    #default-storage-engine = MyISAM
    innodb_file_per_table = 1
    innodb_open_files = 500
    innodb_buffer_pool_size = 64M
    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_log_buffer_size = 2M
    innodb_log_file_size = 32M
    innodb_log_files_in_group = 3
    innodb_max_dirty_pages_pct = 90
    innodb_lock_wait_timeout = 120

    bulk_insert_buffer_size = 8M
    myisam_sort_buffer_size = 8M
    myisam_max_sort_file_size = 10G
    myisam_repair_threads = 1

    interactive_timeout = 28800
    wait_timeout = 28800

    [mysqldump]
    quick
    max_allowed_packet = 16M

    [myisamchk]
    key_buffer_size = 8M
    sort_buffer_size = 8M
    read_buffer = 4M
    write_buffer = 4M
    EOF
    ```

    7. 初始化数据库

    ```bash
    # 清空 /data/mysql 文件夹,用来保存 mysql 初始数据
    cd /data && rm -rf /data/mysql/build
    # 在 /data/mysql 下初始化 mysql 数据库
    /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
    ```

    8. 查找 mysql 默认密码

    ```shell
    # 查找到的字符即为 mysql 默认密码
    grep -o "A temporary password is generated for root@localhost:.*" mysql/mysql-error.log | cut -d ":" -f2
    ```

    9. mysql 启动脚本

    ```bash
    cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
    chmod +x /etc/init.d/mysqld
    # 设置开机自启 mysql
    systemctl enable mysqld
    # 启动 mysql 进程
    systemctl start mysqld
    ```

    10. mysql 环境变量

    ```bash
    echo PATH=${PATH}:/usr/local/mysql/bin/ >> /etc/profile
    source /etc/profile
    ```

    11. 开启远程登录和密码修改(出于安全考虑,请自行斟酌)

    ```bash
    # 修改 mysql 默认密码
    mysql -u root -p

    # 设置新密码为 123456
    mysql> set password for 'root'@'localhost'=password('123456');

    # 创建一个远程 root 账户 密码为 mypassword
    mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;

    # 刷新 权限
    mysql> FLUSH PRIVILEGES;
    ```

    12. mysql 增量和全量备份

    ```shell
    # 创建备份目录, 备份脚本
    mkdir -p /data/mysql_backup/daily

    # 全量备份脚本 ---BEGIN
    cat > /data/mysql_backup/backup_all.sh <<-'EOF'
    #!/bin/bash
    export LANG=en_US.UTF-8
    # 备份目录
    BackupDir=/data/mysql_backup
    # 备份日志
    LogFile=${BackupDir}/bak.log
    # 当前日期
    Date=`date +%Y%m%d`
    # 开始时间
    Begin=`date +"%Y年%m月%d日 %H:%M:%S"`

    cd $BackupDir
    DumpFile=$Date.sql
    GZDumpFile=$Date.sql.tgz
    # 全量备份 数据库,重新建立 binlog, 建议使用非 root 用户进行备份
    mysqldump -ubackup -p123456 --all-databases --flush-logs --delete-master-logs --single-transaction > $DumpFile

    # 打包压缩
    tar -czvf $GZDumpFile $DumpFile
    rm $DumpFile



    count=$(ls -l *.tgz |wc -l)
    if [ $count -ge 7 ]
    then
    file=$(ls -l *.tgz |awk '{print $9}'|awk 'NR==1')
    rm -f $file
    fi

    # 结束时间
    Last=`date +"%Y年%m月%d日 %H:%M:%S"`

    # 写入日志文件
    echo 开始:$Begin 结束:$Last $GZDumpFile succ >> $LogFile

    # 删除以前的增量备份文件
    cd $BackupDir/daily
    rm -f *
    EOF
    # 全量备份脚本 --END


    # 增量备份脚本 --BEGIN
    cat > /data/mysql_backup/backup_inc.sh <<-'EOF'

    #!/bin/bash
    export LANG=en_US.UTF-8
    # 工作目录
    WorkDir=/data/mysql_backup

    # 增量备份目录
    BackupDir=${WorkDir}/daily

    # mysql bin log 日志文件所在目录
    BinDir=/data/mysql
    LogFile=${WorkDir}/binlog.log

    # bin log 索引文件
    BinFile=/data/mysql/logindex.index

    #这个是用于产生新的mysql-bin.00000*文件 ,建议使用非 root 用户执行
    mysqladmin -ubackup -p123456 flush-logs

    Counter=`wc -l $BinFile |awk '{print $1}'`
    NextNum=0
    #这个for循环用于比对$Counter,$NextNum这两个值来确定文件是不是存在或最新的。
    for file in `cat $BinFile`
    do
    base=`basename $file`
    #basename用于截取mysql-bin.00000*文件名,去掉./mysql-bin.000005前面的./
    NextNum=`expr $NextNum + 1`
    if [ $NextNum -eq $Counter ]
    then
    echo $base skip! >> $LogFile
    else
    dest=$BackupDir/$base
    if(test -e $dest)
    #test -e用于检测目标文件是否存在,存在就写exist!到$LogFile去。
    then
    echo $base exist! >> $LogFile
    else
    cp $BinDir/$base $BackupDir
    echo $base copying >> $LogFile
    fi
    fi
    done
    echo `date +"%Y年%m月%d日 %H:%M:%S"` Bakup succ! >> $LogFile
    EOF
    # 增量备份脚本 --END

    # 创建 mysql backup 最小权限用户用于备份操作
    mysql> CREATE USER 'backup'@'localhost';
    mysql> ALTER USER 'backup'@'localhost' IDENTIFIED BY '123456';
    mysql> grant lock tables,reload,process,replication client,super,select,event,trigger,show view on *.* to backup@'localhost';
    mysql> FLUSH PRIVILEGES;

    # crontab -e 添加以下任务计划
    #-------------- BEGIN ---------------

    #每个星期日凌晨3:00执行完全备份脚本
    0 3 * * 0 /data/mysql_backup/backup_all.sh >/dev/null 2>&1
    #周一到周六凌晨3:00做增量备份
    0 3 * * 1-6 /data/mysql_backup/backup_inc.sh >/dev/null 2>&1

    #--------------- END ----------------
    ```

  • 相关阅读:
    INNODB在裸设备上的性能简单测试
    java interface 不能存常量数组?
    jfreechart linux图片中文显示乱码解决方法
    linux下tomcat工程从oracle读取中文出现乱码
    java读取文件输出流出现的问题
    运用mysqldump 工具时注意的问题
    ibatis中字段名问题
    oracle 定长字段查询问题 ,ibatis 与pl/sql查询的char类型字段查询不同
    ibatis #跟$的区别
    C#取整相关
  • 原文地址:https://www.cnblogs.com/Christine-ting/p/10697708.html
Copyright © 2020-2023  润新知