mariadb是一个开源数的据库管理软件,是MySQL的一个分支,是MySQL创始人在出售MySQL之后开发的一个新的数据库软件,在MySQL的基础上进行了新的优化。
准备工作
安装mariadb的方式有多种,在这里介绍一下mariadb的二进制安装方式,首先要现在mariadb的二进制包,推荐官网下载https://downloads.mariadb.org/mariadb/10.2.8/,在这里下载最新的10.2.8稳定版
把已经下载好的二进制包mariadb-10.2.8-linux-x86_64.tar上传至虚拟机内
[root@c7 ~]#rz
[root@c7 ~]#ll
total 445180
-rw-r--r-- 1 root root 455856309 Sep 21 09:43 mariadb-10.2.8-linux-x86_64.tar.gz
准备mysql用户和家目录
[root@c7 ~]#getent passwd mysql --------------------------------------- # 查看mysql用户是否存在
[root@c7 ~]#useradd -r -m -d /app/dbdata -s /sbin/nologin mysql ------- # 创建mysql用户,指定家目录和用户类型
[root@c7 ~]#getent passwd mysql --------------------------------------- # 确认创建mysql用户
mysql:x:989:984::/app/dbdata:/sbin/nologin
[root@c7 ~]#ll /app/ -------------------------------------------------- # 确认创建mysql用户家目录
drwx------ 3 mysql mysql 78 Sep 25 14:46 dbdata
解压二进制包并安装
二进制包是编译过后的程序,不是源码,在编译过程中已经指定过安装的路径,必须安装编译的路径去解压存放。mariadb默认安装在/usr /local/mysql/文件夹下,在解压时直接指定解压在/usr/local/文件夹下
[root@c7 ~]#tar xvf mariadb-10.2.8-linux-x86_64.tar.gz -C /usr/local/
[root@c7 ~]#ll /usr/local/
total 0
drwxr-xr-x. 2 root root 6 Nov 5 2016 bin
drwxr-xr-x. 2 root root 6 Nov 5 2016 etc
drwxr-xr-x. 2 root root 6 Nov 5 2016 games
drwxr-xr-x. 2 root root 6 Nov 5 2016 include
drwxr-xr-x. 2 root root 6 Nov 5 2016 lib
drwxr-xr-x. 2 root root 6 Nov 5 2016 lib64
drwxr-xr-x. 2 root root 6 Nov 5 2016 libexec
drwxrwxr-x 12 1021 1004 290 Aug 18 04:16 mariadb-10.2.8-linux-x86_64
drwxr-xr-x. 2 root root 6 Nov 5 2016 sbin
drwxr-xr-x. 5 root root 49 Sep 9 20:56 share
drwxr-xr-x. 2 root root 6 Nov 5 2016 src
由此可见,因为解压后的文件夹名称和报名相同,而不是mariadb安装的文件夹mysql,在这里可以把文件夹名称改为mysql或者创建一个软连接
[root@c7 local]#ln -sv mariadb-10.2.8-linux-x86_64/ mysql
‘mysql’ -> ‘mariadb-10.2.8-linux-x86_64/’
[root@c7 local]#ll
total 0
drwxr-xr-x. 2 root root 6 Nov 5 2016 bin
drwxr-xr-x. 2 root root 6 Nov 5 2016 etc
drwxr-xr-x. 2 root root 6 Nov 5 2016 games
drwxr-xr-x. 2 root root 6 Nov 5 2016 include
drwxr-xr-x. 2 root root 6 Nov 5 2016 lib
drwxr-xr-x. 2 root root 6 Nov 5 2016 lib64
drwxr-xr-x. 2 root root 6 Nov 5 2016 libexec
drwxrwxr-x 12 1021 1004 290 Aug 18 04:16 mariadb-10.2.8-linux-x86_64
lrwxrwxrwx 1 root root 28 Sep 25 15:04 mysql -> mariadb-10.2.8-linux-x86_64/
drwxr-xr-x. 2 root root 6 Nov 5 2016 sbin
drwxr-xr-x. 5 root root 49 Sep 9 20:56 share
drwxr-xr-x. 2 root root 6 Nov 5 2016 src
准备配置文件
配置格式:类ini格式,各程序由单个配置文件提供配[prog_name]
配置文件查找次序:后面覆盖前面的配置文件
/etc/my.cnf--> /etc/mysql/my.cnf--> --default-extra-file=/PATH/TO/CONF_FILE --> ~/.my.cnf
虽然现在还没安装配置文件,但/etc/my.cnf已经生产,在这里我们可以创建一个优先级更高的的/etc/mysql/my,cnf文件。在整理可以用support-files/my-huge.cnf为模板,复制并改名为my.cnf
[root@c7 mysql]#mkdir /etc/mysql
[root@c7 mysql]#ll /etc/mysql/
total 0
[root@c7 mysql]#cp support-files/my-huge.cnf /etc/mysql/my.cnf
[root@c7 mysql]#ll /etc/mysql/my.cnf
-rw-r--r-- 1 root root 4901 Sep 25 15:36 /etc/mysql/my.cnf
修改配置文件,添加必要的修改信息
[root@c7 mysql]#vim /etc/mysql/my.cnf
......
# The MySQL server
[mysqld]
port = 3306
datadir = /app/dbdata -------------- # 数据库目录
innodb_file_per_table = on --------- # 生成独立的数据库文件
skip_name_resolve = on ------------- # 禁止主机名解析
创建日志文件
在mariadb的配置文件中,默认日志文件存在于/var/log/mariadb/maria.log,这个文件需要手动去创建,并且修改用户为mysql
[root@c7 mysql]#mkdir /var/log/mariadb
[root@c7 mysql]#touch /var/log/mariadb/maria.log
[root@c7 mysql]#chown -R mysql:mysql /var/log/mariadb/
[root@c7 mysql]#ll /var/log/mariadb/
total 0
-rw-r--r-- 1 mysql mysql 0 Sep 25 17:19 maria.log
[root@c7 mysql]#ll /var/log/mariadb/ -d
drwxr-xr-x 2 mysql mysql 23 Sep 25 17:19 /var/log/mariadb/
创建系统数据库
在系统中存在mysql数据库和text数据库,mysql数据库是系统数据库,在/app/dbdata/文件夹中还没有创建
[root@c7 mysql]#ll /app/dbdata/
total 0
执行脚本文件scripts/mysql_install_db创建系统数据库,这个脚本只能在/usr/local/mysql/下执行。否则会报错。在执行此脚本时需指定数据库目录和运行者身份。
[root@c7 mysql]#scripts/mysql_install_db --datadir=/app/dbdata --user=mysql
......
[root@c7 mysql]#ls /app/dbdata/
aria_log.00000001 ibdata1 mysql mysql-bin.state
aria_log_control ib_logfile0 mysql-bin.000001 performance_schema
ib_buffer_pool ib_logfile1 mysql-bin.index test
此时系统数据库mysql和text已经创建成功,一个文件夹代表一个数据库
启动数据库
复制文件夹support-files下的启动文件mysql.server到 /etc/init.d/并改名mysqld,启动mysqld,并查看启动后的进程
[root@c7 mysql]#cp support-files/mysql.server /etc/init.d/mysqld
[root@c7 mysql]#chkconfig mysqld on
[root@c7 mysql]#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
[root@c7 mysql]#service mysqld start
Starting mysqld (via systemctl): [ OK ]
配置环境变量
此时mysqld对应的端口3306已经打开,但是使用mysql依然无法进入数据库,再次需要配置环境变量
[root@c7 mysql]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 80 :::3306 :::*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25 :::*
[root@c7 mysql]#mysql
bash: mysql: command not found...
在etc下编辑变量配置文件,执行新的环境变量文件,重新登录mysql
[root@c7 bin]#vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
[root@c7 bin]#. /etc/profile.d/mysql.sh
[root@c7 bin]#mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.2.8-MariaDB-log MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
初始化数据库
刚建立的数据库没有进行任何的加密,用户可以随意登录,起不到保护数据的作用,因此需要对数据库进行初始化设置
[root@c7 bin]#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] y ---------------------------------------------- # 删除匿名用户
... 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] n ---------------------------------------- # 不允许root远程登录
... skipping.
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] n ------------------------------- # 不删除test测试数据库
... skipping.
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y ---------------------------------------- # 重新加载数据库
... Success!
Cleaning up...
此时数据库已经不能使用mysql命令直接连接,如要指定用户,并输入密码才能连接
[root@c7 ~]#mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@c7 ~]#mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 25
Server version: 10.2.8-MariaDB-log MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>