美文网首页
10.CentOS7环境安装MariaDB(MySQL开源版)

10.CentOS7环境安装MariaDB(MySQL开源版)

作者: 哈哈大圣 | 来源:发表于2019-11-28 19:32 被阅读0次

    CentOS7环境安装MariaDB(MySQL开源版)

    一、MariaDB概述

    1). 说明

    MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。

    2). 预准备

    1. 有的centos7已经默认安装了Mariadb,可以查看自己的有没有安装,没有安装的再进行安装,已经安装了可以不用安装也可以卸载了重装。
    [root@hadoop000 ~]# yum remove mariadb-server
    [root@hadoop000 ~]# cat /etc/redhat-release
    
    CentOS Linux release 7.4.1708 (Core)
    

    二、安装配置Mariadb

    1).安装MariaDB

    通过yum安装就行了。简单快捷,安装mariadb-server,默认依赖安装mariadb,一个是服务端、一个是客户端。

    [root@hadoop000 ~]# yum install mariadb-server
    

    2).制作系统服务

    1. 安装完成后首先要把MariaDB服务开启,并设置为开机启动
    [root@hadoop000 ~]# systemctl start mariadb  # 开启服务
    [root@hadoop000 ~]# systemctl enable mariadb  # 设置为开机自启动服务
    

    3).MariaDB初始化配置

    1. 首次安装需要进行数据库的配置,命令都和mysql的一样
    [root@hadoop000 ~]# mysql_secure_installation
    
    1. 配置时出现的各个选项以及建议的操作
    Enter current password for root (enter for none):  # 输入数据库超级管理员root的密码(注意不是系统root的密码),第一次进入还没有设置密码则直接回车
    
    Set root password? [Y/n]  # 设置密码,y
    
    New password:  # 新密码
    Re-enter new password:  # 再次输入密码
    
    Remove anonymous users? [Y/n]  # 移除匿名用户, y
    
    Disallow root login remotely? [Y/n]  # 拒绝root远程登录,n,不管y/n,都会拒绝root远程登录
    
    Remove test database and access to it? [Y/n]  # 删除test数据库,y:删除。n:不删除,数据库中会有一个test数据库,一般不需要
    
    Reload privilege tables now? [Y/n]  # 重新加载权限表,y。或者重启服务也许
    
    1. 测试是否能够登录成功

    出现MariaDB [(none)]>就表示已经能够正常登录使用MariaDB数据库了

    [root@hadoop000 ~]# mysql -u root -p
    Enter password:
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 8
    Server version: 5.5.60-MariaDB MariaDB Server
    
    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    MariaDB [(none)]>
    

    4).设置MariaDB字符集为utf-8

    建议使用vim工具进行配置

    1. 配置/etc/my.cnf文件,在[mysqld]标签下添加如下内容
    init_connect='SET collation_connection = utf8_unicode_ci'
    init_connect='SET NAMES utf8'
    character-set-server=utf8
    collation-server=utf8_unicode_ci
    skip-character-set-client-handshake
    
    1. 配置/etc/my.cnf.d/client.cnf文件,在[client]标签下添加
    default-character-set=utf8
    
    1. 配置/etc/my.cnf.d/mysql-clients.cnf文件,在[mysql]标签下添加
    default-character-set=utf8
    
    1. 重启服务
    [root@hadoop000 ~]# systemctl restart mariadb
    
    1. 进入mariadb查看字符集

    配置字符集后的内容

    MariaDB [(none)]> show variables like "%character%";show variables like "%collation%";
    +--------------------------+----------------------------+
    | Variable_name            | Value                      |
    +--------------------------+----------------------------+
    | character_set_client     | utf8                       |
    | character_set_connection | utf8                       |
    | character_set_database   | utf8                       |
    | character_set_filesystem | binary                     |
    | character_set_results    | utf8                       |
    | character_set_server     | utf8                       |
    | character_set_system     | utf8                       |
    | character_sets_dir       | /usr/share/mysql/charsets/ |
    +--------------------------+----------------------------+
    8 rows in set (0.00 sec)
    
    +----------------------+-----------------+
    | Variable_name        | Value           |
    +----------------------+-----------------+
    | collation_connection | utf8_unicode_ci |
    | collation_database   | utf8_unicode_ci |
    | collation_server     | utf8_unicode_ci |
    +----------------------+-----------------+
    3 rows in set (0.00 sec)
    
    MariaDB [(none)]>
    

    5).禁用防火墙配置(开发阶段建议)

    1. 查看防火墙状态

    enabled表示可以启用;running表示已经在启用

    [hadoop@hadoop000 ~]$ systemctl status firewalld
    ● firewalld.service - firewalld - dynamic firewall daemon
       Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
       Active: active (running) since Thu 2019-11-28 19:13:33 CST; 1min 41s ago
         Docs: man:firewalld(1)
     Main PID: 18478 (firewalld)
       CGroup: /system.slice/firewalld.service
               └─18478 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
    [hadoop@hadoop000 ~]$    
    
    1. 关闭防火墙
    [hadoop@hadoop000 ~]$ systemctl stop firewalld
    ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
    Authentication is required to manage system services or units.
    Authenticating as: root
    Password:
    ==== AUTHENTICATION COMPLETE ===
    [hadoop@hadoop000 ~]$
    
    1. 禁用防火墙,开机不自动启动
    [hadoop@hadoop000 ~]$ systemctl disable firewalld
    ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-unit-files ===
    Authentication is required to manage system service or unit files.
    Authenticating as: root
    Password:
    ==== AUTHENTICATION COMPLETE ===
    Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
    Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
    ==== AUTHENTICATING FOR org.freedesktop.systemd1.reload-daemon ===
    Authentication is required to reload the systemd state.
    Authenticating as: root
    Password:
    ==== AUTHENTICATION COMPLETE ===
    [hadoop@hadoop000 ~]$
    
    1. 再次检查状态
    [hadoop@hadoop000 ~]$ systemctl status firewalld
    ● firewalld.service - firewalld - dynamic firewall daemon
       Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
       Active: inactive (dead)
         Docs: man:firewalld(1)
    [hadoop@hadoop000 ~]$
    

    6).防火墙配置开放端口(生产环境)

    在不关闭防火墙的情况下,允许某端口的外来链接。步骤如下,开启3306端口,重启防火墙

    [root@hadoop000 ~]# systemctl stop firewalld
    
    
    [root@hadoop000 ~]# firewall-cmd --query-port=3306/tcp  # 查看3306端口是否开启
    
    [root@hadoop000 ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent  # 开启3306端口
    
    [root@hadoop000 ~]# firewall-cmd --reload  # 重启防火墙
    success
    [root@hadoop000 ~]# firewall-cmd --query-port=3306/tcp  # 查看3306端口是否开启
    yes
    

    7). 配置远程登录

    MariaDB默认是拒绝root远程登录的。

    1. 查看mysql数据库中的user表
    [root@hadoop000 ~]# mysql -u root -p  # 先通过本地链接进入数据库
    
    MariaDB [(none)]> use mysql;
    
    MariaDB [mysql]> select host, user from user;
    +-----------+------+
    | host      | user |
    +-----------+------+
    | 127.0.0.1 | root |
    | ::1       | root |
    | hadoop000      | root |
    +-----------+------+
    3 rows in set (0.00 sec)
    
    1. 将与主机名相等的字段改为%,我的主机名为hadoop000;
    MariaDB [mysql]> update user set host='%' where host='hadoop000';
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    MariaDB [mysql]> select host, user from user;
    +-----------+------+
    | host      | user |
    +-----------+------+
    | %         | root |
    | 127.0.0.1 | root |
    | localhost | root |
    +-----------+------+
    3 rows in set (0.00 sec)
    
    1. 刷新权限表,或重启MariaDB服务
    MariaDB [mysql]> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
    [root@hadoop000 ~]# systemctl restart mariadb
    

    注意:刷新权限表是在数据库中,重启服务是在外部命令行中

    三、添加用户并授权

    root用户太危险,APP应用建议使用其他普通用户,设置权限!

    1). 添加新用户并授权

    [hadoop@hadoop000 ~]$ mysql -u root -p
    Enter password:
    ... 
    
    MariaDB [(none)]> create user 'hadoop'@'%' identified by '^_^_^_^';
    MariaDB [(none)]> grant ALL HADOOP_HIVE.* to 'hadoop'@'%';
    

    2). 远程登录测试

    相关文章

      网友评论

          本文标题:10.CentOS7环境安装MariaDB(MySQL开源版)

          本文链接:https://www.haomeiwen.com/subject/pxhiwctx.html