Linux CentOS下MariaDB安装(mariaDB 和 mysql共存)

齐阳
2023-12-01

MariaDB安装(mariaDB 和 mysql共存)

近期工作中遇到再同一台服务器下部署多个mysql服务,在linux (centOS) 下已安装mysql的场景下,再安装一个MariaDB服务,并且同时运行启动。在这个过程中遇到很多的坑,长时间摸索后,终于找到结局方案如下:

  1. 下载MariaDB的二进制安装包,官网下载地址:http://mirror.biznetgio.com/mariadb//mariadb-10.2.39/bintar-linux-x86_64/mariadb-10.2.39-linux-x86_64.tar.gz

    如果下载速度太慢,可以使用迅雷来下载,或者找一台带宽比较大的服务器来下载,命令如下:

    wget http://mirror.biznetgio.com/mariadb//mariadb-10.2.39/bintar-linux-x86_64/mariadb-10.2.39-linux-x86_64.tar.gz
    
  2. 下载完成后将其上传到linux服务器上(上传到自己的安装目录就可以,本文假定为:/opt)

    [root@bmserver opt]# ll
    total 455252
    -rw-r--r--. 1 root root 466170452 Jul 30 17:14 mariadb-10.2.39-linux-x86_64.tar.gz
    
  3. 然后查看当前服务器上mysql的安装情况

    [root@bmserver ~]# rpm -qa mysql*
    [root@bmserver ~]# ps axf | grep mysqld
    25041 ?        S      0:00  |                               \_ /bin/sh /opt/xinghai/mysql/software/mysql/bin/mysqld_safe --datadir=/opt/xinghai/mysql/data --pid-file=/opt/xinghai/mysql/pids/mysqld.pid
    25704 ?        Sl     0:00  |                               |   \_ /opt/xinghai/mysql/software/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/xinghai/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/opt/xinghai/mysql/logs/mysqld.log --pid-file=/opt/xinghai/mysql/pids/mysqld.pid --socket=/opt/xinghai/mysql/mysql.sock --port=3306
    25798 pts/0    S+     0:00          \_ grep --color=auto mysqld
    
  4. 明确情况后,创建mariadb的安装目录

    [root@localhost /]# cd opt
    [root@localhost opt]# mkdir mariadb
    [root@localhost opt]# cd mariadb/
    [root@localhost mariadb]# mkdir mariadb-data
    
  5. 然后将数据二进制压缩包复制到 /opt/mariadb文件夹下,并解压,设置软连接

    [root@localhost mariadb]# cp ../mariadb-10.2.39-linux-x86_64.tar.gz ./
    [root@localhost mariadb]# tar -zxvf mariadb-10.2.39-linux-x86_64.tar.gz
    ……等待解压完后,设置软连接
    [root@localhost mariadb]# ln -s mariadb-10.2.39-linux-x86_64 mariadb
    [root@localhost mariadb]# ll
    total 455164
    lrwxrwxrwx.  1 root root        28 Aug  4 15:42 mariadb -> mariadb-10.2.39-linux-x86_64
    drwxrwxr-x. 12 etcd etcd       257 Jun 22 10:19 mariadb-10.2.39-linux-x86_64
    -rw-r--r--.  1 root root 466085204 Aug  4 15:35 mariadb-10.2.39-linux-x86_64.tar.gz
    drwxr-xr-x.  2 root root         6 Aug  4 15:34 mariadb-data
    
  6. 如果想给mariadb单独设置一个用户来管理,先新增用户,更改mariadb的文件夹的权限

    [root@localhost opt]# groupadd --system mariadb
    [root@localhost opt]# useradd -c "MariaDB Server" -d /opt/mariadb -g mariadb --system mariadb
    [root@localhost opt]# chown -R mariadb:mariadb mariadb-5.5.24-linux-x86_64/
    [root@localhost opt]# chown -R mariadb:mariadb mariadb-data/
    
  7. 创建mariadb的默认配置文件,可以直接从mariadb/support-files文件夹下复制一个模板到mariadb-data文件夹下,重名为my.cnf;

    然后编辑文件,添加以下配置

    [root@localhost mariadb]# cp ./support-files/my-medium.cnf ../mariadb-data/my.cnf
    [root@localhost mariadb]# vim ../mariadb-data/my.cnf
    

    修改配置如下:

    [client]
    port		= 3307
    socket		= /opt/mariadb-data/mariadb.sock
    
    [mysqld]
    datadir         = /opt/mariadb-data
    basedir         = /opt/mariadb
    port			= 3307
    socket			= /opt/mariadb-data/mariadb.sock
    user            = mariadb
    
  8. 配置完成后将mysql启动服务,复制到 /etc/init.d文件夹下,然后编辑这个服务,修改如下:

    [root@localhost mariadb]# cp support-files/mysql.server /etc/init.d/mariadb
    [root@localhost mariadb]# vim /etc/init.d/mariadb
    

    修改配置如下:分别对应修改,45、46、57行

    - # Provides: mysql
    + # Provides: mariadb
    - basedir=
    + basedir=/opt/mariadb
    - datadir=
    + datadir=/opt/mariadb-data
    - lock_file_path="$lockdir/mysql"
    + lock_file_path="$lockdir/mariadb"
    

    还需要在**$bindir/mysqld_safe** 后面加上 –defaults-file=/opt//mariadb/mariadb-data/my.cnf;如下:(对应310行)

    # Give extra arguments to mysqld with the my.cnf file. This script
    # may be overwritten at next upgrade.
    $bindir/mysqld_safe --defaults-file=/opt//mariadb/mariadb-data/my.cnf --datadir="$datadir" --pid-file="$mysqld_pid_file_path" "$@" &
    

    同样的修改,需要在**$bindir/mysqladmin** 后面加上 –defaults-file=/opt/mariadb-data/my.cnf ,如下:(对应261行)

     wait_for_ready () {
     
       i=0
       while test $i -ne $service_startup_timeout ; do
     
         if $bindir/mysqladmin --defaults-file=/opt/mariadb-data/my.cnf  ping >/dev/null 2>&1; then
    
    

    配置完成后,保存退出;

  9. 然后开始执行初始化 mysql_install_db

    [root@localhost mariadb]# pwd
    /opt/mariadb/mariadb
    [root@localhost mariadb]# ./scripts/mysql_install_db --user=root --defaults-file=/opt/mariadb/mariadb-data/my.cnf
    
  10. 初始化成功后,可以启动服务了

    [root@localhost mariadb]# /etc/init.d/mariadb start
    Starting mariadb (via systemctl):                          [  OK  ]
    
  11. 启动成功后,想要测试是否成功:

    [root@localhost ~]# mysql --defaults-file=/opt/mariadb/mariadb-data/my.cnf -uroot -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 27
    Server version: 5.5.5-10.2.39-MariaDB-log MariaDB Server
    
    Copyright (c) 2000, 2021, Oracle and/or its affiliates.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> 
    
  12. 设置开机自动启动

    [root@localhost ~]# chkconfig --add mariadb
    [root@localhost ~]# chkconfig --list
    
    Note: This output shows SysV services only and does not include native
          systemd services. SysV configuration data might be overridden by native
          systemd configuration.
    
          If you want to list systemd services use 'systemctl list-unit-files'.
          To see services enabled on particular target use
          'systemctl list-dependencies [target]'.
    
    mariadb        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
    netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
    network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
    

备注:大坑 在设置my.cnf文件时,一定要设置user配置,否则在后面启动时一直报错,还没有日志可查!!!

[mysqld]
user     = mariadb
 类似资料: