MARIADB安装

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MARIADB安装相关的知识,希望对你有一定的参考价值。

二进制安装MARIADB

1、解压到/usr/local/
[[email protected] ~]# tar xvf mariadb-10.2.23-linux-x86_64.tar.gz -C /usr/local/

2、软连接
[[email protected] local]# ln -s mariadb-10.2.23-linux-x86_64/ mysql

3、更改所有者,所属组
[[email protected] local]# chown -R root:root mysql/

4、创建系统用户,用户组
[[email protected] local]# groupadd -r -g 66 mysql
[[email protected] local]# useradd -r -g mysql -u 66 -s /sbin/nologin -d /data/mysql mysql

5、运行脚本,指定数据存放位置
/usr/local/mysql/scripts/mysql_install_db
[[email protected] mysql]# scripts/mysql_install_db --user=mysql --datadir=/data/mysql

6、创建配置文件
[[email protected] mysql]# mkdir /etc/mysql/
[[email protected] mysql]# pwd
/usr/local/mysql
[[email protected] mysql]# cp support-files/my-huge.cnf /etc/mysql/my.cnf
[[email protected] mysql]# vim /etc/mysql/my.cnf
[mysqld]        #在mysqld加入数据路径
datadir=/data/mysql

7、制作启动脚本
[[email protected] mysql]# pwd
/usr/local/mysql
[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld

8、加入chkconfig
[[email protected] mysql]# chkconfig --add mysqld

9、启动服务
[[email protected] mysql]# service mysqld start
[[email protected] mysql]# ss -nlt
State      Recv-Q Send-Q Local Address:Port                Peer Address:Port              
LISTEN     0      128                *:22                             *:*                  
LISTEN     0      100        127.0.0.1:25                             *:*                  
LISTEN     0      80                :::3306                          :::*                  
LISTEN     0      128               :::22                            :::*                  
LISTEN     0      100              ::1:25                            :::*      

10、加入PATH环境变量
[[email protected] mysql]# echo PATH=‘/usr/local/mysql/bin:$PATH‘ > /etc/profile.d/mysql.sh
[[email protected] mysql]# . /etc/profile.d/mysql.sh
[[email protected] mysql]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.2.23-MariaDB-log 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)]> 

11、跑加固脚本
[[email protected] mysql]# mysql_secure_installation

源码编译安装MARIADB
1、准备编译软件

yum -y install bison bison-devel zlib-devel libcurl-devel libarchive-devel boostdevel gcc gcc-c++ cmake ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel 

2、创建用户

[[email protected] ~]# useradd -r -s /sbin/nologin -d /data/mysql -m mysql       
[[email protected] ~]# ll -d /data/mysql/
drwx------. 2 mysql mysql 4096 Apr  2 19:36 /data/mysql/

3、解压,cmake编译

[[email protected] ~]# tar xvf mariadb-10.2.23.tar.gz
[[email protected] ~]# cd mariadb-10.2.23
[[email protected] mariadb-10.2.23]# cmake . -DCMAKE_INSTALL_PREFIX=/app/mysql -DMYSQL_DATADIR=/data/mysql/ -DSYSCONFDIR=/etc/mysql -DMYSQL_USER=mysql -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITHOUT_MROONGA_STORAGE_ENGINE=1 -DWITH_DEBUG=0 -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0 -DENABLED_LOCAL_INFILE=1 -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
make && make install
[[email protected] mariadb-10.2.23]# make && make install
如果出错,执行rm -f CMakeCache.txt

4、加入环境变量

[[email protected] mariadb-10.2.23]# echo PATH=‘/app/mysql/bin:$PATH‘ > /etc/profile.d/mysql.sh
[[email protected] mariadb-10.2.23]# . /etc/profile.d/mysql.sh

5、生成数据库

[[email protected] mysql]# cd /app/mysql/
[[email protected] mysql]# scripts/mysql_install_db --datadir=/data/mysql/ --user=mysql

6、准备配置文件

[[email protected] mysql]# mkdir /etc/mysql/
[[email protected] mysql]# cp /app/mysql/support-files/my-huge.cnf /etc/mysql/mysql.cnf

7、准备启动脚本

[[email protected] mysql]# cp /app/mysql/support-files/mysql.server /etc/init.d/mysqld

8、启动服务

[[email protected] ~]# cp /etc/mysql/mysql.cnf /etc/my.cnf --backup
[[email protected] mysql]# service mysqld start

yum多实例安装mariadb

1、规划目录

[[email protected] ~]# mkdir /mysql/{3306,3307,3308}/{data,etc,socket,bin,log,pid} -p
[[email protected] ~]# tree /mysql/
/mysql/
├── 3306
│?? ├── bin
│?? ├── data
│?? ├── etc
│?? ├── log
│?? ├── pid
│?? └── socket
├── 3307
│?? ├── bin
│?? ├── data
│?? ├── etc
│?? ├── log
│?? ├── pid
│?? └── socket
└── 3308
    ├── bin
    ├── data
    ├── etc
    ├── log
    ├── pid
    └── socket

