Centos 7 安装MariaDB
系统信息
- 如下:
[root@big-creen yum.repos.d]# cat /etc/*release
CentOS Linux release 7.8.2003 (Core)
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
CentOS Linux release 7.8.2003 (Core)
CentOS Linux release 7.8.2003 (Core)
[root@big-creen yum.repos.d]#
Yum安装
默认版本
- 比如我这里的默认版本为5.5.65,yum源为阿里源。
[root@big-creen yum.repos.d]# yum -y install mariadb mariadb-server
Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.bfsu.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package mariadb.x86_64 1:5.5.65-1.el7 will be installed
--> Processing Dependency: mariadb-libs(x86-64) = 1:5.5.65-1.el7 for package: 1:mariadb-5.5.65-1.el7.x86_64
---> Package mariadb-server.x86_64 1:5.5.65-1.el7 will be installed
--> Processing Dependency: perl-DBD-MySQL for package: 1:mariadb-server-5.5.65-1.el7.x86_64
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.65-1.el7 will be installed
---> Package perl-DBD-MySQL.x86_64 0:4.023-6.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=============================================================================================================================================================================================
Package Arch Version Repository Size
=============================================================================================================================================================================================
Installing:
mariadb x86_64 1:5.5.65-1.el7 base 8.7 M
mariadb-server x86_64 1:5.5.65-1.el7 base 11 M
Installing for dependencies:
mariadb-libs x86_64 1:5.5.65-1.el7 base 759 k
perl-DBD-MySQL x86_64 4.023-6.el7 base 140 k
Transaction Summary
=============================================================================================================================================================================================
Install 2 Packages (+2 Dependent packages)
Total download size: 21 M
Installed size: 112 M
Downloading packages:
(1/4): mariadb-libs-5.5.65-1.el7.x86_64.rpm | 759 kB 00:00:00
(2/4): mariadb-5.5.65-1.el7.x86_64.rpm | 8.7 MB 00:00:08
(3/4): perl-DBD-MySQL-4.023-6.el7.x86_64.rpm | 140 kB 00:00:00
(4/4): mariadb-server-5.5.65-1.el7.x86_64.rpm | 11 MB 00:00:09
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 2.1 MB/s | 21 MB 00:00:09
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 1:mariadb-libs-5.5.65-1.el7.x86_64 1/4
Installing : perl-DBD-MySQL-4.023-6.el7.x86_64 2/4
Installing : 1:mariadb-5.5.65-1.el7.x86_64 3/4
Installing : 1:mariadb-server-5.5.65-1.el7.x86_64 4/4
Verifying : 1:mariadb-server-5.5.65-1.el7.x86_64 1/4
Verifying : 1:mariadb-libs-5.5.65-1.el7.x86_64 2/4
Verifying : perl-DBD-MySQL-4.023-6.el7.x86_64 3/4
Verifying : 1:mariadb-5.5.65-1.el7.x86_64 4/4
Installed:
mariadb.x86_64 1:5.5.65-1.el7 mariadb-server.x86_64 1:5.5.65-1.el7
Dependency Installed:
mariadb-libs.x86_64 1:5.5.65-1.el7 perl-DBD-MySQL.x86_64 0:4.023-6.el7
Complete!
[root@big-creen yum.repos.d]#
配置MariaDB
启动MariaDB
[root@big-creen yum.repos.d]# systemctl start mariadb
[root@big-creen ~]# systemctl status mariadb
● mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/mariadb.service.d
└─migrated-from-my.cnf-settings.conf
Active: active (running) since Mon 2020-11-02 07:27:39 UTC; 12min ago
Process: 4676 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
Process: 4593 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
Main PID: 4675 (mysqld_safe)
CGroup: /system.slice/mariadb.service
├─4675 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
└─4837 /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/ma...
Nov 02 07:27:37 big-creen mariadb-prepare-db-dir[4593]: MySQL manual for more instructions.
Nov 02 07:27:37 big-creen mariadb-prepare-db-dir[4593]: Please report any problems at http://mariadb.org/jira
Nov 02 07:27:37 big-creen mariadb-prepare-db-dir[4593]: The latest information about MariaDB is available at http://mariadb.org/.
Nov 02 07:27:37 big-creen mariadb-prepare-db-dir[4593]: You can find additional information about the MySQL part at:
Nov 02 07:27:37 big-creen mariadb-prepare-db-dir[4593]: http://dev.mysql.com
Nov 02 07:27:37 big-creen mariadb-prepare-db-dir[4593]: Consider joining MariaDB's strong and vibrant community:
Nov 02 07:27:37 big-creen mariadb-prepare-db-dir[4593]: https://mariadb.org/get-involved/
Nov 02 07:27:37 big-creen mysqld_safe[4675]: 201102 07:27:37 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
Nov 02 07:27:37 big-creen mysqld_safe[4675]: 201102 07:27:37 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
Nov 02 07:27:39 big-creen systemd[1]: Started MariaDB database server.
简单配置MariaDB
- 每一个配置都有一个详细的说明,我这里就不做过多的阐述了,重置密码之后,其余的操作都是按回车就可以了。
[root@big-creen yum.repos.d]# 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 current
password for the root user. If you've just installed MariaDB, and
you 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 MariaDB
root user without the proper authorisation.
Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n]
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n]
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n]
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n]
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
测试登录
[root@big-creen ~]# mysql -uroot -p*********
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.65-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)]>
配置MariaDB的字符集
在/etc/my.cnf
配置:
[root@big-creen ~]# vim /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文件
[root@big-creen ~]# vim /etc/my.cnf.d/client.cnf
# 备注: 在[client]中添加
default-character-set=utf8
[root@big-creen ~]#
修改/etc/my.cnf.d/mysql-clients.cnf文件
[root@big-creen ~]# vim /etc/my.cnf.d/mysql-clients.cnf
#备注: 在[mysql]中添加
default-character-set=utf8
[root@big-creen ~]#
重启服务
[root@big-creen ~]# systemctl restart mariadb
[root@big-creen ~]# systemctl status mariadb
● mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/mariadb.service.d
└─migrated-from-my.cnf-settings.conf
Active: active (running) since Mon 2020-11-02 08:17:00 UTC; 11s ago
Process: 5371 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
Process: 5336 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
Main PID: 5370 (mysqld_safe)
CGroup: /system.slice/mariadb.service
├─5370 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
└─5653 /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/ma...
Nov 02 08:16:58 big-creen systemd[1]: Starting MariaDB database server...
Nov 02 08:16:58 big-creen mariadb-prepare-db-dir[5336]: Database MariaDB is probably initialized in /var/lib/mysql already, nothing is done.
Nov 02 08:16:58 big-creen mariadb-prepare-db-dir[5336]: If this is not the case, make sure the /var/lib/mysql is empty before running mariadb-prepare-db-dir.
Nov 02 08:16:58 big-creen mysqld_safe[5370]: 201102 08:16:58 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
Nov 02 08:16:59 big-creen mysqld_safe[5370]: 201102 08:16:59 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
Nov 02 08:17:00 big-creen systemd[1]: Started MariaDB database server.
[root@big-creen ~]#
配置成功查看
[root@big-creen yum.repos.d]# mysql -uroot -p******
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-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)
MariaDB [(none)]>
远程链接访问
没有开启远程访问权限
[root@big-creen yum.repos.d]# mysql -uroot -ppassw0rd
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.65-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)]> select User, host from mysql.user;
+------+-----------+
| User | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| root | localhost |
+------+-----------+
3 rows in set (0.01 sec)
MariaDB [(none)]>
配置允许远程访问
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
# 备注: %表示针对所有IP,password表示将用这个密码登录root用户,如果想只让某个IP段的主机连接,可以修改为你需要访问的具体IP地址,我这里放开所有了。
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%.%' IDENTIFIED BY 'new-password' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
# 备注: 此时远程连接的密码可能与你在本地登录时的密码不同了,主要看你在IDENTIFIED BY后面给了什么密码,我这里和原密码保持一致了,主要为了方便记忆。
# 更多的内容可以参考具体的GRANT命令
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>
配置完成之后
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select User, host from mysql.user;
+------+-----------+
| User | host |
+------+-----------+
| root | % |
| root | %.% |
| root | 127.0.0.1 |
| root | ::1 |
| root | localhost |
+------+-----------+
5 rows in set (0.00 sec)
MariaDB [(none)]>
- 如果这个时候还是没办法访问数据库,就需要检查一下服务器是否设置了防火墙了。
[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: active (running) since Fri 2021-02-26 15:08:31 CST; 2s ago
Docs: man:firewalld(1)
Main PID: 82697 (firewalld)
Tasks: 2
CGroup: /system.slice/firewalld.service
├─82697 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
└─82831 /usr/sbin/ip6tables-restore -n
Feb 26 15:08:29 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
Feb 26 15:08:31 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# 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)
Feb 26 15:08:29 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
Feb 26 15:08:31 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
Feb 26 15:09:21 localhost.localdomain systemd[1]: Stopping firewalld - dynamic firewall daemon...
Feb 26 15:09:23 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon.
[root@localhost ~]#
- 再远程访问就可以访问了。
- 以上为Centos7 利用Yum安装默认版本的MariaDB, 如果想安装高版本的MariaDB,比如10.2版本的MariaDB怎么弄了?
配置高版本yum源
[root@big-creen yum.repos.d]# cd /etc/yum.repos.d/
[root@big-creen yum.repos.d]# vim MariaDB.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
# 添加上面的内容到MariaDB.repo中,当然也可以换版本10.3(http://yum.mariadb.org/10.3/centos7-amd64)或者10.4(http://yum.mariadb.org/10.4/centos7-amd64), 可以通过访问链接本身存在与否进行安装。
[root@big-creen yum.repos.d]# yum makecache
Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 7.0 kB 00:00:00
* base: mirrors.aliyun.com
* epel: mirrors.bfsu.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
Metadata Cache Created
[root@big-creen yum.repos.d]#
- 其他的步骤参照默认Yum安装MariaDB的步骤。
提示: 替换阿里Yum源—Centos7
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 或者
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache
JackDan Thinking!