二进制安装数据库MariaDB

举报
灰烬 发表于 2022/02/16 15:32:39 2022/02/16
【摘要】 数据库安装的几种方法二

=================================

/*

* @系统:CentOS7.9

* @描述:数据库安装的几种方法二

*/

=================================

一、获取安装包

进制包

#进入到安装包存放位置
# cd /usr/local/src/
#上传文件
# rz #选择上传文件
# ls mariadb-10.5.4-linux-x86_64.tar.gz

二、安装

1. 检查环境

# ss -ntl #检查端口
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        128             [::]:22                       [::]:* 
LISTEN        0        100             [::1]:25                     [::]:*
# rpm -q mysql
package mysql is not installed
# rpm -q mariadb
package mariadb is not installed
# getent passwd mysql #检查mysql用户是否创建

2. 创建用户和组

# useradd -r -d /data/mariadb -s /sbin/nologin mysql
# getent passwd mysql
mysql:x:998:996::/data/mariadb:/sbin/nologin
# id mysql
uid=998(mysql) gid=996(mysql) groups=996(mysql)

3. 解压到指定路径并创建软连接

# tar xvf mariadb-10.5.4-linux-x86_64.tar.gz -C /usr/local/
# cd /usr/local/
# ls
bin  etc  games  include  lib  lib64  libexec  mariadb-10.5.4-linux-x86_64  sbin  share  src
# ln -s mariadb-10.5.4-linux-x86_64/ mysql

4. 修改所所有者

# ls -l mysql/
total 176
drwxrwxr-x  2 1000 1000  4096 Jun 22 11:50 bin
-rw-r--r--  1 1000 1000 17987 Jun 23 23:10 COPYING
-rw-r--r--  1 1000 1000  2354 Jun 23 23:10 CREDITS
-rw-r--r--  1 1000 1000  8245 Jun 23 23:10 EXCEPTIONS-CLIENT
drwxrwxr-x  3 1000 1000    19 Jun 23 23:39 include
-rw-r--r--  1 1000 1000  8782 Jun 23 23:10 INSTALL-BINARY
drwxrwxr-x  5 1000 1000   275 Jun 22 11:50 lib
drwxrwxr-x  4 1000 1000    30 Jun 23 23:39 man
drwxrwxr-x  9 1000 1000  4096 Jun 23 23:39 mysql-test
-rw-r--r--  1 1000 1000  2973 Jun 23 23:10 README.md
-rw-r--r--  1 1000 1000 19520 Jun 23 23:10 README-wsrep
drwxrwxr-x  2 1000 1000    56 Jun 23 23:39 scripts
drwxrwxr-x 31 1000 1000  4096 Jun 23 23:39 share
drwxrwxr-x  4 1000 1000  4096 Jun 23 23:39 sql-bench
drwxrwxr-x  3 1000 1000   165 Jun 23 23:39 support-files
-rw-r--r--  1 1000 1000 86263 Jun 23 23:10 THIRDPARTY
# chown -R root:root mysql/
# ls -l mysql/
total 176
drwxrwxr-x  2 root root  4096 Jun 22 11:50 bin
-rw-r--r--  1 root root 17987 Jun 23 23:10 COPYING
-rw-r--r--  1 root root  2354 Jun 23 23:10 CREDITS
-rw-r--r--  1 root root  8245 Jun 23 23:10 EXCEPTIONS-CLIENT
drwxrwxr-x  3 root root    19 Jun 23 23:39 include
-rw-r--r--  1 root root  8782 Jun 23 23:10 INSTALL-BINARY
drwxrwxr-x  5 root root   275 Jun 22 11:50 lib
drwxrwxr-x  4 root root    30 Jun 23 23:39 man
drwxrwxr-x  9 root root  4096 Jun 23 23:39 mysql-test
-rw-r--r--  1 root root  2973 Jun 23 23:10 README.md
-rw-r--r--  1 root root 19520 Jun 23 23:10 README-wsrep
drwxrwxr-x  2 root root    56 Jun 23 23:39 scripts
drwxrwxr-x 31 root root  4096 Jun 23 23:39 share
drwxrwxr-x  4 root root  4096 Jun 23 23:39 sql-bench
drwxrwxr-x  3 root root   165 Jun 23 23:39 support-files
-rw-r--r--  1 root root 86263 Jun 23 23:10 THIRDPARTY

