CentOS7安装MariaDB|CentOS7如何安装MariaDB|Linux安装MariaDB详解

CentOS7安装MariaDB|CentOS7如何安装MariaDB|Linux安装MariaDB详解_第1张图片

MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。

本文介绍CentOS7.6安装MariaDB过程。

执行安装命令:

[root@db ~]# yum -y install mariadb*Loaded plugins: fastestmirrorLoading mirror speeds from cached hostfile * base: centos.ustc.edu.cn * epel: mirrors.njupt.edu.cn * extras: centos.ustc.edu.cn * updates: centos.ustc.edu.cnResolving Dependencies--> Running transaction check---> Package mariadb.x86_64 1:5.5.60-1.el7_5 will be installed---> Package mariadb-bench.x86_64 1:5.5.60-1.el7_5 will be installed---> Package mariadb-devel.x86_64 1:5.5.60-1.el7_5 will be installed---> Package mariadb-embedded.x86_64 1:5.5.60-1.el7_5 will be installed---> Package mariadb-embedded-devel.x86_64 1:5.5.60-1.el7_5 will be installed---> Package mariadb-libs.x86_64 1:5.5.60-1.el7_5 will be installed---> Package mariadb-server.x86_64 1:5.5.60-1.el7_5 will be installed--> Processing Dependency: perl-DBD-MySQL for package: 1:mariadb-server-5.5.60-1.el7_5.x86_64---> Package mariadb-test.x86_64 1:5.5.60-1.el7_5 will be installed--> Running transaction check---> Package perl-DBD-MySQL.x86_64 0:4.023-6.el7 will be installed--> Finished Dependency Resolution....

开机自动启动MariaDB:​​​​​​​

[root@db ~]# systemctl enable mariadbCreated symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

启动MariaDB:

[root@db ~]# systemctl start mariadb

查看MariaDB状态:​​​​​​​

[root@db ~]# systemctl status mariadb● mariadb.service - MariaDB database server   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)   Active: active (running) since Fri 2019-09-13 22:54:44 CST; 32s ago  Process: 3280 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)  Process: 3201 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS) Main PID: 3279 (mysqld_safe)   CGroup: /system.slice/mariadb.service           ├─3279 /bin/sh /usr/bin/mysqld_safe --basedir=/usr           └─3441 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/va...
Sep 13 22:54:42 db mariadb-prepare-db-dir[3201]: '/usr/bin/mysql_secure_installation'Sep 13 22:54:42 db mariadb-prepare-db-dir[3201]: which will also give you the option of removing the testSep 13 22:54:42 db mariadb-prepare-db-dir[3201]: databases and anonymous user created by default.  This isSep 13 22:54:42 db mariadb-prepare-db-dir[3201]: strongly recommended for production servers.Sep 13 22:54:42 db mariadb-prepare-db-dir[3201]: See the MariaDB Knowledgebase at http://mariadb.com/kb or theSep 13 22:54:42 db mariadb-prepare-db-dir[3201]: MySQL manual for more instructions.Sep 13 22:54:42 db mariadb-prepare-db-dir[3201]: Please report any problems at http://mariadb.org/jiraSep 13 22:54:42 db mysqld_safe[3279]: 190913 22:54:42 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.Sep 13 22:54:42 db mysqld_safe[3279]: 190913 22:54:42 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysqlSep 13 22:54:44 db systemd[1]: Started MariaDB database server.

初次安装数据库,进行数据库配置:​​​​​​​

