软件包的安装方式有三种:源码包编译安装、二进制包安装、rpm包安装。这三种安装方法都各有优劣。RPM安装:适合小环境,核心功能都具备,快速搭建环境,但它的版本一般都不会太高;二进制安装:它是发布出来时预先编译过的,既避免了编译的麻烦,又提供了增强功能;源码包编译安装:它的要求高,要有编译环境,编译时可指定几乎所有选项,根据需求进行定制,可满足你的所有选择。
由于centos7系统RPM包自带Mariadb包版本过低,不适合学习使用,因此今天就通过二进制包安装的方式来解决版本问题。
安装步骤如下:
1,下载二进制包;
2,解压到安装目录,安装相应的依赖包;
3、准备配置文件;
4、设置启动服务项;
5、测试是否可以正常的登录;
6、数据库安全加固;
7、安全初始化;
安装过程如下:
1,下载二进制包:
二进制包的下载可以从官方网站下载,也可以从国内清华大学官网下载,下载地址如下:
- 项目官方:https://downloads.mariadb.org/mariadb/repositories/
- 国内镜像:https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/
这里下载的Mariadb包的版本为mariadb-10.2.23-linux-x86_64.tar.gz。
2,解压到安装目录,安装相应的依赖包:
(1)安装相应的依赖包:
[root@localhost ~]# yum -ql libaio-devel numactl-devel #环境依赖包
[root@localhost ~]# yum install lrzsz-0.12.20-36.el7.x86_64 #负责文件的上传和下载
(2)创建mysql用户,创建/data/mysql文件夹,计划将数据库安装到/data/mysql文件夹下面:
# useradd -r -s /sbin/nologin -d /data/mysql mysql #创建mysql的系统账号,
指定shell类型为nologin,指定安装目录为/data/mysql
(3)将下载的二进制包进行解压,解压到/usr/local/目录下面:
#tar xvf mariadb-10.2.25-linux-x86_64.tar.gz -C /usr/local/ #注意一定要将包解压到/usr/local/目录下面。
3、准备配置文件:
(1)将解压到/usr/local的创建软连接并修改权限:
[root@host7 local]# ln -s mariadb-10.2.25-linux-x86_64/ mysql
[root@localhost local]# ll
lrwxrwxrwx 1 root root 29 Jul 3 22:28 mysql -> mariadb-10.2.25-linux-x86_64/
[root@localhost local]#chown -R root.root mysql/ #将文件夹的权限修改为root
[root@localhost mysql]# ll
total 176
drwxr-xr-x 2 root root 4096 Jul 4 09:06 bin
-rw-r--r-- 1 root root 17987 Jun 15 02:29 COPYING
-rw-r--r-- 1 root root 2354 Jun 15 02:29 CREDITS
drwxrwxr-x 3 root root 18 Jun 15 23:35 data
-rw-r--r-- 1 root root 8245 Jun 15 02:29 EXCEPTIONS-CLIENT
drwxrwxr-x 3 root root 19 Jun 15 23:35 include
-rw-r--r-- 1 root root 8694 Jun 15 02:29 INSTALL-BINARY
drwxr-xr-x 5 root root 335 Mar 15 01:07 lib
drwxrwxr-x 4 root root 30 Jun 15 23:35 man
drwxrwxr-x 11 root root 4096 Jun 15 23:35 mysql-test
-rw-r--r-- 1 root root 2843 Jun 15 02:29 README.md
-rw-r--r-- 1 root root 19477 Jun 15 02:29 README-wsrep
drwxrwxr-x 2 root root 30 Jul 3 22:50 scripts
drwxrwxr-x 32 root root 4096 Jun 15 23:35 share
drwxrwxr-x 4 root root 4096 Jun 15 23:35 sql-bench
drwxrwxr-x 3 root root 275 Jun 15 23:35 support-files
-rw-r--r-- 1 root root 86263 Jun 15 02:29 THIRDPARTY
(2)进入/mysql/scripts 目录,运行里面的 脚本,设置数据库在哪个目录创建:
数据库创建脚本:
[root@localhost mysql]# cd scripts/
[root@localhost scripts]# ll
total 20
-rwxr-xr-x 1 root root 19257 Jun 15 02:34 mysql_install_db #运行该脚本可创建数据库
注意运行该目录使用相对路径的方法,否者运行过程会报错:
[root@localhost mysql]# ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql #指定安装路径为/data/myssql,以mysql的用户进行运行。
(3)将 /usr/local/mysql/suppert-files/my-huge.cnf文件下数据库配置大小文件拷贝到/data/mysql/my.cnf目录下并修改路径:
将包内配置文件拷贝到指定目录下,并修改文件名称:
[root@localhost ~]# cp /usr/local/mysql/support-files/my-huge.cnf /etc/mysql/my.cnf
修改/etc/myaql/my.cnf文件:
[root@localhost ~]# vim /etc/mysql/my.cnf
# The MySQL server
[mysqld]
datadir=/data/mysql #将路径修改为/data/mysql
port = 3306
socket = /tmp/mysql.sock
4,设置启动服务项:
(1) 将/usr/local/mysql/suppert-files下的mysql.server 拷贝到 /etc/init.d/mysql目录下,并设置成启动服务项:
[root@localhost ~]# cp /usr/local/mysql//support-files/mysql.server /etc/init.d/mysqld
检查服务项,没有Mysqldq启动服务:
[root@localhost ~]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
将mysqld的服务项加入到服务启动脚本里:
[root@localhost ~]# chkconfig --add mysqld
启动mysql服务:
[root@localhost ~]# service mysqld start
[root@localhost ~]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off #服务已经启动
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
(2)将Mysql的bin路径添加到全局路径下,并使其生效:
[root@localhost mysql]# echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh #将路径添加到全局路径下。
[root@localhost mysql]# . etc/profile.d/mysql.sh 使该设置生效
5,测试是否可以正常的登录:
# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 34
Server version: 10.2.25-MariaDB-log 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 [(none)]> create database testdb; #测试创建数据库;
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| db1 | |
| information_schema |
| mysql |
| performance_schema |
| testdb |
+--------------------+
5 rows in set (0.00 sec)
6,数据库安全初始化:
使用 /usr/local/mysql/bin 下面的mysql_secure_installation脚本进行安全加固:
[root@localhost ~]# 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): 输入新密码,并确认新密码,后面选项采用默认即可。
7,测试登陆:
[root@localhost ~]# mysql -uroot -p #测试是否可用密码正确登陆
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.2.25-MariaDB-log 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的二进制安装过程已经完成,可以使用该数据库了。