10.数据库安装

一、源码编译安装MySQL-5.7

  1. 安装相关依赖包
    后面采用cmake方式编译,需要把相应的工具包先装上,不然会报错。这里没有做测试是否都需要,只是参考老师们给出的工具包,直接安装上。
    yum -y install gcc gcc-c++ cmake bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel perl-Data-Dumper

  2. 准备用户和数据目录,以及数据库配置文件

[root@CentOS7 ~]# groupadd mysql
[root@CentOS7 ~]# useradd -r -g mysql -s /bin/nologin mysql
[root@CentOS7 ~]# mkdir /data/mysql
[root@CentOS7 ~]# chown -R mysql.mysql /data/mysql
[root@CentOS7 ~]# mkdir -p /apps/mysql
  1. 数据库源文件(提前下载好了),解压及修改权限
[root@CentOS7 ~]# ll /root
total 51732
-rw-------. 1 root root     1587 Dec 22 13:10 anaconda-ks.cfg
-rw-r--r--. 1 root root 52968383 Sep  7  2021 mysql-boost-5.7.36.tar.gz
[root@CentOS7 ~]# tar xzf mysql-boost-5.7.36.tar.gz -C /usr/local/src/
[root@CentOS7 ~]# cd /usr/local/src/mysql-5.7.36/
[root@CentOS7 mysql-5.7.36]# chown -R root.root /usr/local/src/mysql-5.7.36/
[root@CentOS7 mysql-5.7.36]# ll
total 508
drwxr-xr-x.  3 root root     26 Sep  7  2021 boost
drwxr-xr-x.  2 root root   4096 Sep  7  2021 BUILD
drwxr-xr-x.  6 root root   4096 Sep  7  2021 client
drwxr-xr-x.  4 root root   4096 Sep  7  2021 cmake
#...中间省略部分输出...
drwxr-xr-x.  3 root root   4096 Sep  7  2021 vio
drwxr-xr-x.  2 root root     32 Sep  7  2021 win
drwxr-xr-x. 11 root root   4096 Sep  7  2021 zlib
  1. 编译安装
    利用cmake编译可以在另一个指定的目录中而非源码目录中进行,这可以保证源码目录不受任何一次编译的影响,因此在同一个源码树上可以进行多次不同的编译。

     提示:如果出错,执行rm -f CMakeCache.txt
    
[root@CentOS7 mysql-5.7.36]# cmake . \
> -DCMAKE_INSTALL_PREFIX=/apps/mysql \
> -DMYSQL_DATADIR=/data/mysql/ \
> -DSYSCONFDIR=/etc/ \
> -DMYSQL_USER=mysql \
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
> -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
> -DWITH_PARTITION_STORAGE_ENGINE=1 \
> -DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
> -DWITH_DEBUG=0 \
> -DWITH_READLINE=1 \
> -DWITH_SSL=system \
> -DWITH_ZLIB=system \
> -DWITH_LIBWRAP=0 \
> -DENABLED_LOCAL_INFILE=1 \
> -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
> -DDEFAULT_CHARSET=utf8 \
> -DDEFAULT_COLLATION=utf8_general_ci
-bash: cmake: command not found     `#如果没有安装相应工具,就会报错`
[root@CentOS7 mysql-5.7.36]#
[root@CentOS7 mysql-5.7.36]# cmake . \
> -DCMAKE_INSTALL_PREFIX=/apps/mysql \
> -DMYSQL_DATADIR=/data/mysql/ \
> -DSYSCONFDIR=/etc/ \
> -DMYSQL_USER=mysql \
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
> -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
> -DWITH_PARTITION_STORAGE_ENGINE=1 \
> -DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
> -DWITH_DEBUG=0 \
> -DWITH_READLINE=1 \
> -DWITH_SSL=system \
> -DWITH_ZLIB=system \
> -DWITH_LIBWRAP=0 \
> -DENABLED_LOCAL_INFILE=1 \
> -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
> -DDEFAULT_CHARSET=utf8 \
> -DDEFAULT_COLLATION=utf8_general_ci
-- Running cmake version 2.8.12.2
#省略部分输出
-- Performing Test HAVE_CXX_FP_CONTRACT_FLAG - Success
-- MySQL 5.7.36
-- Packaging as: mysql-5.7.36-Linux-x86_64
-- Found /usr/include/boost/version.hpp
-- BOOST_VERSION_NUMBER is #define BOOST_VERSION 105300
CMake Warning at cmake/boost.cmake:273 (MESSAGE):
  Boost minor version found is 53 we need 59
