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

本文详细介绍了如何在已经安装了MySQL的CentOS服务器上,安装并配置MariaDB,使其与MySQL共存。包括下载MariaDB二进制包,创建安装目录,设置软连接,创建配置文件,修改服务脚本,初始化数据库以及启动服务的步骤,特别提醒在my.cnf配置文件中设置user参数的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
### CentOS 离线环境下 MySQL 安装教程 #### 删除原有 MariaDB 在开始安装之前,需要确认并删除可能已经存在的MariaDB组件。这是因为MariaDBMySQL存在冲突,无法共存于同一环境。 ```bash yum remove mariadb-libs -y ``` 此命令会卸载系统中的MariaDB库文件[^1]。 --- #### 查看 CentOS 版本 为了确保下载适合当前系统的MySQL版本,需先检查CentOS的具体版本号: ```bash lsb_release -a ``` 上述命令可以显示Linux标准基础(LSB)的版本信息,从而帮助判断所需的软件包架构依赖项[^3]。 --- #### 下载 MySQL 软件包 访问官方MySQL社区服务器页面获取对应版本的压缩包或RPM包。具体链接如下所示: [MySQL Community Server (Archived Versions)](https://dev.mysql.com/downloads/mysql/) 根据操作系统位数以及硬件平台选择合适的二进制分发版进行下载[^2]。 --- #### 解压并配置 MySQL 文件 假设已成功将所需文件传输至目标机器,则按照以下流程完成后续设置工作: 1. **创建用户组服务账户** 创建专门用于运行mysqld进程的新用户及其所属群组。 ```bash groupadd mysql useradd -r -g mysql -s /bin/false mysql ``` 2. **解压归档文件到指定目录** 将先前获得的tarball提取出来放置在一个固定位置比如`/usr/local/mysql`下。 ```bash tar zxvf mysql-VERSION-linux-glibcVER-ARCH.tar.gz -C /usr/local/ ln -s /usr/local/mysql-VERSION-linux-glibcVER-ARCH /usr/local/mysql cd /usr/local/mysql ``` 3. **初始化数据存储区域** 执行脚本来建立必要的表空间结构以及其他初始参数定义。 ```bash mkdir /var/lib/mysql chown -R mysql:mysql /var/lib/mysql ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/var/lib/mysql ``` 4. **调整权限分配情况** 修改部分关键路径的所有者属性以便正常启动服务程序。 ```bash chown -R root . chown -R mysql data chmod -R go-rwx . ``` 5. **复制模板配置文档** 使用默认样例作为实际使用的my.cnf依据之一。 ```bash cp support-files/my-default.cnf /etc/my.cnf ``` 6. **设定开机自启机制** 注册systemd单元文件或者传统init.d方式实现自动化管理功能。 对于System V风格的服务控制系统来说: ```bash cp support-files/mysql.server /etc/init.d/mysqld chkconfig --add mysqld chkconfig mysqld on service mysqld start ``` 对于较新的基于systemctl的操作体系而言则执行下面这些指令即可达到相同效果: ```bash systemctl enable mysqld.service systemctl start mysqld.service ``` 7. **安全加固措施实施** 运行wizard引导工具来更改root密码、移除匿名账号等功能增强整体安全性级别。 ```bash bin/mysql_secure_installation ``` 至此整个过程基本结束,可以通过客户端连接测试验证其有效性。 --- ```python import pymysql.cursors connection = pymysql.connect(host='localhost', user='your_username', password='your_password', database='testdb', cursorclass=pymysql.cursors.DictCursor) try: with connection.cursor() as cursor: sql_query = "SELECT 'Installation Successful' AS response" cursor.execute(sql_query) result = cursor.fetchone() finally: connection.close() print(result['response']) ``` 以上Python代码片段展示了如何利用pymysql模块尝试登录本地实例以检验刚才搭建好的MYSQL server是否能够正常使用。 ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值