前言

在购买阿里云服务器后通常第一件事情是配置程序运行环境,我这里选用的是Alibaba Cloud Linux 2系统,准备安装mysql数据库,而mysql数据库在无法通过系统库命令下载安装,所以选用mysql的开源版本maridb数据库,他们的功能使用是一样的,就和centos与redhat一样。

好了,下面一起来看下怎么在阿里云 Alibaba Cloud Linux 系统安装 maridb数据库吧

安装步骤

1、安装命令

yum install mariadb-server -y

命令执行结果

Loaded plugins: fastestmirror, langpacks, releasever-adapter, update-motd
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package mariadb-server.x86_64 1:5.5.68-1.1.al7 will be installed
--> Processing Dependency: mariadb(x86-64) = 1:5.5.68-1.1.al7 for package: 1:mariadb-server-5.5.68-1.1.al7.x86_64
--> Processing Dependency: perl-DBI for package: 1:mariadb-server-5.5.68-1.1.al7.x86_64
--> Processing Dependency: perl-DBD-MySQL for package: 1:mariadb-server-5.5.68-1.1.al7.x86_64
--> Processing Dependency: perl(Data::Dumper) for package: 1:mariadb-server-5.5.68-1.1.al7.x86_64
--> Processing Dependency: perl(DBI) for package: 1:mariadb-server-5.5.68-1.1.al7.x86_64
--> Running transaction check
---> Package mariadb.x86_64 1:5.5.68-1.1.al7 will be installed
---> Package perl-DBD-MySQL.x86_64 0:4.023-6.1.al7 will be installed
---> Package perl-DBI.x86_64 0:1.627-4.1.al7 will be installed
--> Processing Dependency: perl(RPC::PlServer) >= 0.2001 for package: perl-DBI-1.627-4.1.al7.x86_64
--> Processing Dependency: perl(RPC::PlClient) >= 0.2000 for package: perl-DBI-1.627-4.1.al7.x86_64
---> Package perl-Data-Dumper.x86_64 0:2.145-3.1.al7 will be installed
--> Running transaction check
---> Package perl-PlRPC.noarch 0:0.2020-14.1.al7 will be installed
--> Processing Dependency: perl(Net::Daemon) >= 0.13 for package: perl-PlRPC-0.2020-14.1.al7.noarch
--> Processing Dependency: perl(Net::Daemon::Test) for package: perl-PlRPC-0.2020-14.1.al7.noarch
--> Processing Dependency: perl(Net::Daemon::Log) for package: perl-PlRPC-0.2020-14.1.al7.noarch
--> Processing Dependency: perl(Compress::Zlib) for package: perl-PlRPC-0.2020-14.1.al7.noarch
--> Running transaction check
---> Package perl-IO-Compress.noarch 0:2.061-2.1.al7 will be installed
--> Processing Dependency: perl(Compress::Raw::Zlib) >= 2.061 for package: perl-IO-Compress-2.061-2.1.al7.noarch
--> Processing Dependency: perl(Compress::Raw::Bzip2) >= 2.061 for package: perl-IO-Compress-2.061-2.1.al7.noarch
---> Package perl-Net-Daemon.noarch 0:0.48-5.1.al7 will be installed
--> Running transaction check
---> Package perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.1.al7 will be installed
---> Package perl-Compress-Raw-Zlib.x86_64 1:2.061-4.1.al7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================== Package                                  Arch                    Version                           Repository                Size 
===================================================================================================================================Installing:
 mariadb-server                           x86_64                  1:5.5.68-1.1.al7                  updates                   11 M 
Installing for dependencies:
 mariadb                                  x86_64                  1:5.5.68-1.1.al7                  updates                  8.9 M 
 perl-Compress-Raw-Bzip2                  x86_64                  2.061-3.1.al7                     base                      32 k 
 perl-Compress-Raw-Zlib                   x86_64                  1:2.061-4.1.al7                   base                      57 k 
 perl-DBD-MySQL                           x86_64                  4.023-6.1.al7                     base                     140 k 
 perl-DBI                                 x86_64                  1.627-4.1.al7                     base                     803 k 
 perl-Data-Dumper                         x86_64                  2.145-3.1.al7                     base                      47 k 
 perl-IO-Compress                         noarch                  2.061-2.1.al7                     base                     260 k 
 perl-Net-Daemon                          noarch                  0.48-5.1.al7                      base                      51 k 
 perl-PlRPC                               noarch                  0.2020-14.1.al7                   base                      36 k 

Transaction Summary
===================================================================================================================================Install  1 Package (+9 Dependent packages)

