CentOS7.3下yum安装MariaDB10.3.12并指定utf8字符集

添加MariaDB的yum源,指定安装的版本,然后使用 yum 工具进行安装

参考MariaDB官方网站对应安装方法和步骤

https://downloads.mariadb.org/mariadb/repositories/#mirror=tuna&distro=CentOS&distro_release=centos7-amd64--centos7&version=10.3

[root@SERVER1 ~]# cd /etc/yum.repos.d/
[root@SERVER1 yum.repos.d]# ls   #<==添加MariaDB.repo文件,并添加内容
CentOS7-ctyun.repo CentOS-MariaDB.repo
[root@SERVER1 yum.repos.d]# cat CentOS-MariaDB.repo 
# MariaDB 10.3 CentOS repository list - created 2019-01-11 03:14 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

   yum自动下载并安装MariaDB,如果此步出错,请查看错误提示,检查 /etc/yum.repo.d/ 下文件是否配置正确,并检查 /etc/resolv.conf 中DNS是否配置
  

[root@SERVER1 yum.repos.d]# yum install -y MariaDB-server MariaDB-client

安装完成MariaDB后,启动MariaDB服务并设置为开机自启动

[root@SERVER1 ~]# systemctl start mariadb
[root@SERVER1 ~]# systemctl enable mariadb
[root@SERVER1 yum.repos.d]# ss -lntup|grep mysql 
tcp LISTEN 0 80 :::3306 :::* users:(("mysqld",pid=2560,fd=21))

尝试登录看看,确保安装正常

[root@SERVER1 ~]# mysql -uroot 

Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 8
Server version: 10.3.12-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)]> select version();
+-----------------+
| version() |
+-----------------+
| 10.3.12-MariaDB |
+-----------------+
1 row in set (0.000 sec)

MariaDB [(none)]> q
Bye

为确保数据库的安全性,必须进行一些简单的初始化工作

1)设定root用户密码。
2)删除匿名帐号。
3)禁止root用户从远程登录。
4)删除test数据库并取消对其的访问权限。
5)刷新授权表,让初始化后的设定立即生效。
6)删除无用的用户。
[root@SERVER1 ~]# mysql_secure_installation 
Set root password? [Y/n] Y        <===root用户建立密码
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
... Success!

Remove anonymous users? [Y/n] Y    <===移除匿名用户
... Success!

Disallow root login remotely? [Y/n] Y   <===禁止root用户远程链接

... Success!

Remove test database and access to it? [Y/n] Y  <===移除test库及其表
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reload privilege tables now? [Y/n] Y       <===刷新权限
... Success!
[root@SERVER1 ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 8
Server version: 10.3.12-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)]> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| root | localhost |
| root | server1 |
+------+-----------+
4 rows in set (0.000 sec)

MariaDB [(none)]> drop user 'root'@'server1';
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> drop user 'root'@'::1'; 
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | localhost |
+------+-----------+
2 rows in set (0.000 sec)

MariaDB [(none)]> q
Bye

修改MariaDB字符集为utf8

[root@SERVER1 ~]# cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

[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

[root@SERVER1 ~]# cat /etc/my.cnf.d/mysql-clients.cnf 
#
# These groups are read by MariaDB command-line tools
# Use it for options that affect only one utility
#

[mysql]
default-character-set=utf8

[root@SERVER1 my.cnf.d]# systemctl restart mariadb.service   #<===重启MariaDB服务
[root@SERVER1 my.cnf.d]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 8
Server version: 10.3.12-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 variables like "%character%";
+--------------------------+----------------------------+
| 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.001 sec)

MariaDB [(none)]> show variables like "%collation%";
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database | utf8_unicode_ci |
| collation_server | utf8_unicode_ci |
+----------------------+-----------------+
3 rows in set (0.001 sec)

注:

/etc/my.cnf 是由mariadb-libs生成的
/etc/my.cnf.d/server.cnf是由 mariadb-server生成
/etc/my.cnf.d/client.cnf是由mariadb生成
原文地址:https://www.cnblogs.com/blog-tim/p/10255606.html