Call Stack (most recent call first):
  CMakeLists.txt:536 (INCLUDE)


-- BOOST_INCLUDE_DIR /usr/include
-- LOCAL_BOOST_DIR
-- LOCAL_BOOST_ZIP
-- Could not find (the correct version of) boost.
-- MySQL currently requires boost_1_59_0
`#提示没有找到boost,安装5.7版本需要的`

CMake Error at cmake/boost.cmake:88 (MESSAGE):
  You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory>

  This CMake script will look for boost in <directory>.  If it is not there,
  it will download and unpack it (in that directory) for you.

  If you are inside a firewall, you may need to use an http proxy:

  export http_proxy=http://example.com:80

Call Stack (most recent call first):
  cmake/boost.cmake:276 (COULD_NOT_FIND_BOOST)
  CMakeLists.txt:536 (INCLUDE)


-- Configuring incomplete, errors occurred!
See also "/usr/local/src/mysql-5.7.36/CMakeFiles/CMakeOutput.log".
See also "/usr/local/src/mysql-5.7.36/CMakeFiles/CMakeError.log".
[root@CentOS7 mysql-5.7.36]#
[root@CentOS7 mysql-5.7.36]# cmake . \
> -DCMAKE_INSTALL_PREFIX=/apps/mysql \
> -DMYSQL_DATADIR=/data/mysql/ \
> -DSYSCONFDIR=/etc/ \
> -DMYSQL_USER=mysql \
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
> -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
> -DWITH_PARTITION_STORAGE_ENGINE=1 \
> -DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
> -DWITH_DEBUG=0 \
> -DWITH_READLINE=1 \
> -DWITH_SSL=system \
> -DWITH_ZLIB=system \
> -DWITH_LIBWRAP=0 \
> -DENABLED_LOCAL_INFILE=1 \
> -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
> -DDEFAULT_CHARSET=utf8 \
> -DDEFAULT_COLLATION=utf8_general_ci \
> -DWITH_BOOST=./boost/boost_1_59_0            `#这里使用了相对路径,所以要进入到mysql-5.7.36目录里才行。其实应该写绝对路径为好。`
-- Running cmake version 2.8.12.2
-- Could NOT find Git (missing:  GIT_EXECUTABLE)
-- Configuring with MAX_INDEXES = 64U
-- CMAKE_GENERATOR: Unix Makefiles

-- Configuring done
-- Generating done
CMake Warning:
  Manually-specified variables were not used by the project:

    MYSQL_USER
    WITHOUT_MROONGA_STORAGE_ENGINE
    WITH_READLINE


-- Build files have been written to: /usr/local/src/mysql-5.7.36
[root@CentOS7 mysql-5.7.36]#
[root@CentOS7 mysql-5.7.36]# make && make install
#省略输出
  1. 准备环境变量
[root@CentOS7 mysql-5.7.36]# echo "PATH=/apps/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
[root@CentOS7 mysql-5.7.36]# cat /etc/profile.d/mysql.sh
PATH=/apps/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@CentOS7 mysql-5.7.36]# . /etc/profile.d/mysql.sh
  1. 生成数据库文件
[root@CentOS7 mysql-5.7.36]# cd /apps/mysql/
[root@CentOS7 mysql]# mysqld --initialize --user=mysql --datadir=/data/mysql/
2022-03-13T05:21:06.338246Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-03-13T05:21:07.182211Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-03-13T05:21:07.259238Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-03-13T05:21:07.271271Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 62c386e3-a28d-11ec-a5ec-000c297e25b5.
2022-03-13T05:21:07.282286Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-03-13T05:21:07.548020Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-03-13T05:21:07.548051Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-03-13T05:21:07.549605Z 0 [Warning] CA certificate ca.pem is self signed.
2022-03-13T05:21:07.773739Z 1 [Note] A temporary password is generated for root@localhost: pVwl>XUxq7&u
[root@CentOS7 mysql]#
  1. 准备配置文件,以及启动脚本和服务
