Linux安装mysql(yum)

  1. 作者QQ:67065435 QQ群:821635552

  2. 本站内容全部为作者原创,转载请注明出处!

  3. 安装前的准备

    yum install \
    vim \
    git \
    gcc \
    gcc-c++ \
    wget \
    make \
    cmake \
    automake \
    autoconf \
    libaio \
    libtool \
    net-tools \
    bison-devel \
    libaio-devel \
    ncurses-devel \
    perl-Data-Dumper \
    -y
    
  4. 获取源码资源包

    wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm
    
  5. 安装源码资源包

    rpm -ivh mysql57-community-release-el7-10.noarch.rpm
    
  6. 通过资源包安装mysql

    yum install mysql-server -y
    
  7. 修改文件打开数上限

    # 系统文件打开数上限
    vim /etc/security/limits.conf
    
    * soft nproc 1024000
    * hard nproc 1024000
    * soft nofile 1024000
    * hard nofile 1024000
    
    ESC
    :wq
    
    # MySQL文件打开数上限
    vim /usr/lib/systemd/system/mysqld.service
    
    LimitNOFILE=1024000
    
    ESC
    :wq
    
    systemctl daemon-reload
    
  8. 修改my.conf

    vim /etc/my.cnf
    
    [client]
    default-character-set = utf8mb4
    
    [mysql]
    default-character-set = utf8mb4
    
    [mysqld]
    max_connections = 512
    tmp_table_size = 256M
    key_buffer_size = 512M
    sort_buffer_size = 64M
    read_buffer_size = 64M
    thread_cache_size = 64
    join_buffer_size = 128M
    sort_buffer_size = 128M
    query_cache_size = 512M
    table_open_cache = 4096M
    read_rnd_buffer_size = 128M
    innodb_buffer_pool_size = 128M
    datadir=/data/mysql
    log_timestamps = SYSTEM
    default_storage_engine = InnoDB
    character-set-server=utf8mb4
    init_connect='SET NAMES utf8mb4'
    collation-server=utf8mb4_unicode_ci
    character-set-client-handshake=FALSE
    
    symbolic-links=0
    log-error=/data/mysql/mysqld.log
    socket=/data/mysql/mysql.sock
    pid-file=/data/mysql/mysqld.pid
    secure_file_priv=/var/lib/mysql-files
    
    server-id=1
    binlog-format=Row
    expire_logs_days=10
    log-bin=/data/mysql/mysql-bin
    sql-mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
    
    ESC
    :wq
    
  9. 创建数据文件目录、所属组、用户改为mysql

    mkdir /data
    mkdir /data/mysql
    chown -R mysql:mysql /etc/my.cnf
    chown -R mysql:mysql /data/mysql/
    chown -R mysql:mysql /var/*/mysql*
    
  10. 修改SELinux

    vim /etc/sysconfig/selinux
    
    SELINUX=permissive
    
    ESC
    :wq
    
    setenforce 0
    
  11. 启动数据库,生成socket文件

    systemctl enable mysqld
    systemctl start mysqld
    # 因为配置文件中指定了
    log-error=/var/log/mysqld.log
    # 所以初始密码在如下文件中
    /var/log/mysqld.log
    cat /var/log/mysqld.log|grep password
    # 结果显示
    A temporary password is generated for root@localhost: **************
    
  12. 修改数据库初始密码

    mysql -u root -p
    # 输入原始密码
    **************
    # 设置允许使用弱密码
    SET GLOBAL validate_password_policy=0;
    ALTER USER 'root'@'localhost' IDENTIFIED BY '**************';
    GRANT ALL PRIVILEGES ON *.* TO 'mysql'@'%' IDENTIFIED BY '**************' WITH GRANT OPTION;
    FLUSH PRIVILEGES;
    QUIT;
    
  13. 初始化数据库(除了密码一律选择Y)

    # 如果是数据库【安装】则只执行下面这步
    mysql_secure_installation
    
    # 如果是数据库【升级】则只执行下面这步(**************表示数据库之前的密码)
    mysql_upgrade -u root -p
    **************
    
  14. 配置防火墙

    systemctl enable firewalld
    systemctl start  firewalld
    firewall-cmd --permanent --zone=public --add-port=3306/tcp
    firewall-cmd --reload
    
  15. 测试启动、停止、重启mysql

    #启用并启动
    systemctl enable mysqld
    systemctl start mysqld
    #禁用并关闭
    systemctl disable mysqld
    systemctl stop mysqld
    #启用并重新启动
    systemctl enable mysqld
    systemctl restart mysqld
    
  16. 设置外部访问密码

    # 数据库名-通配符及格式:
        *[允许操作所有数据库]
        %pay%[允许操作名字包含'pay'的数据库]
        mydatabase[仅允许操作'mydatabase'数据库]
    # 表名-通配符及格式:
        *[允许操作所有表]
        %pay%[允许操作名字包含'pay'的表]
        mytable[仅允许操作'mytable'表]
    # ip地址-通配符及格式:
        %/0.0.0.0[允许任意IP访问]
        xxx.xxx.xxx.xxx[仅限公网访问]
        192.168.x.xxx[仅限局域网访问]
        localhost/127.0.0.1/::1[仅限本机访问]
    
    # 数据库授权格式
    GRANT 权限名 ON 数据库名.数据表名 TO '数据用户'@'访问IP' IDENTIFIED BY '用户密码' WITH GRANT OPTION;
    
    # 为任意数据库表 任意操作 分配root用户,仅限本地访问
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '**************' WITH GRANT OPTION;
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY '**************' WITH GRANT OPTION;
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'::1' IDENTIFIED BY '**************' WITH GRANT OPTION;
    
    # 为任意数据库表 任意操作 分配mysql用户,任意IP访问(安装调试阶段、本地开发使用,用后即删)
    GRANT ALL PRIVILEGES ON *.* TO 'mysql'@'%' IDENTIFIED BY '**************' WITH GRANT OPTION;
    
    # 为任意数据库表 增删改查 分配单独用户,为不同的业务分配专属用户
    GRANT INSERT ON *.* TO 'insert1'@'127.0.0.1' IDENTIFIED BY '**************' WITH GRANT OPTION;
    GRANT DELETE ON *.* TO 'delete1'@'127.0.0.1' IDENTIFIED BY '**************' WITH GRANT OPTION;
    GRANT UPDATE ON *.* TO 'update1'@'127.0.0.1' IDENTIFIED BY '**************' WITH GRANT OPTION;
    GRANT SELECT ON *.* TO 'select1'@'127.0.0.1' IDENTIFIED BY '**************' WITH GRANT OPTION;
    
    # 刷新权限
    FLUSH PRIVILEGES;
    
Copyright © 豆包嘿嘿~ 2012-∞ 冀ICP备17033181号 all right reserved,powered by Gitbook修订: 2020-09-15 15:56:54

results matching ""

    No results matching ""