[root@db ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the currentpassword for the root user.  If you've just installed MariaDB, andyou haven't set the root password yet, the password will be blank,so you should just press enter here.
Enter current password for root (enter for none): OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDBroot user without the proper authorisation.
Set root password? [Y/n] yNew password: Re-enter new password: Password updated successfully!Reloading privilege tables.. ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyoneto log into MariaDB without having to have a user account created forthem.  This is intended only for testing, and to make the installationgo a bit smoother.  You should remove them before moving into aproduction environment.
Remove anonymous users? [Y/n] y ... Success!
Normally, root should only be allowed to connect from 'localhost'.  Thisensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y ... Success!
By default, MariaDB comes with a database named 'test' that anyone canaccess.  This is also intended only for testing, and should be removedbefore moving into a production environment.
Remove test database and access to it? [Y/n] y - Dropping test database... ... Success! - Removing privileges on test database... ... Success!
Reloading the privilege tables will ensure that all changes made so farwill take effect immediately.
Reload privilege tables now? [Y/n] y ... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MariaDBinstallation should now be secure.
Thanks for using MariaDB!

登录数据库:​​​​​​​

[root@db ~]# mysql -uroot -pEnter password: Welcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection id is 16Server 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)]>

设置字符集(建议设置为:UTF-8):

[root@db ~]# vim /etc/my.cnf

默认内容如下:​​​​​​​

[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sock# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0# Settings user and group are ignored when systemd is used.# If you need to run mysqld under a different user or group,# customize your systemd unit file for mariadb according to the# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]log-error=/var/log/mariadb/mariadb.logpid-file=/var/run/mariadb/mariadb.pid
## include all files from the config directory#!includedir /etc/my.cnf.d

修改内容:​​​​​​​

[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sock# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0# Settings user and group are ignored when systemd is used.# If you need to run mysqld under a different user or group,# customize your systemd unit file for mariadb according to the# instructions in http://fedoraproject.org/wiki/Systemdinit_connect='SET collation_connection = utf8_unicode_ci'init_connect='SET NAMES utf8'character-set-server=utf8collation-server=utf8_unicode_ciskip-character-set-client-handshake
[mysqld_safe]log-error=/var/log/mariadb/mariadb.logpid-file=/var/run/mariadb/mariadb.pid
## include all files from the config directory#!includedir /etc/my.cnf.d

添加的内容:

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

 

修改文件:

[root@db ~]# vim /etc/my.cnf.d/client.cnf

默认内容:​​​​​​​

## These two groups are read by the client library# Use it for options that affect all clients, but not the server#

[client]
# This group is not read by mysql client library,# If you use the same .cnf file for MySQL and MariaDB,# use it for MariaDB-only client options[client-mariadb]

修改后内容:​​​​​​​

## These two groups are read by the client library# Use it for options that affect all clients, but not the server#

[client]default-character-set=utf8
# This group is not read by mysql client library,# If you use the same .cnf file for MySQL and MariaDB,# use it for MariaDB-only client options[client-mariadb]

添加的内容:

default-character-set=utf8

 

修改文件:

[root@db ~]# vim /etc/my.cnf.d/mysql-clients.cnf

默认内容:

## These groups are read by MariaDB command-line tools# Use it for options that affect only one utility#
[mysql]
[mysql_upgrade]
[mysqladmin]
[mysqlbinlog]
[mysqlcheck]
[mysqldump]
[mysqlimport]
[mysqlshow]
[mysqlslap]

修改后内容:​​​​​​​

## These groups are read by MariaDB command-line tools# Use it for options that affect only one utility#
[mysql]default-character-set=utf8
[mysql_upgrade]
[mysqladmin]
[mysqlbinlog]
[mysqlcheck]
[mysqldump]
[mysqlimport]
[mysqlshow]
[mysqlslap]

添加的内容:

default-character-set=utf8

接着重启服务生效:

[root@db ~]# systemctl restart mariadb

MYSQL客户端进入,查看字符集:​​​​​​​

[root@db ~]# mysql -uroot -pEnter password: Welcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection id is 4Server 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)]> show variables like "%character%";+--------------------------+----------------------------+| 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)
MariaDB [(none)]> show variables like "%collation%";+----------------------+-----------------+| 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)

此前已经设置默认拒绝root远程登录,如果要开放远程登录,得开放防火墙,允许3306访问:

[root@db ~]# firewall-cmd --permanent --zone=public --add-port=3306/tcpsuccess[root@db ~]# firewall-cmd --reloadsuccess

 MYSQL客户端进入修改权限:​​​​​​​

[root@db ~]# mysql -uroot -pEnter password: Welcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection id is 5Server 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)]> use mysql;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -A
Database changedMariaDB [mysql]> select user,host from user;+------+-----------+| user | host      |+------+-----------+| root | 127.0.0.1 || root | ::1       || root | localhost |+------+-----------+3 rows in set (0.00 sec)
MariaDB [mysql]> update user set host='%' where host='localhost';Query OK, 1 row affected (0.00 sec)Rows matched: 1  Changed: 1  Warnings: 0
MariaDB [mysql]> select user,host from user;+------+-----------+| user | host      |+------+-----------+| root | %         || root | 127.0.0.1 || root | ::1       |+------+-----------+3 rows in set (0.00 sec)
MariaDB [mysql]> flush privileges;Query OK, 0 rows affected (0.00 sec)

 

做2件事:

1.修改localhost为%,表示允许任意主机访问。

2.刷新权限。(这一项改为重启服务器也可以。)

 

 

接下来,可以远程访问了:

[user@client ~]$ mysql -hdb.xxx.com -uroot -pEnter password: Welcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection id is 7Server 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)]> 

 如没有权限,则会提示如下:​​​​​​​

[user@client ~]$ mysql -hdb.xxx.com -uroot -pEnter password: ERROR 1130 (HY000): Host '192.168.1.3' is not allowed to connect to this MariaDB server

至此,本文MariaDB的基本安装和配置介绍完毕。更多使用经验,将在后续再介绍,敬请关注~

更多资讯请搜索公众号:技术训练营

你可能感兴趣的:(操作系统)