MariaDB安装教程
MariaDB版本:10.3.9
资源下载:https://downloads.mariadb.org
一、安装前准备
卸载系统自带mariadb-libs
rpm -qa|grep mariadb-libs
rpm -e mariadb-libs-5.5.52-1.el7.x86_64 --nodeps
此处的mariadb-libs-5.5.52-1.el7.x86_64文件,需要根据自身系统进行更改
二、准备安装
A、安装依赖
yum -y install libaio
yum -y install libaio-devel
yum -y install bison
yum -y install bison-devel
yum -y install zlib-devel
yum -y install openssl
yum -y install openssl-devel
yum -y install ncurses
yum -y install ncurses-devel
yum -y install libcurl-devel
yum -y install libarchive-devel
yum -y install boost
yum -y install boost-devel
yum -y install lsof
yum -y install wget
yum -y install gcc
yum -y install gcc-c++
yum -y install make
yum -y install cmake
yum -y install perl
yum -y install kernel-headers
yum -y install kernel-devel
yum -y install pcre-devel
B、创建MariaDB数据初始化目录
这里提前预定MariaDB的安装目录为/usr/local/mysql并且数据库目录为/data/mysql,这里要建立系统用户及组和数据库存放目录,并且将数据库存放目录赋予mysql用户及组权限
创建mysql系统用户组:
groupadd -r mysql
创建系统用户mysql并加入到mysql系统用户组和禁止mysql用户shell登陆系统:
useradd -r -g mysql -s /sbin/nologin -d /usr/local/mysql -M mysql
创建MariaDB安装目录:
mkdir -p /usr/local/mysql
创建数据库存放目录:
mkdir -p /data/mysql
改变MariaDB安装目录所属用户及组为 mysql.root:
chown -R mysql:root /usr/local/mysql
改变数据库存放目录所属用户及组为 mysql.mysql:
chown -R mysql:mysql /data/mysql
三、开始安装
将下载好的安装包通过FTP放到 /usr/local/src 目录下
进入源码包目录:
cd /usr/local/src/
解压:
tar -zxvf mariadb-10.3.9.tar.gz
进入到解压后的源码包文件夹
cd mariadb-10.3.9
输入编译参数
cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DENABLED_LOCAL_INFILE=1 \
-DENABLE_DOWNLOADS=1 \
-DEXTRA_CHARSETS=all \
-DSYSCONFDIR=/etc \
-DWITHOUT_TOKUDB=1 \
-DWITH_ARCHIVE_STPRAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_DEBUG=0 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LOBWRAP=0 \
-DMYSQL_DATADIR=/data/mysql \
-DMYSQL_USER=mysql \
-DMYSQL_UNIX_ADDR=/var/run/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_MAINTAINER_MODE=0
编译安装
make && make install
提示:
如果编译失败请先删除CMakeCache.txt文件以及使用make clean命令,否则每次编译读取的都是这个文件,从而导致命令修改正确也会报错。
编译过程中若出现internal compiler error: Killed错误提示,请看虚拟内存篇解决
四、配置MariaDB及设置启动命令
进入MariaDB安装目录
cd /usr/local/mysql/
使用MariaDB用户执行脚本, 安装数据库到数据库存放目录
./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
创建MariaDB配置文件my.cnf
vim /etc/my.cnf
输入以下内容并保存(:wq)
[mysqld]
datadir=/data/mysql
socket=/var/run/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-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/mysql/mariadb.log
pid-file=/var/run/mysql/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
创建MySQL通讯目录和日志目录
mkdir /var/run/mysql
mkdir /var/log/mysql
赋予MySQL通讯目录和日志目录权限
chown mysql:mysql /var/run/mysql
chown mysql:mysql /var/log/mysql
创建启动脚本
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
启动mysqld服务
/etc/init.d/mysqld start
设置systemctl启动数据库
创建并编辑mariadb.service文件
vim /usr/lib/systemd/system/mariadb.service
输入以下内容
# It's not recommended to modify this file in-place, because it will be
# overwritten during package upgrades. If you want to customize, the
# best way is to create a file "/etc/systemd/system/mariadb.service",
# containing
# .include /lib/systemd/system/mariadb.service
# ...make your changes here...
# or create a file "/etc/systemd/system/mariadb.service.d/foo.conf",
# which doesn't need to include ".include" call and which will be parsed
# after the file mariadb.service itself is parsed.
#
# For more info about custom unit files, see systemd.unit(5) or
# http://fedoraproject.org/wiki/Systemd#How_do_I_customize_a_unit_file.2F_add_a_custom_unit_file.3F
# For example, if you want to increase mariadb's open-files-limit to 10000,
# you need to increase systemd's LimitNOFILE setting, so create a file named
# "/etc/systemd/system/mariadb.service.d/limits.conf" containing:
# [Service]
# LimitNOFILE=10000
# Note: /usr/lib/... is recommended in the .include line though /lib/...
# still works.
# Don't forget to reload systemd daemon after you change unit configuration:
# root> systemctl --system daemon-reload
[Unit]
Description=MariaDB database server
After=syslog.target
After=network.target
[Service]
Type=simple
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql
ExecStop=/bin/kill -9 $MAINPID
# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300
# Place temp files in a secure directory, not /tmp
PrivateTmp=true
[Install]
WantedBy=multi-user.target
--------------------------------
启动数据库服务
systemctl start mariadb.service
查看进程信息
ps -ef | grep mysql
配置环境变量
新建并编辑mysql.sh脚本
vim /etc/profile.d/mysql.sh
输入以下内容:
export PATH=$PATH:/usr/local/mysql/bin/
保存退出:
:wq
赋予mysql.sh脚本可执行权限
chmod 0777 /etc/profile.d/mysql.sh
读取并执行`mysql.sh`脚本, 并即刻生效环境变量:
source /etc/profile.d/mysql.sh
五、初始化MariaDB
进入mysql安装目录
cd /usr/local/mysql
运行MariaDB初始化脚本
./bin/mysql_secure_installation
根据相应提示进行操作,以下用作参考
Enter current password for root (enter for none): 输入当前root密码(没有输入)
Set root password? [Y/n] 设置root密码?(是/否)
New password: 输入新root密码
Re-enter new password: 确认输入root密码
Password updated successfully! 密码更新成功
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.
默认情况下,MariaDB安装有一个匿名用户,
允许任何人登录MariaDB而他们无需创建用户帐户。
这个目的是只用于测试,安装去更平缓一些。
你应该进入前删除它们生产环境。
Remove anonymous users? [Y/n] 删除匿名用户?(是/否)
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
通常情况下,root只应允许从localhost连接。
这确保其他用户无法从网络猜测root密码。
Disallow root login remotely? [Y/n] 不允许root登录远程?(是/否)
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]
默认情况下,MariaDB提供了一个名为“测试”的数据库,任何人都可以访问。
这也只用于测试,在进入生产环境之前应该被删除。
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
重新加载权限表将确保所有到目前为止所做的更改将立即生效。
Reload privilege tables now? [Y/n] 现在重新加载权限表(是/否)
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
全部完成!如果你已经完成了以上步骤,MariaDB安装现在应该安全。
Thanks for using MariaDB!
感谢使用MariaDB!
--------------------------------
六、配置远程连接
进入MariaDB
mysql -u root -p
输入命令回车后提示输入密码,密码为步骤七所设置。若跳过了步骤七,则默认无密码,直接再次回车即可。
显示数据库
show databases;
创建用户名和密码及相关权限
grant all privileges on 数据库名.表名 to 用户名@"IP地址" identified by "密码";
all代表所有权限
可选权限:
select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等14个权限。
示例
grant all privileges on *.* to 用户名@"%" identified by "密码";
重新加载权限
flush privileges;
七、备注
Useradd/Usermod (选项)(参数):
-c<备注>:加上备注文字。备注文字会保存在passwd的备注栏位中;
-d<登入目录>:指定用户登入时的启始目录;
-D:变更预设值;
-e<有效期限>:指定帐号的有效期限;
-f<缓冲天数>:指定在密码过期后多少天即关闭该帐号;
-g<群组>:指定用户所属的群组;
-G<群组>:指定用户所属的附加群组;
-m:自动建立用户的登入目录;
-M:不要自动建立用户的登入目录;
-n:取消建立以用户名称为名的群组;
-r:建立系统帐号;
-s<shell>:指定用户登入后所使用的shell;
-u<uid>:指定用户id。
编译参数注释:
# cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ [MySQL安装的根目录]
-DDEFAULT_CHARSET=utf8 \ [设置默认字符集为utf8]
-DDEFAULT_COLLATION=utf8_general_ci \ [设置默认字符校对]
-DENABLED_LOCAL_INFILE=1 \ [启用加载本地数据]
-DENABLE_DOWNLOADS=1 \ [编译时允许自主下载相关文件]
-DEXTRA_CHARSETS=all \ [使MySQL支持所有的扩展字符]
-DSYSCONFDIR=/etc \ [MySQL配置文件所在目录]
-DWITHOUT_TOKUDB=1 \
-DWITH_ARCHIVE_STPRAGE_ENGINE=1 \ [MySQL的数据库引擎]
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \ [MySQL的数据库引擎]
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ [MySQL的数据库引擎]
-DWITH_DEBUG=0 \ [禁用调试模式]
-DWITH_MEMORY_STORAGE_ENGINE=1 \ [MySQL的数据库引擎]
-DWITH_MYISAM_STORAGE_ENGINE=1 \ [MySQL的数据库引擎]
-DWITH_INNOBASE_STORAGE_ENGINE=1 \ [MySQL的数据库引擎]
-DWITH_PARTITION_STORAGE_ENGINE=1 \ [MySQL的数据库引擎]
-DWITH_READLINE=1 \ [MySQL的readline library]
-DWITH_SSL=system \ [通讯时支持ssl协议]
-DWITH_ZLIB=system \ [允许使用zlib library]
-DWITH_LOBWRAP=0 \
-DMYSQL_DATADIR=/data/mysql \ [MySQL数据库文件存放目录]
-DMYSQL_USER=mysql \ [MySQL用户名]
-DMYSQL_UNIX_ADDR=/var/run/mysql/mysql.sock \ [MySQL的通讯目录]
-DMYSQL_TCP_PORT=3306 \ [MySQL的监听端口]
-DMYSQL_MAINTAINER_MODE=0
资料参考
my.cnf资料参考:
https://blog.csdn.net/xxx9001/article/details/51837595
数据库权限配置参考:
https://blog.csdn.net/wengyupeng/article/details/3290415