Linux 下离线安装 MariaDB

巧了我就是萌 提交于 2019-12-19 22:14:17
  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.tar.gz 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
    [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)
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!