Linux 下离线安装 MariaDB

  1. 下载 MariaDB 安装包

    下载地址: https://downloads.mariadb.org/

  2. 卸载系统自带的mysql
    #检查mariadb是否存在
    [root@cdh01 local]# rpm -qa | grep mariadb
    mariadb-libs-5.5.60-1.el7_5.x86_64
    #卸载
    [root@cdh01 local]# rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
    [root@cdh01 local]# find / -name mysql
    /var/lib/pcp/config/pmlogconf/mysql
    /usr/lib64/mysql
    /usr/share/mysql
    [root@cdh01 local]# rm -rf /var/lib/pcp/config/pmlogconf/mysql
    # 手动删除  数据库目录
    [root@cdh01 local]# rm -rf /var/lib/mysql/
    # 手动删除  数据库配置文件
    [root@cdh01 local]# rm -rf /usr/share/mysql/
    
  3. 解压 MariaDB 安装包
    [root@cdh01 local]# tar -zxvf mariadb-10.4.11-linux-x86_64.tar.gz
    [root@cdh01 local]# ln -s mariadb-10.4.11-linux-x86_64 mariadb
    
  4. 创建 mysql 用户及用户组并更改所属的组和用户
    [root@cdh01 local]# groupadd mysql
    [root@cdh01 local]# useradd -g mysql mysql
    [root@cdh01 local]# chown -R mysql:mysql mariadb/
    
  5. 新建配置文件my.cnf

    新建配置文件 /etc/my.cnf,并添加以下配置

    [mysql]
    #设置mysql客户端默认字符集
    default-character-set=utf8 
    [mysqld]
    skip-name-resolve
    #设置3306端口
    port = 3306 
    #设置mysql的安装目录
    basedir=/usr/local/mariadb
    #设置mysql数据库的数据的存放目录
    datadir=/usr/local/mariadb/data
    #允许最大连接数
    max_connections=200
    #服务端使用的字符集默认为8比特编码的latin1字符集
    character-set-server=utf8
    #创建新表时将使用的默认存储引擎
    default-storage-engine=INNODB 
    lower_case_table_names=1
    max_allowed_packet=16M
    
  6. 安装 MariaDB 并初始化
    [root@cdh01 mariadb]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mariadb --datadir=/usr/local/mariadb/data
    Installing MariaDB/MySQL system tables in '/usr/local/mariadb/data' ...
    OK
    
    To start mysqld at boot time you have to copy
    support-files/mysql.server to the right place for your system
    ...
    

    参考: https://mariadb.com/kb/en/library/mysql_install_db/

  7. 创建mysqld
    [root@cdh01 mariadb]# cp ./support-files/mysql.server /etc/init.d/mysqld
    [root@cdh01 mariadb]# ll /etc/init.d/mysqld
    -rwxr-xr-x 1 root root 12245 Dec 19 13:46 /etc/init.d/mysqld   # 是否可执行
    
  8. 设置为开机启动
    [root@cdh01 mariadb]# chkconfig --list mysqld
    
    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]'.
    
    service mysqld supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add mysqld')
    # 添加到系统服务 chkconfig
    [root@cdh01 mariadb]# chkconfig --add mysqld
    [root@cdh01 mariadb]# chkconfig --list mysqld
    
    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]'.
    
    mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
    [root@cdh01 mariadb]# service mysqld restart
    Restarting mysqld (via systemctl):                         [  OK  ]
    
  9. 配置环境变量
    [root@cdh01 mariadb]# vim /etc/profile
    
    #set mariadb environment
    export MARIADB_HOME=/usr/local/mariadb
    export PATH=$PATH:${MARIADB_HOME}/bin
    
    [root@cdh01 mariadb]# source /etc/profile  
    
  10. 配置 MariaDB
    # -bash: ./bin/mariadb-secure-installation: No such file or directory
    # 报上述问题,请使用 ./bin/mysql_secure_installation
    [root@cdh01 mariadb]# ./bin/mariadb-secure-installation --basedir=/usr/local/mariadb
    print: /usr/local/mariadb/bin/my_print_defaults
    
    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
    haven't set the root password yet, you should just press enter here.
    
    Enter current password for root (enter for none): 
    OK, successfully used password, moving on...
    
    Setting the root password or using the unix_socket ensures that nobody
    can log into the MariaDB root user without the proper authorisation.
    
    You already have your root account protected, so you can safely answer 'n'.
    
    Switch to unix_socket authentication [Y/n] Y
    Enabled successfully!
    Reloading privilege tables..
     ... Success!
    
    
    You already have your root account protected, so you can safely answer 'n'.
    
    Change the 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] n
     ... skipping.
    
    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!
    
  11. 授权远程连接
    [root@DSJ-TVM002 mariadb]# mysql -u root -p
    Enter password: 
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 10
    Server version: 10.4.11-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)]> grant all privileges on *.* to root@"%" identified by "123456" with grant option;
    Query OK, 0 rows affected (0.003 sec)
    
    MariaDB [(none)]> flush privileges;
    Query OK, 0 rows affected (0.000 sec)
    

    如未授权访问,使用 Navicat 连接时可能遇到如下提示

