有两种安装 MariaDB 服务器的方法。您可以安装 CentOS 7 存储库中可用的默认版本,也可以通过手动添加 MariaDB 存储库来安装最新版本。
如果安装过MariaDB或MySQL,使用以下命令彻底删除它们:
yum remove mariadb*
yum remove mysql*
CentOS 存储库中 MariaDB 的默认版本是 MariaDB 5.5。虽然不是最新版本,但它非常稳定,强烈推荐。
要在 CentOS 7 上安装 MariaDB 5.5,请登录到您的服务器实例并使用 yum 包管理器。
[root@ip-172-31-2-24 ~]# yum -y install mariadb
[root@ip-172-31-2-24 ~]# yum -y install mariadb-server
[root@ip-172-31-2-24 ~]# systemctl start mariadb
[root@ip-172-31-2-24 ~]# systemctl status mariadb
[root@ip-172-31-2-24 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
# 登录到MariaDB并设置root用户密码
[root@ip-172-31-2-24 ~]# /usr/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
... 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] 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!
mysql -V
rpm -qi | grep mariadb
mysql -u root -p
在编写本指南时,MariaDB 的最新版本是 MariaDB 10.4。
vi /etc/yum.repos.d/mariadb.repo
粘贴以下内容:
[mariadb]
name = MariaDB baseurl = http://yum.mariadb.org/10.4/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1
yum makecache fast
yum install mariadb-server mariadb-client -y
systemctl start mariadb
systemctl enable mariadb
systemctl status mariadb
# 查看MariaDB 版本
rpm -qi MariaDB-server
要以 root
用户身份访问 MariaDB
数据库,请调用以下命令:
mysql -u root -p
系统将提示您输入 sudo 密码,然后是 root 密码。
# 只允许本地访问
MariaDB [(none)] > create user 'linoxide'@'localhost' IDENTIFIED BY 'Password';
MariaDB [(none)] > GRANT ALL PRIVILEGES ON *.* TO 'linoxide'@'localhost';
# 允许远程访问
MariaDB [(none)] > create user 'linoxide'@'%' IDENTIFIED BY 'Password';
MariaDB [(none)] > GRANT ALL PRIVILEGES ON *.* TO 'linoxide'@'%';
# 使用以下命令注销:
MariaDB [(none)] > quit;
# 使用新用户登录
mysql -u linoxide -p
/etc/my.cnf
文件在 [mysqld] 标签下添加
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
在 [client]
标签下添加
default-character-set=utf8
/etc/my.cnf.d/mysql-clients.cnf
文件在 [mysql]
标签下添加
default-character-set=utf8
[root@mini ~]# systemctl restart mariadb
MariaDB [(none)]> show variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_general_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]>
MariaDB [(none)]> show variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database | utf8_unicode_ci |
| collation_server | utf8_unicode_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)
MariaDB [(none)]>
mariadb默认是拒绝 root 远程登录的。这里用的是 navicat 软件连接数据库。
[root@mini ~]# mysql -u root -p # 先通过本地链接进入数据库
MariaDB [(none)]> use mysql;
MariaDB [mysql]> select host, user from user;
+-----------+------+
| host | user |
+-----------+------+
| 127.0.0.1 | root |
| ::1 | root |
| mini | root |
+-----------+------+
3 rows in set (0.00 sec)
MariaDB [mysql]> update user set host='%' where host='mini';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [mysql]> select host, user from user;
+-----------+------+
| host | user |
+-----------+------+
| % | root |
| 127.0.0.1 | root |
| localhost | root |
+-----------+------+
3 rows in set (0.00 sec)
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
# or
[root@mini ~]# systemctl restart mariadb
注意:刷新权限表是在数据库中,重启服务是在外部命令行中
# 1. 停止 MariaDB 服务。
systemctl stop mariadb.service
# 2. 删除 MariaDB:
yum remove -y mariadb-server mariadb-client
# 3. 删除所有数据文件
$ sudo rm -rf /var/lib/mysql /etc/my.cnf