Yum安装MariaDB

2021-04-27T21:50:08

CenttOS7.9安装MariaDB数据库,从CentOS7就已经使用MariaDB来替代MySQL数据库,使用基本和MySQL一致,主要由开源社区维护。

安装与卸载

通过yum命令安装就可以了。

yum install mariadb mariadb-server mariadb-libs

卸载mariadb

yum remove mariadb mariadb-server mariadb-libs

启动、停止、查看状态

# 启动服务
systemctl start mariadb
# 关闭服务
systemctl stop mariadb
# 设置开机自启
systemctl enable mariadb
# 查看状态
ststemctl status mariadb

初始化MariaDB

进行数据库首次配置

mysql_secure_installation

设置root密码和参数

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.
# 输入数据库root用户密码,第一次进入还没有设置密码,直接回车
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.

You already have a root password set, so you can safely answer 'n'.
# 修改root密码,输入y
Change the 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.
# 删除匿名用户,输入y
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.
# 拒绝root用户远程登录,输入y
Disallow root login remotely? [Y/n] y
 ... 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.
# 删除test数据库,输入y
Remove test database and access to it? [Y/n] y
 - 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.
# 刷新权限表,输入y
Reload privilege tables now? [Y/n] y
 ... 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!

文件参数调优

MariaDB的配置文件位于 /etc/my.cnf

可能你在/etc中找不到my. cnf文件,可以查找一下是否存在my-huge.cnf 、my-large.cnf 、my-medium.cnf、my-small.cnf

cp /usr/share/mysql/my-medium.cnf /etc/my.cnf

下面是优化过配置,可以根据自己的需求自己去调试。

[mysqld]
# 数据库存放目录
datadir=/var/lib/mysql
socket=/var/lib/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

#symbolic-links=0
# 该项为禁止TCP/IP远程连接
skip-external-locking
# 设置索引块缓存大小
key_buffer_size = 8M
# 通信缓冲大小
max_allowed_packet = 1M
# table高速缓存数量
table_open_cache = 4
# 读查询操作能使用缓存大小
sort_buffer_size = 64K
# 顺序读写数据缓冲区使用内存
read_buffer_size = 256K
# 随机读取数据缓冲区使用内存
read_rnd_buffer_size = 256K
# 每个客户端连接时。用于维持连接缓冲
net_buffer_length = 2K
# 每个连接分配内存
thread_stack = 240K
# AIO异步IO,提升性能
#innodb_use_native_aio = 0
# InnoDB使用一个缓冲池来保存索引和原始数据, 这里你设置越大,你在存取表里面数据时所需要的磁盘I/O越少.
innodb_buffer_pool_size=2M
# 检测的表对象的最大数目
performance_schema_max_table_instances=50
# 控制表定义缓存的个数
table_definition_cache=50
# MariaDB允许的最大连接进程数
max_connections=50
# 限制用户最大连接数
max_user_connections=35
# 一个请求的最大连接时间
wait_timeout=10
# mysql关闭交互式连接前等待的秒数
interactive_timeout=15
# 慢查询阀值
long_query_time=5
# 主要用于收集数据库服务器性能参数
performance_schema = off
# Disabling symbolic-links is recommended to prevent assorted security risks
# 符号链接数据库或表可以存储在my.cnf中指定datadir之外的分区或目录
symbolic-links=0
# 开启bin-log日志
#log-bin=mysql-bin
# Recommended in standard MySQL setup
# mysql严格模式
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
# 新创建数据表默认引擎
default_storage_engine = InnoDB


[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »