数据库
首页 > 数据库> > Centos7.0 安装MariaDB数据库

Centos7.0 安装MariaDB数据库

作者:互联网

(我是本地虚拟机,root用户登录的centos)
1、安装wget
      yum install wget 或者 sudo yum install wget

2、安装mysql
     查看可下载的mysql版本:yum list | grep mysql
     安装mysql,执行命令:yum install mysql
     执行上述命令后,竟然安装的是一个叫Mariadb的数据库,这个数据库是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。

3、等待安装完成后,执行mysql -u root -p命令,结果发现无法链接,出现如下提示:

     ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
    查看一下Mariadb是否开启:
    [root@node4 etc]# ps -A|grep mysql
         执行命令后如果没有任何结果,证明Mariadb没有开启,直接执行命令开启:systemctl start mariadb。
        如果不能开启,安装了maridb-server后再启动服务器。

4、设置服务开启自动启动:
     [root@node4 ~]# systemctl enabled mariadb

5、开始配置选项:
    [root@node4 ~]# mysql_secure_installation
       Enter current password for root (enter for none):
       这是提示要输入Mariadb数据库的root密码,由于我们是第一安装,没有密码,直接按enter,接着会提示是否要设置root密码,按照提示设置密码即可。
       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。或者重启服务也许

6、测试是否可以连接成功:
      [root@node4 ~]# mysql -u root -p
      出现MariaDB [(none)]> 即表示连接成功。

7、设置MariaDB字符集为utf-8:
修改之前进入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 | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_general_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)

在/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

在/etc/my.cnf.d/client.cnf 文件的[client]标签下增加如下内容:
default-character-set=utf8

在/etc/my.cnf.d/mysql-clients.cnf 文件的[mysql]标签下增加如下内容:
default-character-set=utf8

修改完重启Mariadb服务,进入Mariadb再查看数据库字符集:
[root@node4 my.cnf.d]# systemctl restart 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)

 

8、远程链接mariadb数据库:
Mariadb默认是拒绝 root 远程登录的。这里用的是 navicat 软件连接数据库
两种方式:
1、关闭防火墙:
[root@node4 ~]# systemctl stop firewalld
MariaDB [(none)]> use mysql;
MariaDB [mysql]> select host,user from user;
+-----------+------+
| host | user |
+-----------+------+
| 127.0.0.1 | root |
| ::1 | root |
| localhost | root |
+-----------+------+
MariaDB [mysql]> update user set host='%' where host='::1';

刷新权限,下面连个命令都可以,二选一即可:
MariaDB [mysql]> flush privileges;
[root@node4 ~]# systemctl restart mariadb

2、不关闭防火墙:
[root@node4 ~]# systemctl start firewalld

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

MariaDB [(none)]> use mysql;
MariaDB [mysql]> select host,user from user;
+-----------+------+
| host | user |
+-----------+------+
| 127.0.0.1 | root |
| ::1 | root |
| localhost | root |
+-----------+------+
MariaDB [mysql]> update user set host='%' where host='::1';

9、刷新权限,下面连个命令都可以,二选一即可:
MariaDB [mysql]> flush privileges;
[root@node4 ~]# systemctl restart mariadb

标签:set,utf8,数据库,character,mysql,Centos7.0,MariaDB,root
来源: https://www.cnblogs.com/Ryan-Fei/p/11790618.html