Total download size: 21 M
Installed size: 110 M
Is this ok [y/d/N]: y
Downloading packages:
(1/10): perl-Compress-Raw-Zlib-2.061-4.1.al7.x86_64.rpm                                                     |  57 kB  00:00:00     
(2/10): perl-Compress-Raw-Bzip2-2.061-3.1.al7.x86_64.rpm                                                    |  32 kB  00:00:00     
(3/10): perl-DBD-MySQL-4.023-6.1.al7.x86_64.rpm                                                             | 140 kB  00:00:00     
(4/10): perl-Data-Dumper-2.145-3.1.al7.x86_64.rpm                                                           |  47 kB  00:00:00     
(5/10): perl-DBI-1.627-4.1.al7.x86_64.rpm                                                                   | 803 kB  00:00:00     
(6/10): perl-IO-Compress-2.061-2.1.al7.noarch.rpm                                                           | 260 kB  00:00:00     
(7/10): perl-Net-Daemon-0.48-5.1.al7.noarch.rpm                                                             |  51 kB  00:00:00     
(8/10): perl-PlRPC-0.2020-14.1.al7.noarch.rpm                                                               |  36 kB  00:00:00     
(9/10): mariadb-server-5.5.68-1.1.al7.x86_64.rpm                                                            |  11 MB  00:00:00     
(10/10): mariadb-5.5.68-1.1.al7.x86_64.rpm                                                                  | 8.9 MB  00:00:00     
-----------------------------------------------------------------------------------------------------------------------------------Total                                                                                               36 MB/s |  21 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : perl-Data-Dumper-2.145-3.1.al7.x86_64                                                                          1/10 
  Installing : 1:mariadb-5.5.68-1.1.al7.x86_64                                                                                2/10 
  Installing : perl-Compress-Raw-Bzip2-2.061-3.1.al7.x86_64                                                                   3/10 
  Installing : 1:perl-Compress-Raw-Zlib-2.061-4.1.al7.x86_64                                                                  4/10 
  Installing : perl-IO-Compress-2.061-2.1.al7.noarch                                                                          5/10 
  Installing : perl-Net-Daemon-0.48-5.1.al7.noarch                                                                            6/10 
  Installing : perl-PlRPC-0.2020-14.1.al7.noarch                                                                              7/10 
  Installing : perl-DBI-1.627-4.1.al7.x86_64                                                                                  8/10 
  Installing : perl-DBD-MySQL-4.023-6.1.al7.x86_64                                                                            9/10 
  Installing : 1:mariadb-server-5.5.68-1.1.al7.x86_64                                                                        10/10 
  Verifying  : perl-DBD-MySQL-4.023-6.1.al7.x86_64                                                                            1/10 
  Verifying  : perl-Data-Dumper-2.145-3.1.al7.x86_64                                                                          2/10 
  Verifying  : perl-Net-Daemon-0.48-5.1.al7.noarch                                                                            3/10 
  Verifying  : perl-DBI-1.627-4.1.al7.x86_64                                                                                  4/10 
  Verifying  : perl-PlRPC-0.2020-14.1.al7.noarch                                                                              5/10 
  Verifying  : 1:perl-Compress-Raw-Zlib-2.061-4.1.al7.x86_64                                                                  6/10 
  Verifying  : perl-Compress-Raw-Bzip2-2.061-3.1.al7.x86_64                                                                   7/10 
  Verifying  : perl-IO-Compress-2.061-2.1.al7.noarch                                                                          8/10 
  Verifying  : 1:mariadb-5.5.68-1.1.al7.x86_64                                                                                9/10 
  Verifying  : 1:mariadb-server-5.5.68-1.1.al7.x86_64                                                                        10/10 

Installed:
  mariadb-server.x86_64 1:5.5.68-1.1.al7

Dependency Installed:
  mariadb.x86_64 1:5.5.68-1.1.al7                                 perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.1.al7
  perl-Compress-Raw-Zlib.x86_64 1:2.061-4.1.al7                   perl-DBD-MySQL.x86_64 0:4.023-6.1.al7
  perl-DBI.x86_64 0:1.627-4.1.al7                                 perl-Data-Dumper.x86_64 0:2.145-3.1.al7
  perl-IO-Compress.noarch 0:2.061-2.1.al7                         perl-Net-Daemon.noarch 0:0.48-5.1.al7
  perl-PlRPC.noarch 0:0.2020-14.1.al7

Complete!

2、启动数据

systemctl start mariadb

3、修改mysql编码 utf-8

vim /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

最终结果如下:

[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
#

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

[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

4、重启数据库

systemctl restart mariadb

5、数据库初始化与配置

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!

6、通过密码登陆数据库

mysql -uroot -p # 执行并输入密码
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.68-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)]> 

到此maridb数据库安装完毕

总结

如果是小项目在Linux下我还是推荐使用maridb数据库,安装比较方便,和mysql类似,当然如过数据量比较大了还是需要所数据库优化的,这个mysql也是一样的