阿里云服务器使用二进制方式安装Mariadb

软件包的安装方式有三种:源码包编译安装、二进制包安装、rpm包安装。这三种安装方法都各有优劣。RPM安装:适合小环境,核心功能都具备,快速搭建环境,但它的版本一般都不会太高;二进制安装:它是发布出来时预先编译过的,既避免了编译的麻烦,又提供了增强功能;源码包编译安装:它的要求高,要有编译环境,编译时可指定几乎所有选项,根据需求进行定制,可满足你的所有选择。

由于centos7系统RPM包自带Mariadb包版本过低,不适合学习使用,因此今天就通过二进制包安装的方式来解决版本问题。

安装步骤如下:

1,下载二进制包;

2,解压到安装目录,安装相应的依赖包;

3、准备配置文件;

4、设置启动服务项;

5、测试是否可以正常的登录;

6、数据库安全加固;

7、安全初始化;

安装过程如下:

1,下载二进制包:

二进制包的下载可以从官方网站下载,也可以从国内清华大学官网下载,下载地址如下:  

  • 项目官方:https://downloads.mariadb.org/mariadb/repositories/
  • 国内镜像:https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/

这里下载的Mariadb包的版本为mariadb-10.2.23-linux-x86_64.tar.gz。

2,解压到安装目录,安装相应的依赖包:

(1)安装相应的依赖包:


   
   
   
   
  1. [root@localhost ~]# yum -ql libaio-devel numactl-devel #环境依赖包
  2. [root@localhost ~]# yum install lrzsz-0 .12 .20-36 .el7 .x86_64 #负责文件的上传和下载

(2)创建mysql用户,创建/data/mysql文件夹,计划将数据库安装到/data/mysql文件夹下面:


   
   
   
   
  1. # useradd -r -s /sbin/nologin -d / data/mysql mysql #创建mysql的系统账号,
  2. 指定shell类型为nologin,指定安装目录为/ data/mysql

(3)将下载的二进制包进行解压,解压到/usr/local/目录下面:

#tar xvf mariadb-10.2.25-linux-x86_64.tar.gz -C /usr/local/ #注意一定要将包解压到/usr/local/目录下面。
   
   
   
   

3、准备配置文件:     

 (1)将解压到/usr/local的创建软连接并修改权限:


   
   
   
   
  1. [root@host7 local] # ln -s mariadb-10.2.25-linux-x86_64/ mysql
  2. [root@localhost local] # ll
  3. lrwxrwxrwx 1 root root 29 Jul 3 22: 28 mysql -> mariadb- 10.2. 25-linux-x86_64/
  4. [root@localhost local] #chown -R root.root mysql/ #将文件夹的权限修改为root
  5. [root@localhost mysql] # ll
  6. total 176
  7. drwxr-xr- x 2 root root 4096 Jul 4 09: 06 bin
  8. -rw-r--r-- 1 root root 17987 Jun 15 02: 29 COPYING
  9. -rw-r--r-- 1 root root 2354 Jun 15 02: 29 CREDITS
  10. drwxrwxr- x 3 root root 18 Jun 15 23: 35 data
  11. -rw-r--r-- 1 root root 8245 Jun 15 02: 29 EXCEPTIONS-CLIENT
  12. drwxrwxr- x 3 root root 19 Jun 15 23: 35 include
  13. -rw-r--r-- 1 root root 8694 Jun 15 02: 29 INSTALL-BINARY
  14. drwxr-xr- x 5 root root 335 Mar 15 01: 07 lib
  15. drwxrwxr- x 4 root root 30 Jun 15 23: 35 man
  16. drwxrwxr- x 11 root root 4096 Jun 15 23: 35 mysql-test
  17. -rw-r--r-- 1 root root 2843 Jun 15 02: 29 README.md
  18. -rw-r--r-- 1 root root 19477 Jun 15 02: 29 README-wsrep
  19. drwxrwxr- x 2 root root 30 Jul 3 22: 50 scripts
  20. drwxrwxr- x 32 root root 4096 Jun 15 23: 35 share
  21. drwxrwxr- x 4 root root 4096 Jun 15 23: 35 sql-bench
  22. drwxrwxr- x 3 root root 275 Jun 15 23: 35 support-files
  23. -rw-r--r-- 1 root root 86263 Jun 15 02: 29 THIRDPARTY