5. 添加环境变量

# echo PATH=/usr/local/mysql/bin:$PATH > /etc/profile.d/mysql.sh 
# . /etc/profile.d/mysql.sh
# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

三、配置

1. 修改数据库存放位置(可以新增一个磁盘存放)

    # mkdir /data/mariadb

    2. 修改属组

    # chown -R mysql.mysql /data/mariadb

    3. 修改权限(为了安全需要修改权限为770)

    # chmod 770 /data/mariadb

    四、初始化

    1. 进入到二进制程序目录

    # cd /usr/local/mysql/

    2. 执行初始化脚本文件(切记不要进入到进脚本存放位置的文件夹)

    # scripts/mariadb-install-db --datadir=/data/mariadb --user=mysql
    Installing MariaDB/MySQL system tables in '/data/mariadb' 
    ...OK #出现这个就证明没问题了(有些可能只有mysql_install_db,一样一样的)

    3. 修改配置文件(因为没有找到提供的配置文件,所以直接使用原有的)

    # cd 
    # vim /etc/my.cnf
    ================================
    [mysqld]
    datadir=/data/mariadb
    socket=/usr/local/mysql/mysql.sock
    [client]
    port=3306
    socket=/usr/local/mysql/mysql.sock

    4. 设置开机自启动

    # cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mariadb #拷贝启动脚本
    # 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]'.
    
    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
    # chkconfig --add mariadb #添加启动服务
    # 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

    5. 设置权限

    # setfacl -R -m u:mysql:rwx /usr/local/mysql/ #授予mysql用户读写执行权限
    #注:也可以将socket文件放到有执行权限的文件夹中

    6. 添加服务

    # systemctl start mariadb
    # ss -nutl
    Netid  State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
    tcp    LISTEN     0      128        *:22                     *:*                  
    tcp    LISTEN     0      100    127.0.0.1:25                     *:*                  
    tcp    LISTEN     0      128     [::]:22                  [::]:*                  
    tcp    LISTEN     0      100    [::1]:25                  [::]:*                  
    tcp    LISTEN     0      80      [::]:3306                [::]:*

    7. 运行安全脚本

    # mysql_secure_installation
    #执行后出现报错,需要修改socket路径
    # /usr/local/mysql/bin/mysqld --print-defaults #查看编译路径
    /usr/local/mysql/bin/mysqld would have been started with the following arguments:
    --datadir=/data/mariadb --socket=/tmp/mysql.sock --symbolic-links=0
    #解决方案
    # vim /etc/my.cnf
    ================
    [mysqld]
    datadir=/data/mariadb
    socket=/tmp/mysql.sock
    # socket=/usr/local/mysql/mysql.sock
    
    [client]
    port=3306
    socket=/tmp/mysql.sock
    # socket=/usr/local/mysql/mysql.sock
    [root@51try ~]# systemctl restart mariadb #重启服务
    [root@51try ~]# mysql_secure_installation
    =======================================
    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: root1234
    Re-enter new password: root1234
    Password updated successfully!
    Reloading privilege tables..
     ... Success!
     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 #是否禁用root用户远程登录
     ... 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!

    8. 设置开机启动

    # /sbin/chkconfig mariadb on

    五、连接数据库

    # mysql -uroot -p #切记不要在-p后面加密码,这样等于明文
    Enter password:
    MariaDB [(none)]> status --查看一下信息
    --------------
    mysql  Ver 15.1 Distrib 10.5.4-MariaDB, for Linux (x86_64) using readline 5.1
    MariaDB [(none)]> \q --退出数据库
    Bye

    未完待续......

    --本篇完--

    【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
    • 点赞
    • 收藏
    • 关注作者

    评论(0

    0/1000
    抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。