1 安装并启动 MariaDB
MariaDB 采用 Percona 的 XtraDB 存储引擎替代 MySQL 的 InnoDB,XtraDB 完全兼容 InnoDB。
1.1 安装
MySQL 与 MariaDB 版本对比:
MySQL 版本 | MariaDB 版本 | 主要特点 |
---|---|---|
5.5 | 5.5 | |
5.6 | 10 | |
5.7 | 10.1 | 通过 gtid 彻底解决主从同步的延迟问题 |
- | 10.2 | |
- | 10.3 |
在 下载页面,可以查看每个版本的特点。
通过 Yum 安装
对于 CentOS7,通过 yum 安装的版本是 5.5,比较老了:
yum -y install mariadb-server
通过官网安装最新版本
MariaDB 的官方 yum 源在 这里,可用将下面示例中的版本换成适合你的操作系统的最新版本。例如我用的版本是 http://yum.mariadb.org/10.3/centos7-amd64/
。
首先,配置 yum 源:
vi /etc/yum.repos.d/mariadb.repo
在里面加入如下内容:
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/centos7-amd64/
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
然后,开始安装:
yum -y install mariadb-server
1.2 常用命令
在使用 MariaDB 时,命令中可以直接用 mysql 替换 mariadb,例如 systemctl start mariadb
等价于 systemctl start mysql
。
- 启动 MariaDB:
systemctl start mariadb
- 查看服务状态:
systemctl status mariadb
如果 MariaDB 启动成功,状态中会包括“Active: active (running)”,最后一句话一般是:
Apr 17 14:32:54 VM_157_18_centos systemd[1]: Started MariaDB database server.
- 设置每次服务器重启时都自动启动数据库:
systemctl enable mariadb
上面命令的输出跟下面一样时,表示成功执行:
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
1.3 其他可用命令
mysql mysqld_safe_helper mysql_secure_installation
mysqlaccess mysqldump mysql_setpermission
mysqladmin mysqldumpslow mysqlshow
mysqlbinlog mysql_find_rows mysqlslap
mysqlbug mysql_fix_extensions mysqltest
mysqlcheck mysqlhotcopy mysql_tzinfo_to_sql
mysql_convert_table_format mysqlimport mysql_upgrade
mysqld_multi mysql_install_db mysql_waitpid
mysqld_safe mysql_plugin mysql_zap
2. 对 MariaDB 服务器进行安全设置
MariaDB 包含一个安全脚本,可以改变一些不安全的默认行为,比如远程登录、简单用户设置等。运行安全脚本:
sudo mysql_secure_installation
脚本的每一步都会有详细的解释。第一个交互部分需要你提供 root 密码,因为刚安装的 MariaDB 还没有设置密码,这里直接按 ENTER 进入下一步。下一步设置 root 用户密码。后面的可以全部输入 Y 使用 MariaDB 的建议操作。完整命令如下
# 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] 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.
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.
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!
3. 测试
用 mysqladmin
执行命令,查看数据库版本:
mysqladmin -u root -p version
输出类似下面:
mysqladmin Ver 9.0 Distrib 5.5.56-MariaDB, for Linux on x86_64
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Server version 5.5.56-MariaDB
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /var/lib/mysql/mysql.sock
Uptime: 14 min 59 sec
Threads: 1 Questions: 25 Slow queries: 0 Opens: 1 Flush tables: 2 Open tables: 27 Queries per second avg: 0.027
登录数据库
以 root 身份登录后,可以执行任何操作:
mysql -u root -p