(2)进入/mysql/scripts 目录,运行里面的 脚本,设置数据库在哪个目录创建:


   
   
   
   
  1. 数据库创建脚本:
  2. [root@localhost mysql] # cd scripts/
  3. [root@localhost scripts] # ll
  4. total 20
  5. -rwxr-xr- x 1 root root 19257 Jun 15 02: 34 mysql_install_db #运行该脚本可创建数据库
  6. 注意运行该目录使用相对路径的方法,否者运行过程会报错:
  7. [root@localhost mysql] # ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql #指定安装路径为/data/myssql,以mysql的用户进行运行。

  (3)将 /usr/local/mysql/suppert-files/my-huge.cnf文件下数据库配置大小文件拷贝到/data/mysql/my.cnf目录下并修改路径:


   
   
   
   
  1. 将包内配置文件拷贝到指定目录下,并修改文件名称:
  2. [root@localhost ~] # cp /usr/local/mysql/support-files/my-huge.cnf /etc/mysql/my.cnf
  3. 修改/etc/myaql/my.cnf文件:
  4. [root@localhost ~] # vim /etc/mysql/my.cnf
  5. # The MySQL server
  6. [mysqld]
  7. datadir= /data/mysql #将路径修改为/data/mysql
  8. port = 3306
  9. socket = /tmp/mysql.sock

     4,设置启动服务项:

(1)   将/usr/local/mysql/suppert-files下的mysql.server 拷贝到 /etc/init.d/mysql目录下,并设置成启动服务项:


   
   
   
   
  1. [root@localhost ~] # cp /usr/local/mysql//support-files/mysql.server /etc/init.d/mysqld
  2. 检查服务项,没有Mysqldq启动服务:
  3. [root@localhost ~] # chkconfig --list
  4. Note: This output shows SysV services only and does not include native
  5. systemd services. SysV configuration data might be overridden by native
  6. systemd configuration.
  7. If you want to list systemd services use 'systemctl list-unit-files'.
  8. To see services enabled on particular target use
  9. 'systemctl list-dependencies [target]'.
  10. netconsole 0: off 1: off 2: off 3: off 4: off 5: off 6: off
  11. network 0: off 1: off 2: on 3: on 4: on 5: on 6: off
  12. 将mysqld的服务项加入到服务启动脚本里:
  13. [root@localhost ~] # chkconfig --add mysqld
  14. 启动mysql服务:
  15. [root@localhost ~] # service mysqld start
  16. [root@localhost ~] # chkconfig --list
  17. Note: This output shows SysV services only and does not include native
  18. systemd services. SysV configuration data might be overridden by native
  19. systemd configuration.
  20. If you want to list systemd services use 'systemctl list-unit-files'.
  21. To see services enabled on particular target use
  22. 'systemctl list-dependencies [target]'.
  23. mysqld 0: off 1: off 2: on 3: on 4: on 5: on 6: off #服务已经启动
  24. netconsole 0: off 1: off 2: off 3: off 4: off 5: off 6: off
  25. network 0: off 1: off 2: on 3: on 4: on 5: on 6: off

    (2)将Mysql的bin路径添加到全局路径下,并使其生效:


   
   
   
   
  1. [root@localhost mysql] # echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh #将路径添加到全局路径下。
  2. [root@localhost mysql] # . etc/profile.d/mysql.sh 使该设置生效

              

   5,测试是否可以正常的登录:


   
   
   
   
  1. # mysql
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MariaDB connection id is 34
  4. Server version: 10.2. 25-MariaDB-log MariaDB Server
  5. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  6. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  7. MariaDB [(none)]> MariaDB [(none)]> create database testdb; #测试创建数据库;
  8. MariaDB [(none)]> show databases;
  9. +--------------------+
  10. | Database |
  11. +--------------------+
  12. | db1 | |
  13. | information_schema |
  14. | mysql |
  15. | performance_schema |
  16. | testdb |
  17. +--------------------+
  18. 5 rows in set (0.00 sec)

6,数据库安全初始化:


   
   
   
   
  1. 使用 /usr/local/mysql/bin 下面的mysql_secure_installation脚本进行安全加固:
  2. [root@localhost ~] # mysql_secure_installation
  3. NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
  4. SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
  5. In order to log into MariaDB to secure it, we 'll need the current
  6. password for the root user. If you 've just installed MariaDB, and
  7. you haven 't set the root password yet, the password will be blank,
  8. so you should just press enter here.
  9. Enter current password for root (enter for none): 输入新密码,并确认新密码,后面选项采用默认即可。

7,测试登陆:


   
   
   
   
  1. [root@localhost ~]# mysql -uroot -p #测试是否可用密码正确登陆
  2. Enter password:
  3. Welcome to the MariaDB monitor. Commands end with ; or \g.
  4. Your MariaDB connection id is 10
  5. Server version: 10.2. 25-MariaDB-log MariaDB Server
  6. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  7. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  8. MariaDB [(none)]>

至此,Mariadb的二进制安装过程已经完成,可以使用该数据库了。

发布了18 篇原创文章 · 获赞 13 · 访问量 5033

你可能感兴趣的:(MariaDB数据库)