### Linux 离线安装 MariaDB MariaDB 是一个开源的关系型数据库管理系统,广泛应用于各种生产环境中。要在Linux系统上离线安装MariaDB,你需要提前下载好所有必要的软件包,并将其传输到目标机器进行安装。 #### 准备阶段: 1. **确定操作系统版本** 需要知道你要部署的操作系统的具体版本信息(例如CentOS 7、Ubuntu 20.04等),因为不同发行版所需的依赖项可能会有所不同。 2. **获取正确的MariaDB仓库文件或rpm/deb包** - 如果有网络连接良好的环境,则可以在该环境下通过官方源直接抓取所需的所有.deb(对于基于Debian/Ubuntu) 或 .rpm (针对RedHat/CentOS) 包及其依赖关系链。 - 访问[MariaDB官网](https://mariadb.org/download/)选择适合自己平台的稳定版本并手动下载对应的二进制分发包及依赖库至本地磁盘。 3. **将已下载的数据移送到无网的目标服务器** 4. **配置本地yum/apt源**(如果是以repo形式获得的话) 5. **确认selinux状态以及防火墙设置**, 因为这二者都可能导致无法正常启动服务 #### 安装过程: 假设你已经把所有的deb/rpm文件拷贝到了`/opt/mariadb_offline_installation`目录下: ##### 对于RHEL/CentOS/Rocky Linux: ```bash # 进入存放了rpm包的路径 cd /opt/mariadb_offline_installation/ # 使用 rpm 命令批量安装 mariadb 及其相关组件 sudo yum localinstall *.rpm --nogpgcheck # 启动 mariadb 并设为开机自启 sudo systemctl start mariadb.service sudo systemctl enable mariadb.service ``` ##### 对于Debian/Ubuntu: ```bash # 更新APT缓存并不需要在线检查,只是为了让后续命令能够找到刚添加的 deb 文件. dpkg-scanpackages ./ | gzip -9c > Packages.gz echo "deb [trusted=yes] file:/opt/mariadb_offline_installation ./">> /etc/apt/sources.list.d/offline-mariadb.list apt-get update && apt-get install mariadb-server service mysql restart # Ubuntu/Debian 上通常叫这个名而非 mariadb-service ``` 最后一步初始化安全设定如更改root密码之类的可以参考官方文档完成。 #### 注意事项: - 确保在目标计算机上的架构与下载的包匹配(x86_64 vs i386) - 检查是否有其他正在运行的服务占用默认端口(比如MySQL) - 根据实际情况调整SELinux策略,默认可能是阻止Mysql访问某些位置 ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值