[root@CentOS7 bin]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
[root@CentOS7 bin]# cp /apps/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@CentOS7 bin]# chkconfig --add mysqld
[root@CentOS7 bin]# service mysqld start
Starting MySQL.Logging to '/data/mysql/mysql.log'.
 SUCCESS!
[root@CentOS7 bin]#
[root@CentOS7 bin]# 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      80                                      [::]:3306                                                [::]:*
LISTEN     0      128                                     [::]:22                                                  [::]:*
LISTEN     0      100                                    [::1]:25                                                  [::]:*
[root@CentOS7 bin]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36

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> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> quit
Bye
[root@CentOS7 bin]#

二、二进制方式安装数据库

2.1 二进制方式安装mariadb-10.4

1.准备用户和组,数据目录

[root@CentOS7 ~]# groupadd mysql
[root@CentOS7 ~]# grep mysql /etc/group
mysql:x:1001:
[root@CentOS7 ~]# useradd -r -g mysql -s /bin/nologin mysql
[root@CentOS7 ~]# grep mysql /etc/passwd
mysql:x:997:1001::/home
[root@CentOS7 ~]# mkdir /data/mysql
[root@CentOS7 ~]# chown -R mysql.mysql /data/mysql
[root@CentOS7 ~]# ll -d /data/mysql
drwxr-xr-x. 5 mysql mysql 4096 Mar 15 09:19 /data/mysql
  1. 准备程序文件
[root@CentOS7 ~]# ll
total 1126100
-rw-------. 1 root root       1587 Dec 22 13:10 anaconda-ks.cfg
-rw-r--r--. 1 root root 1153120329 Mar 13 13:41 mariadb-10.4.24-linux-x86_64.tar.gz
[root@CentOS7 ~]# tar xzf mariadb-10.4.24-linux-x86_64.tar.gz -C /usr/local/
[root@CentOS7 ~]# cd /usr/local/
[root@CentOS7 local]# ll
total 0
drwxr-xr-x.  2 root    root      6 Apr 11  2018 bin
drwxr-xr-x.  2 root    root      6 Apr 11  2018 etc
drwxr-xr-x.  2 root    root      6 Apr 11  2018 games
drwxr-xr-x.  2 root    root      6 Apr 11  2018 include
drwxr-xr-x.  2 root    root      6 Apr 11  2018 lib
drwxr-xr-x.  2 root    root      6 Apr 11  2018 lib64
drwxr-xr-x.  2 root    root      6 Apr 11  2018 libexec
drwxrwxr-x. 12 jiangde jiangde 257 Feb 12 07:20 mariadb-10.4.24-linux-x86_64
drwxr-xr-x.  2 root    root      6 Apr 11  2018 sbin
drwxr-xr-x.  5 root    root     49 Dec 22 13:03 share
drwxr-xr-x.  2 root    root      6 Apr 11  2018 src
[root@CentOS7 local]# chown -R root.root mariadb-10.4.24-linux-x86_64/
[root@CentOS7 local]# ln -s mariadb-10.4.24-linux-x86_64/ mysql
[root@CentOS7 local]# ll
total 0
drwxr-xr-x.  2 root root   6 Apr 11  2018 bin
drwxr-xr-x.  2 root root   6 Apr 11  2018 etc
drwxr-xr-x.  2 root root   6 Apr 11  2018 games
drwxr-xr-x.  2 root root   6 Apr 11  2018 include
drwxr-xr-x.  2 root root   6 Apr 11  2018 lib
drwxr-xr-x.  2 root root   6 Apr 11  2018 lib64
drwxr-xr-x.  2 root root   6 Apr 11  2018 libexec
drwxrwxr-x. 12 root root 257 Feb 12 07:20 mariadb-10.4.24-linux-x86_64
lrwxrwxrwx.  1 root root  29 Mar 13 13:44 mysql -> mariadb-10.4.24-linux-x86_64/
drwxr-xr-x.  2 root root   6 Apr 11  2018 sbin
drwxr-xr-x.  5 root root  49 Dec 22 13:03 share
drwxr-xr-x.  2 root root   6 Apr 11  2018 src
[root@CentOS7 local]#

3.准备环境变量

[root@CentOS7 local]# cat /etc/profile.d/mysql.sh
PATH=/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@CentOS7 local]# . /etc/profile.d/mysql.sh
  1. 准备配置文件