2、修改文件属性

[[email protected] ~]# chown -R mysql:mysql /mysql/

3、生成数据库

[[email protected] ~]# ls /var/lib/mysql/
aria_log.00000001  ibdata1      ib_logfile1  mysql.sock          test
aria_log_control   ib_logfile0  mysql        performance_schema
[[email protected] ~]# mysql_install_db --user=mysql --datadir=/mysql/3306/data
[[email protected] ~]# mysql_install_db --user=mysql --datadir=/mysql/3307/data
[[email protected] ~]# mysql_install_db --user=mysql --datadir=/mysql/3308/data

4、创建配置文件

[[email protected] ~]# cp /etc/my.cnf /mysql/3306/etc/
[[email protected] ~]# vim /mysql/3306/etc/my.cnf
[mysqld]
port=3306
datadir=/mysql/3306/data
socket=/mysql/3306/socket/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

[mysqld_safe]
log-error=/mysql/3306/log/mariadb.log
pid-file=/mysql/3306/pid/mariadb.pid

#
# include all files from the config directory

[[email protected] ~]# cp /mysql/3306/etc/my.cnf /mysql/3307/etc/my.cnf
[[email protected] bin]# sed -i ‘s/3306/3307/‘ /mysql/3307/etc/my.cnf

[[email protected] ~]# cp /mysql/3306/etc/my.cnf /mysql/3308/etc/my.cnf
[[email protected] bin]# sed -i ‘s/3306/3308/‘ /mysql/3308/etc/my.cnf

5、准备服务脚本

[[email protected] ~]# cd /mysql/3306/bin/
[[email protected] bin]# vim mysqld
#!/bin/bash

port=3306
mysql_user="root"
mysql_pwd=""
cmd_path="/usr/bin"
mysql_basedir="/mysql"
mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock"

function_start_mysql()
{
    if [ ! -e "$mysql_sock" ];then
      printf "Starting MySQL...\n"
      ${cmd_path}/mysqld_safe --defaults-file=${mysql_basedir}/${port}/etc/my.cnf  &> /dev/null  &
    else
      printf "MySQL is running...\n"
      exit
    fi
}

function_stop_mysql()
{
    if [ ! -e "$mysql_sock" ];then
       printf "MySQL is stopped...\n"
       exit
    else
       printf "Stoping MySQL...\n"
       ${cmd_path}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown
   fi
}

function_restart_mysql()
{
    printf "Restarting MySQL...\n"
    function_stop_mysql
    sleep 2
    function_start_mysql
}

case $1 in
start)
    function_start_mysql
;;
stop)
    function_stop_mysql
;;
restart)
    function_restart_mysql
;;
*)
    printf "Usage: ${mysql_basedir}/${port}/bin/mysqld {start|stop|restart}\n"
esac

...
...
[[email protected] bin]# chmod +x mysqld 
[[email protected] bin]# cp mysqld /mysql/3307/bin/
[[email protected] bin]# cp mysqld /mysql/3308/bin/
[[email protected] bin]# vim /mysql/3307/bin/mysqld
[[email protected] bin]# vim /mysql/3308/bin/mysqld
#!/bin/bash
port=3307
#!/bin/bash
port=3308

6、启动服务

[[email protected] bin]# /mysql/3306/bin/mysqld start
Starting MySQL...
[[email protected] bin]# /mysql/3307/bin/mysqld start
Starting MySQL...
[[email protected] bin]# /mysql/3308/bin/mysqld start
Starting MySQL...

[[email protected] bin]# ss -nlt
State      Recv-Q Send-Q    Local Address:Port                   Peer Address:Port              
LISTEN     0      50                    *:3306                              *:*                  
LISTEN     0      50                    *:3307                              *:*                  
LISTEN     0      50                    *:3308                              *:*         

7、连接

[[email protected] bin]# mysql -P3306 -S /mysql/3306/socket/mysql.sock

8、关闭服务

[[email protected] bin]# /mysql/3308/bin/mysqld stop
Stoping MySQL...

9、用mysqladmin设置密码

[[email protected] bin]# mysqladmin -S /mysql/3307/socket/mysql.sock -uroot password 123456

[[email protected] bin]# mysql -P3307 -S /mysql/3307/socket/mysql.sock 

10、避免关闭服务需要输入密码,需要在文件中将密码输入

[[email protected] bin]# vim /mysql/3307/bin/mysqld
mysql_pwd="123456"      

以上是关于MARIADB安装的主要内容,如果未能解决你的问题,请参考以下文章

基于CentOS 6.8平台最新源代码包MariaDB数据库企业版安装

centos7安装mariadb(mysql的替代品)

MariaDB三种方法安装及多实例实现

windows安装mariaDB失败,服务不能启动

LAMP的基本配置

手把手教你在Linux(Deepin)安装MySQL和MariaDB