关系型数据库
数据存储发展过程
文件系统-》网状/层次型数据库-》关系型数据库-》分布式数据库/noSQL
文件系统是对磁盘的抽象,将磁盘的地址空间抽象给可理解,易操作的文件;关系型数据库dbms是堆文件系统的进一步抽象,进一步抽象为数据之间关系更有逻辑,更清楚的表,数据库文件都是存在于文件系统之上的文件,只是需要dbms软件才能打开识别,也有直接去掉文件系统,直接在裸设备上存储数据库数据的,去掉一层更快,
关系型数据库范式
共有6种范式:1NF、2NF、3NF、BCNF、4NF、5NF;一般关系型数据库满足3NF即可;详见wikipedia
https://en.wikipedia.org/wiki/Category:Database_normalization
mysql安装
mysql简介
作者:Michael Widenius
相关官网:分别是mysql,mariadb,percona第三方发行版服务商
特性:
- 插件式存储引擎,支持多种存储引擎,常用myisam和innodb(mysql5.5.5后默认)
- 单进程、多线程
- 开源
- 诸多扩展、新特性
- 提供较多测试组件
安装方式
- yum仓库安装:版本较落后,适合测试
- 二进制安装,采用官方编译后二进制文件,简单配置即可;(类似与windows的绿色免安装软件)
- 编译安装,从源码从头编译安装
yum安装
安装、启动:
[root@host3 ~]# yum list all |grep mariadb
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
mariadb-libs.x86_64 1:5.5.56-2.el7 @anaconda
mariadb.x86_64 1:5.5.65-1.el7 base
mariadb-bench.x86_64 1:5.5.65-1.el7 base
mariadb-devel.i686 1:5.5.65-1.el7 base
mariadb-devel.x86_64 1:5.5.65-1.el7 base
mariadb-embedded.i686 1:5.5.65-1.el7 base
mariadb-embedded.x86_64 1:5.5.65-1.el7 base
mariadb-embedded-devel.i686 1:5.5.65-1.el7 base
mariadb-embedded-devel.x86_64 1:5.5.65-1.el7 base
mariadb-libs.i686 1:5.5.65-1.el7 base
mariadb-libs.x86_64 1:5.5.65-1.el7 base
mariadb-server.x86_64 1:5.5.65-1.el7 base
mariadb-test.x86_64 1:5.5.65-1.el7 base
# centos7采用阿里云的yum源,带的mariadb默认版本5.5,
# mariadb-server是包含服务器软件的包
# 直接安装即可,会自动安装依赖包,更新依赖包,
[root@host3 ~]# yum install -y mariadb-server
# 启动后查看进程状态,
[root@host3 ~]# systemctl start mariadb
[root@host3 ~]# systemctl status mariadb
● mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2020-09-18 11:48:10 CST; 2s ago
Process: 1263 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
Process: 1179 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
Main PID: 1262 (mysqld_safe)
CGroup: /system.slice/mariadb.service
├─1262 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
└─1424 /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/m...
[root@host3 ~]# ss -nlt
默认3306端口
# 免密连接
[root@host3 ~]# mysql
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 databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]>
# 启动后生成的数据文件
[root@host3 ~]# ll /var/lib/mysql/
total 37852
-rw-rw---- 1 mysql mysql 16384 Sep 18 11:48 aria_log.00000001
-rw-rw---- 1 mysql mysql 52 Sep 18 11:48 aria_log_control
-rw-rw---- 1 mysql mysql 18874368 Sep 18 11:48 ibdata1
-rw-rw---- 1 mysql mysql 5242880 Sep 18 11:48 ib_logfile0
-rw-rw---- 1 mysql mysql 5242880 Sep 18 11:48 ib_logfile1
drwx------ 2 mysql mysql 4096 Sep 18 11:48 mysql
srwxrwxrwx 1 mysql mysql 0 Sep 18 11:48 mysql.sock
drwx------ 2 mysql mysql 4096 Sep 18 11:48 performance_schema
drwx------ 2 mysql mysql 6 Sep 18 11:48 test
安装后目录结构:
[root@host3 ~]# rpm -ql mariadb-server
/etc/logrotate.d/mariadb
/etc/my.cnf.d/server.cnf
配置文件
/usr/bin/mysqld_multi
/usr/bin/mysqld_safe 启动脚本,通过该脚本调用启动主程序/usr/libexec/mysqld
/usr/bin/mysqld_safe_helper
各类程序文件
/usr/lib/systemd/system/mariadb.service
服务脚本
/usr/lib/tmpfiles.d/mariadb.conf
/usr/lib64/mysql/INFO_BIN
/usr/lib64/mysql/INFO_SRC
/usr/lib64/mysql/mysqlbug
/usr/lib64/mysql/plugin
/usr/lib64/mysql/plugin/adt_null.so
/usr/lib64/mysql/plugin/auth_0x0100.so
...
插件目录,下的各类插件,有些需要显示启用才行
/usr/libexec/mariadb-prepare-db-dir
/usr/libexec/mariadb-wait-ready
/usr/libexec/mysqld
主程序
/usr/share/man/man1/innochecksum.1.gz
/usr/share/man/man1/msql2mysql.1.gz
/usr/share/man/man1/myisam_ftdump.1.gz
...
帮助文件手册、示例sql、配置文件模版等
/var/lib/mysql
/var/log/mariadb
/var/log/mariadb/mariadb.log
/var/run/mariadb
数据目录,日志目录,pid文件目录
进程结构
[root@host3 ~]# pstree -p |grep mysql
|-mysqld_safe(1262)---mysqld(1424)-+-{mysqld}(1428)
| |-{mysqld}(1429)
| |-{mysqld}(1430)
| |-{mysqld}(1431)
| |-{mysqld}(1432)
| |-{mysqld}(1433)
| |-{mysqld}(1434)
| |-{mysqld}(1435)
| |-{mysqld}(1436)
| |-{mysqld}(1437)
| |-{mysqld}(1438)
| |-{mysqld}(1439)
| |-{mysqld}(1440)
| |-{mysqld}(1441)
| |-{mysqld}(1442)
| |-{mysqld}(1443)
| |-{mysqld}(1451)
| `-{mysqld}(1452)
可以看出,由mysqld_safe脚本启动的mysqld主进程,派生多个子线程;
符合单进程,多线程特性;
二进制安装
- 下载适合版本、适合平台的二进制软件包;
- 解压到需要的目录,可选择做软链接,方便版本更新
- 创建mysql用户、数据目录,改数据目录属主
- 准备配置文件、服务脚本、
- 配置环境变量
- 数据库初始化脚本,初始化数据目录
- 测试启停数据库
- 安装后配置操作
1、下载适合版本、适合平台的二进制软件包;以mariadb-10.2.25为例
[root@host2 src]# ll
total 517536
-rw-r--r-- 1 root root 457955398 Oct 29 2019 mariadb-10.2.25-linux-x86_64.tar.gz
2、解压到需要的目录,可选择做软链接,方便版本更新
[root@host2 src]# tar -xf mariadb-10.2.25-linux-x86_64.tar.gz -C /usr/local/
# 注意,软连接名字一定要是/usr/local/mysql,不能是/usr/local/mariadb,
# 否则,启动时,官方提供的服务脚本默认去找/usr/local/mysql的文件,其他的路径,会报错找不到!
[root@host2 mariadb]# ln -sv /usr/local/mariadb-10.2.25-linux-x86_64/ /usr/local/mysql
‘/usr/local/mysql’ -> ‘/usr/local/mariadb-10.2.25-linux-x86_64/’
[root@host2 mariadb]# ll /usr/local/
total 0
drwxr-xr-x. 2 root root 6 Apr 11 2018 bin
drwxr-xr-x. 2 root root 6 Apr 11 2018 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 games
drwxr-xr-x. 2 root root 6 Apr 11 2018 include
drwxr-xr-x. 2 root root 6 Apr 11 2018 lib
drwxr-xr-x. 2 root root 6 Apr 11 2018 lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 libexec
drwxrwxr-x 12 root mysql 282 Jun 15 2019 mariadb-10.2.25-linux-x86_64
lrwxrwxrwx 1 root root 40 Sep 18 14:49 mysql -> /usr/local/mariadb-10.2.25-linux-x86_64/
drwxr-xr-x. 2 root root 6 Apr 11 2018 sbin
drwxr-xr-x. 5 root root 49 Aug 8 11:58 share
drwxr-xr-x. 2 root root 79 Sep 18 13:13 src
[root@host2 mariadb]# chown -R root:mysql /usr/local/mariadb-10.2.25-linux-x86_64/
3、创建mysql用户、数据目录,改数据目录属主
[root@host2 src]# mkdir /data/mysql -pv
mkdir: created directory ‘/data’
mkdir: created directory ‘/data/mysql’
[root@host2 src]# groupadd mysql -g 306
[root@host2 src]# useradd -g mysql -u 306 mysql
[root@host2 src]# id mysql
uid=306(mysql) gid=306(mysql) groups=306(mysql)
[root@host2 src]# chown -R mysql.mysql /data/mysql/
4、准备配置文件、服务脚本
# mysql读取配置文件顺序第一位是/etc/my.cnf,属于mariadb-libs包,二进制安装时,默认配置文件不适合,可选择官方提供的配置文件,并做修改;
[root@host2 mariadb]# rpm -qf /etc/my.cnf
mariadb-libs-5.5.56-2.el7.x86_64
[root@host2 mariadb]# cp /etc/my.cnf{,.bak}
[root@host2 mariadb]# cp support-files/my-medium.cnf /etc/my.cnf
cp: overwrite ‘/etc/my.cnf’? y
# 修改配置文件,
[mysqld]
port = 3306
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
在mysqld配置段,加入如下3项,datadir根据情况修改,innodb存储引擎性能相关;禁止反解客户端名字,因为没必要
datadir=/data/mysql
innodb_file_per_table=on
skip_name_resolve=on
# 解压后目录的support-files目录下,有服务脚本,sysV风格,centos7可用
[root@host2 mariadb]# pwd
/usr/local/mariadb
[root@host2 mariadb]# ls support-files/
binary-configure my-innodb-heavy-4G.cnf my-small.cnf mysql.server wsrep_notify
magic my-large.cnf mysqld_multi.server policy
my-huge.cnf my-medium.cnf mysql-log-rotate wsrep.cnf
[root@host2 mariadb]# cp support-files/mysql.server /etc/init.d/mysqld
[root@host2 mariadb]# chkconfig --add mysqld
[root@host2 mariadb]# chkconfig mysqld on
[root@host2 mariadb]# chkconfig --list
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
5、配置环境变量
[root@host2 mysql]# cat /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
source /etc/profile.d/mysql.sh
6、数据库初始化脚本,初始化数据目录
注:不同版本,数据目录初始化,和安全加固脚本,步骤和实现脚本会有变化,以官网为准;
[root@host2 mariadb]# ./scripts/mysql_install_db --datadir=/data/mysql/ --user=mysql
Installing MariaDB/MySQL system tables in '/data/mysql/' ...
./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
[root@host2 mariadb]# yum install -y libaio
# 初始化时,缺失libaio.so.1,安装对应的包即可
[root@host2 mariadb]# ./scripts/mysql_install_db --datadir=/data/mysql/ --user=mysql
# 提示的后续操作:
# 准备服务脚本
# 改root密码
# 安全加固脚本
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:
'./bin/mysqladmin' -u root password 'new-password'
'./bin/mysqladmin' -u root -h host2.b.com password 'new-password'
Alternatively you can run:
'./bin/mysql_secure_installation'
# 再次安装即可,并在数据目录,生成如下数据文件;
[root@host2 mariadb]# ll /data/mysql/
total 110660
-rw-rw---- 1 mysql mysql 16384 Sep 18 14:42 aria_log.00000001
-rw-rw---- 1 mysql mysql 52 Sep 18 14:42 aria_log_control
-rw-rw---- 1 mysql mysql 938 Sep 18 14:42 ib_buffer_pool
-rw-rw---- 1 mysql mysql 12582912 Sep 18 14:42 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Sep 18 14:42 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Sep 18 14:42 ib_logfile1
drwx------ 2 mysql mysql 4096 Sep 18 14:42 mysql
-rw-rw---- 1 mysql mysql 29277 Sep 18 14:42 mysql-bin.000001
-rw-rw---- 1 mysql mysql 19 Sep 18 14:42 mysql-bin.index
-rw-rw---- 1 mysql mysql 7 Sep 18 14:42 mysql-bin.state
drwx------ 2 mysql mysql 20 Sep 18 14:42 performance_schema
drwx------ 2 mysql mysql 20 Sep 18 14:42 test
7、测试启停数据库
[root@host2 mysql]# service mysqld status
ERROR! MariaDB is not running
[root@host2 mysql]# service mysqld start
Starting mysqld (via systemctl): [ OK ]
[root@host2 mysql]# service mysqld status
SUCCESS! MariaDB running (5561)
[root@host2 mysql]# service mysqld restart
Restarting mysqld (via systemctl): [ OK ]
[root@host2 mysql]# service mysqld status
SUCCESS! MariaDB running (6050)
[root@host2 mysql]# ss -nlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 80 :::3306 :::*
# 查看进程信息,利用mysqld_safe脚本启动mysqld二进制主程序,pid文件,不指定时,默认在数据目录下,以主机名加.pid后缀,为文件名,最好指定;
[root@host2 mysql]# ps aux
root 4461 0.0 0.1 115432 1724 ? S 14:52 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/host2.b.com.pid
mysql 4585 0.8 4.8 1598104 48460 ? Sl 14:52 0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --us
root 4621 0.0 0.1 155324 1868 pts/1 R+ 14:52 0:00 ps aux
8、安装后配置操作,安全加固脚本!
[root@host2 mysql]# mysql
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)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
# 默认本机,免密直接连入,因此需执行自带的安全加固脚本;
安装后配置
- 执行安全增强脚本:
- 设置数据库root用户口令
- 禁止root远程登陆
- 删除anonymous账户
- 删除test数据库
[root@host2 mysql]# mysql_secure_installation
# 按照提示进行即可;
# 分别是:
1. 设置数据库root用户口令
2. 禁止root远程登陆
3. 删除anonymous账户
4. 删除test数据库
[root@host2 mysql]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 23
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)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
# 此时需要口令才能登陆
编译安装
- 安装编译工具、依赖包
- 下载需要版本的源码包,解压
- 创建用户、数据目录,改数据目录属主
- cmake编译,添加自己需要的编译选项
- make && make install
- 执行数据库数据文件初始化脚本
- 准备配置文件、服务脚本
- 配置环境变量
- 测试启停数据库进程
- 安装后配置
1、安装编译工具、依赖包
[root@host3 src]# yum install bison bison-devel zlib-devel libcurl-devel libarchive-devel boostdevel gcc gcc-c++ cmake ncurses-devel gnutls-devel libxml2-devel openssldevel libevent-devel libaio-devel
2、下载需要版本的源码包,解压
[root@host3 src]# tar -xf mariadb-10.2.25.tar.gz
[root@host3 src]# ll
total 87692
drwxrwxr-x 33 1001 1001 4096 Jun 15 2019 mariadb-10.2.25
-rw-r--r-- 1 root root 71997847 Sep 21 16:00 mariadb-10.2.25.tar.gz
3、创建用户、数据目录,改数据目录属主
[root@host3 src]# mkdir /data/mysql
[root@host3 src]# id mysql
uid=27(mysql) gid=27(mysql) groups=27(mysql)
[root@host3 src]# chown -R mysql.mysql /data/mysql/
[root@host3 src]# ll /data/mysql/ -d
drwxr-xr-x 2 mysql mysql 6 Sep 21 16:04 /data/mysql/
# 本机之前安装后mariadb,所以存在安装过程中脚本创建好的mysql用户
# 正常情况下,仍需手动创建mysql用户,如下:
[root@host2 src]# groupadd mysql -g 306
[root@host2 src]# useradd -g mysql -u 306 mysql
[root@host2 src]# id mysql
uid=306(mysql) gid=306(mysql) groups=306(mysql)
4、cmake编译,添加自己需要的编译选项
利用cmake工具指定编译选项,会生成一系列文件和目录;编译选项参数官网:https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html
cmake . \
-DCMAKE_INSTALL_PREFIX=/app/mysql \
-DMYSQL_DATADIR=/data/mysql/ \
-DSYSCONFDIR=/etc/ \
-DMYSQL_USER=mysql \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
-DWITH_DEBUG=0 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci
# 指定了:安装目录、配置文件目录、进程用户、启用ssl,zlib、编码utf8、unix套接字文件等信息;
-- Looking for event.h - found
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/local/src/mariadb-10.2.25
[root@host3 mariadb-10.2.25]# echo $?
0
# 过程中未报错!
5、make && make install
[root@host3 mariadb-10.2.25]# make && make install
# 时间较长,根据性能和分配的cpu数量而定;
# 安装后,程序目录文件如下,数据目录仍为空,需要初始化;
[root@host3 mariadb-10.2.25]# ll /app/mysql/
total 176
drwxr-xr-x 2 root root 4096 Sep 21 16:58 bin
-rw-r--r-- 1 root root 17987 Jun 15 2019 COPYING
-rw-r--r-- 1 root root 2354 Jun 15 2019 CREDITS
drwxr-xr-x 3 root root 18 Sep 21 16:58 data
drwxr-xr-x 2 root root 81 Sep 21 16:58 docs
-rw-r--r-- 1 root root 8245 Jun 15 2019 EXCEPTIONS-CLIENT
drwxr-xr-x 3 root root 19 Sep 21 16:58 include
-rw-r--r-- 1 root root 8694 Jun 15 2019 INSTALL-BINARY
drwxr-xr-x 4 root root 235 Sep 21 16:58 lib
drwxr-xr-x 4 root root 30 Sep 21 16:58 man
drwxrwxr-x 11 root root 4096 Sep 21 16:58 mysql-test
-rw-r--r-- 1 root root 2843 Jun 15 2019 README.md
-rw-r--r-- 1 root root 19477 Jun 15 2019 README-wsrep
drwxr-xr-x 2 root root 30 Sep 21 16:58 scripts
drwxr-xr-x 29 root root 4096 Sep 21 16:58 share
drwxr-xr-x 4 root root 4096 Sep 21 16:58 sql-bench
drwxr-xr-x 3 root root 275 Sep 21 16:58 support-files
-rw-r--r-- 1 root root 86263 Jun 15 2019 THIRDPARTY
[root@host3 mariadb-10.2.25]# ll /data/mysql/
total 0
6、执行数据库数据文件初始化脚本
[root@host3 mariadb-10.2.25]# ll scripts/mysql_install_db
-rwxrwxr-x 1 root root 19257 Sep 21 16:13 scripts/mysql_install_db
# 利用自带的数据目录初始化脚本;
[root@host3 mariadb-10.2.25]# mysql_install_db --datadir=/data/mysql/ --user=mysql
# 遇到的错误;
# 参数写错,reslove改为resolve,修改后,删除生成的一半的数据目录文件,再次执行脚本即可;
[root@host3 mariadb-10.2.25]# mysql_install_db --datadir=/data/mysql/ --user=mysql
Installing MariaDB/MySQL system tables in '/data/mysql/' ...
200921 17:02:39 [Warning] 'THREAD_CONCURRENCY' is deprecated and will be removed in a future release.
200921 17:02:39 [Note] /usr/libexec/mysqld (mysqld 5.5.65-MariaDB) starting as process 29524 ...
200921 17:02:39 [ERROR] /usr/libexec/mysqld: unknown variable 'skip_name_reslove=on'
[root@host3 mariadb-10.2.25]# rm -rf /data/mysql/*
# 初始化后的目录
[root@host3 mariadb-10.2.25]# ll /data/mysql/
total 1076
-rw-rw---- 1 mysql mysql 16384 Sep 21 17:03 aria_log.00000001
-rw-rw---- 1 mysql mysql 52 Sep 21 17:03 aria_log_control
drwx------ 2 mysql root 4096 Sep 21 17:03 mysql
-rw-rw---- 1 mysql mysql 28409 Sep 21 17:03 mysql-bin.000001
-rw-rw---- 1 mysql mysql 1038814 Sep 21 17:03 mysql-bin.000002
-rw-rw---- 1 mysql mysql 38 Sep 21 17:03 mysql-bin.index
drwx------ 2 mysql mysql 4096 Sep 21 17:03 performance_schema
drwx------ 2 mysql root 6 Sep 21 17:03 test
7、准备配置文件、服务脚本
# 源码包提供的配置文件,复制到对应目录,覆盖掉mariadb-libs包里自带的配置文件,并适当修改;
[root@host3 mariadb-10.2.25]# cp support-files/my-large.cnf /etc/my.cnf
cp: overwrite ‘/etc/my.cnf’? y
[root@host3 mariadb-10.2.25]# vim /etc/my.cnf
...
[mysqld]
port = 3306
socket = /data/mysql/mysql.sock
# 加入下2行;
datadir=/data/mysql
skip_name_reslove=1
# 将源码包提供的服务脚本,复制到对应目录,并添加为开机启动
[root@host3 mariadb-10.2.25]# cp support-files/mysql.server /etc/init.d/mysqld
[root@host3 mariadb-10.2.25]# chkconfig --add mysqld
[root@host3 mariadb-10.2.25]# chkconfig mysqld on
[root@host3 mariadb-10.2.25]# chmod +x /etc/init.d/mysqld # 注意执行权限;
8、配置环境变量
[root@host3 mariadb-10.2.25]# vim /etc/profile.d/mysql.sh
[root@host3 mariadb-10.2.25]# source /etc/profile.d/mysql.sh
[root@host3 mariadb-10.2.25]# cat !$
cat /etc/profile.d/mysql.sh
export PATH=/app/mysql/bin:$PATH
9、测试启停数据库进程
[root@host3 mariadb-10.2.25]# service mysqld status
ERROR! MariaDB is not running
[root@host3 mariadb-10.2.25]# service mysqld start
Reloading systemd: [ OK ]
Starting mysqld (via systemctl): ^[[A^[[A [ OK ]
10、安装后配置
[root@host3 mariadb-10.2.25]# ll scripts/mysql_secure_installation
-rwxrwxr-x 1 root root 12539 Sep 21 16:13 scripts/mysql_secure_installation
# 利用自带的安全加固脚本;
[root@host3 mariadb-10.2.25]# /app/mysql/bin/mysql_secure_installation
# 根据提示执行即可;
# 问题:
# 采用的sock文件路径不对,which发现默认的路径是yum安装mariadb的脚本路径,和编译安装的路径不同
# 实验机器,之前用yum安装了mariadb-server包;
# 用绝对路径即可解决
# 干净机器,没有用yum安装过mariadb的机器不会出现;
[root@host3 mariadb-10.2.25]# mysql_secure_installation
Enter current password for root (enter for none):
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
[root@host3 mariadb-10.2.25]# which mysql_secure_installation
/usr/bin/mysql_secure_installation
mariadb
和mysql同一个作者,mysql归属oracle后的另写的开源数据库管理软件DBMS,和mysql基本兼容;
mariadb与mysql版本演变
- mysql:
- 5.1-》5.5-》5.6-》5.7-》8.0
- mariadb:
- 5.5-》10.0-》10.1-》10.2-》10.3
mysql安装脚本
根据如下步骤,将二进制安装mysql的步骤,写到脚本里即可;
- 解压二进制包到指定目录;
- 做解压后程序目录软链接;改目录属主,属组为root.mysql
- 配置环境变量,并source;
- 复制服务脚本到指定目录;
- 准备好的配置文件my.cnf复制到指定目录;
- 创建mysql用户,
- 创建数据目录
- 改数据目录属主为mysql
- 数据目录初始化,mysql_install_db或mysqld --initiale (mysql5.7)(不同发行版,或不同版本不同)
- 启动数据库实例
- 安全加固脚本my_secure_installtion