[root@CentOS7 local]# cp /etc/my.cnf /etc/my.cnf.bak
[root@CentOS7 local]# vi /etc/my.cnf
[root@CentOS7 local]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
[root@CentOS7 local]#
  1. 初始化数据库文件
[root@CentOS7 local]# cd mysql/
[root@CentOS7 mysql]# scripts/mariadb-install-db --datadir=/data/mysql --user=mysql
Installing MariaDB/MySQL system tables in '/data/mysql' ...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system


Two all-privilege accounts were created.
One is root@localhost, it has no password, but you need to
be system 'root' user to connect. Use, for example, sudo mysql
The second is mysql@localhost, it has no password either, but
you need to be the system 'mysql' user to connect.
After connecting you can set the password, if you would need to be
able to connect as any of these users with a password and without sudo

See the MariaDB Knowledgebase at http://mariadb.com/kb

You can start the MariaDB daemon with:
cd '.' ; ./bin/mysqld_safe --datadir='/data/mysql'

You can test the MariaDB daemon with mysql-test-run.pl
cd './mysql-test' ; perl mysql-test-run.pl

Please report any problems at http://mariadb.org/jira

The latest information about MariaDB is available at http://mariadb.org/.

Consider joining MariaDB's strong and vibrant community:
https://mariadb.org/get-involved/

[root@CentOS7 mysql]#
  1. 准备服务脚本和启动
[root@CentOS7 mysql]# cp support-files/mysql.server /etc/init.d/mariadb
[root@CentOS7 mysql]# chkconfig --add mariadb
[root@CentOS7 mysql]# service mariadb start
Starting mysqld (via systemctl):                           [  OK  ]
[root@CentOS7 mysql]# systemctl status mariadb
● mysqld.service - LSB: start and stop MariaDB
   Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
   Active: active (running) since Sun 2022-03-13 13:55:35 CST; 8s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1531 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/mysqld.service
           ├─1552 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/mysql.pid
           └─1647 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin -...

Mar 13 13:55:34 CentOS7.9.example.com systemd[1]: Starting LSB: start and stop MariaDB...
Mar 13 13:55:34 CentOS7.9.example.com mysqld[1531]: Starting MariaDB.220313 13:55:34 mysqld_safe Logging to '/data/mysql/mysql.log'.
Mar 13 13:55:34 CentOS7.9.example.com mysqld[1531]: 220313 13:55:34 mysqld_safe Starting mysqld daemon with databases from /data/mysql
Mar 13 13:55:35 CentOS7.9.example.com mysqld[1531]: SUCCESS!
Mar 13 13:55:35 CentOS7.9.example.com systemd[1]: Started LSB: start and stop MariaDB.
[root@CentOS7 mysql]#
[root@CentOS7 mysql]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.4.24-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)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.001 sec)

MariaDB [(none)]>

2.2 二进制方式安装MySQL-5.7

  1. 安装相关包:yum -y install libaio numactl-libs

  2. 用户和组:groupadd mysql; useradd -r -g mysql -s /bin/false mysql

  3. 准备程序文件

    mkdir -p /data/mysql
    chown -R mysql:mysql /data/mysql
    wget https://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
    tar xzf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local
    cd /usr/local/
    ln -s mysql-5.7.31-linux-glibc2.12-x86_64/ mysql
    chown -R root.root /usr/local/mysql/
    
  4. 准备环境变量

    echo "PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
    . /etc/profile.d/mysql.sh
    
  5. 准备配置文件

    cp /etc/my.cnf{,.bak}
    vim /etc/my.cnf
    [mysqld]
    datadir=/data/mysql
    skip_name_resolve=1
    socket=/data/mysql/mysql.sock
    log-error=/data/mysql/mysql.log
    pid-file=/data/mysql/mysql.pid
    [client]
    socket=/data/mysql/mysql.sock
    
  6. 初始化数据库文件并提取root密码

    mysqld --initialize --user=mysql --datadir=/data/mysql
    
  7. 准备服务脚本和启动

    cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
    chkconfig --add mysqld
    service mysqld start
    

三、总结

  1. 要使用数据库,首先得会安装,不同的版本和不同的安装方式,都会有所差异,以及初始化数据的参数及路径选择(这部分只是按笔记本上抄的,后续需要多加理解运用)。后面也可以使用shell脚本来安装。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值