CentOS7.X yum安装MySQL

yum安装mysql

  1. 安装前的准备

    yum install \
    vim \
    wget \
    -y
  2. 获取源码资源包

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

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

    yum install mysql-server -y
  5. 修改my.conf

    vim /etc/my.cnf
    
    [client]
    default-character-set = utf8mb4
    
    [mysql]
    default-character-set = utf8mb4
    
    [mysqld]
    datadir=/data/mysql
    character-set-server=utf8mb4
    init_connect='SET NAMES utf8mb4'
    collation-server=utf8mb4_unicode_ci
    character-set-client-handshake=FALSE
    
    symbolic-links=0
    log-error=/var/log/mysqld.log
    socket=/var/lib/mysql/mysql.sock
    pid-file=/var/run/mysqld/mysqld.pid
    secure_file_priv=/var/lib/mysql-files
    
    server-id=1
    binlog-format=Row
    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
  6. 创建数据文件目录、所属组、用户改为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*
  7. 修改SELinux

    vim /etc/sysconfig/selinux
    
    SELINUX=permissive
    
    ESC
    :wq
    
    setenforce 0
  8. 启动数据库,生成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: **************
  9. 修改数据库初始密码

    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;
  10. 初始化数据库(除了密码一律选择Y)

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

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

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

    # 数据库名-通配符及格式:
        *[允许操作所有数据库]
        %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;
  14. 领支付宝红包支持作者

    CentOS7.X yum安装MySQL

上一篇:CentOS7.X安装postgresql-10.3


下一篇:CentOS7.X安装php-7.x.x