MySQL8.1源码安装与部署
官方文档
https://downloads.mysql.com/archives/community/https://dev.mysql.com/doc/refman/8.1/en/binary-installation.html官方文档源码安装步骤
# Preconfiguration setup
$> groupadd mysql
$> useradd -r -g mysql -s /bin/false mysql
# Beginning of source-build specific instructions
$> tar zxvf mysql-VERSION.tar.gz
$> cd mysql-VERSION
$> mkdir bld
$> cd bld
$> cmake ..
$> make
$> make install
# End of source-build specific instructions
# Postinstallation setup
$> cd /usr/local/mysql
$> mkdir mysql-files
$> chown mysql:mysql mysql-files
$> chmod 750 mysql-files
$> bin/mysqld --initialize --user=mysql
$> bin/mysql_ssl_rsa_setup
$> bin/mysqld_safe --user=mysql &
# Next command is optional
$> cp support-files/mysql.server /etc/init.d/mysql.server
下载源码
https://dev.mysql.com/get/Downloads/MySQL-8.1/mysql-8.1.0.tar.gz[root@node01 ~]# wget https://dev.mysql.com/get/Downloads/MySQL-8.1/mysql-8.1.0.tar.gz
源码安装先决条件
1、gcc和cmake版本要求
Linux:GCC 7.1 或 Clang 5
CMake:3.752、依赖包
cmake、make、automake、autoconf、gcc、gcc-c++、ncurses-devel、openssl-devel、libmcrypt3、安装依赖包
[root@node01 ~]# yum install cmake make gcc gcc-c++ autoconf automake openssl openssl-devel ncurses-devel libmcrypt* -y4、卸载mariadb
[root@node01 ~]# yum remove mariadb -y5、删除配置文件
[root@node01 ~]# rm -rf /etc/my.cnf
升级gcc
1、操作系统版本
[root@node01 ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)2、查看gcc版本
[root@node01 ~]# gcc -v
gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 3、安装scl
[root@node01 ~]# yum install centos-release-scl -y
[root@node01 ~]# yum list |grep gcc4、安装gcc8
[root@node01 ~]# yum install -y devtoolset-8-gcc*5、临时切换gcc
[root@node01 ~]# source /opt/rh/devtoolset-8/enable 6、查看gcc版本
[root@node01 ~]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-8/root/usr/libexec/gcc/x86_64-redhat-linux/8/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-8/root/usr --mandir=/opt/rh/devtoolset-8/root/usr/share/man --infodir=/opt/rh/devtoolset-8/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-8.3.1-20190311/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 8.3.1 20190311 (Red Hat 8.3.1-3) (GCC)
升级cmake
1、cmake编译
[root@node01 bld]# cmake ..
-- Running cmake version 2.8.12.2
CMake Warning at CMakeLists.txt:82 (MESSAGE):Please use cmake3 rather than cmake on this platform-- Please install cmake3 (yum install cmake3)
CMake Error at CMakeLists.txt:112 (CMAKE_MINIMUM_REQUIRED):CMake 3.5.1 or higher is required. You are running version 2.8.12.22、升级cmake,先移除原cmake
[root@node01 ~]# yum remove cmake -y3、下载cmake源码
[root@node01 ~]# wget https://github.com/Kitware/CMake/releases/download/v3.14.5/cmake-3.14.5.tar.gz4、解压缩
[root@node01 ~]# tar -zxf cmake-3.14.5.tar.gz
5、编译安装
[root@node01 ~]# cd cmake-3.14.5
[root@node01 cmake-3.14.5]# ./bootstrap
[root@node01 cmake-3.14.5]# gmake
[root@node01 cmake-3.14.5]# gmake install5、查看升级后版本
[root@node01 cmake-3.14.5]# /usr/local/bin/cmake --version
cmake version 3.14.5CMake suite maintained and supported by Kitware (kitware.com/cmake).6、建立软链接
[root@node01 cmake-3.14.5]# ln -s /usr/local/bin/cmake /usr/bin/[root@node01 ~]# cmake -version
cmake version 3.14.5CMake suite maintained and supported by Kitware (kitware.com/cmake).至此,cmake升级完毕
创建用户组和用户
1、查看用户mysql是否存在
[root@node01 ~]# id mysql
id: mysql: no such user2、创建用户组
[root@node01 ~]# groupadd -g 54321 mysql3、创建用户
[root@node01 ~]# useradd -r -g mysql -s /bin/false -u 54321 mysql4、查看用户[root@node01 ~]# id mysql
uid=54321(mysql) gid=54321(mysql) groups=54321(mysql)
解压缩创建软链接
[root@node01 ~]# tar -zxf mysql-8.1.0.tar.gz -C /usr/local/cluster/
[root@node01 ~]# ln -s /usr/local/cluster/mysql-8.1.0/ /usr/local/cluster/mysql
[root@node01 ~]# ll /usr/local/cluster/mysql
lrwxrwxrwx 1 root root 31 Jul 26 11:51 /usr/local/cluster/mysql -> /usr/local/cluster/mysql-8.1.0/
cmake编译安装
1、camke编译报错
[root@node01 ~]# cd /usr/local/cluster/mysql
[root@node01 mysql]# mkdir bld
[root@node01 bld]# cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql-- Running cmake version 3.14.5
-- Could NOT find Git (missing: GIT_EXECUTABLE)
-- This is .el7. as found from 'rpm -qf /'
-- Looking for a devtoolset compiler
CMake Warning at CMakeLists.txt:395 (MESSAGE):Could not find devtoolset compiler/linker in /opt/rh/devtoolset-11CMake Warning at CMakeLists.txt:397 (MESSAGE):You need to install the required packages:yum install devtoolset-11-gcc devtoolset-11-gcc-c++ devtoolset-11-binutilsCMake Error at CMakeLists.txt:399 (MESSAGE):Or you can set CMAKE_C_COMPILER and CMAKE_CXX_COMPILER explicitly.-- Configuring incomplete, errors occurred!2、根据报错提示安装devtoolset依赖包
[root@node01 bld]# yum install devtoolset-11-gcc devtoolset-11-gcc-c++ devtoolset-11-binutils3、再次执行cmake报错
[root@node01 bld]# cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysqlCMake Error at cmake/boost.cmake:108 (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.You can also download boost manually, fromhttps://boostorg.jfrog.io/artifactory/main/release/1.77.0/source/boost_1_77_0.tar.bz2If you are inside a firewall, you may need to use an https proxy:export https_proxy=http://example.com:80Call Stack (most recent call first):cmake/boost.cmake:277 (COULD_NOT_FIND_BOOST)CMakeLists.txt:1555 (INCLUDE)-- Configuring incomplete, errors occurred!
See also "/usr/local/cluster/mysql/bld/CMakeFiles/CMakeOutput.log".
See also "/usr/local/cluster/mysql/bld/CMakeFiles/CMakeError.log".4、根据报错提示添加参数-DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory>
[root@node01 bld]# cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/cluster/boost5、如果执行过程中还是报错,可以选择第二种手工下载boost
[root@node01 ~]# wget https://boostorg.jfrog.io/artifactory/main/release/1.77.0/source/boost_1_77_0.tar.bz2 解压缩
[root@node01 ~]# yum -y install bzip2
[root@node01 ~]# tar -xjf boost_1_77_0.tar.bz2 -C /usr/local/这种手工下载boost需要将参数DDOWNLOAD_BOOST设为0,DDOWNLOAD_BOOST=1,表示系统会自动下载并解压
[root@node02 bld]# cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DDOWNLOAD_BOOST=0 -DWITH_BOOST=/usr/local/cluster/boost_1_77_0make&&make install
[root@node01 bld]# make
[root@node01 bld]# make install在执行make过程中如果报错,解决方便是升级内存,我的虚拟机环境内存2G,执行make报错,升级到4G后再次make成功
g++: fatal error: Killed signal terminated program cc1plus
创建数据目录
1、创建数据目录
[root@node01 ~]# mkdir -p /data/mysql
3、更改目录属组
[root@node01 ~]# chown -R mysql:mysql /data/mysql/
参数配置
[root@node01 ~]# vim /etc/my.cnf[client]
port=3306[mysql]
prompt="\\u@\\h [\\d]>"[mysqld]
port=3306
basedir=/usr/local/mysql
datadir=/data/mysql
pid-file=/data/mysql/mysql.pid
log-error=/data/mysql/mysql.err
初始化数据库
1、默认使用/etc/my.cnf参数文件
[root@node01 ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql2、看到日志输出显示临时密码说明初始化成功
[root@node01 ~]# more /data/mysql/mysql.err | grep temporary
2023-07-26T10:11:27.989113Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: VfLweNjef9!w
启动数据库
1、启动数据库
[root@node01 ~]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &
[1] 8797
[root@node01 ~]# 2023-07-26T10:16:06.974009Z mysqld_safe Logging to '/data/mysql/mysql.err'.
2023-07-26T10:16:07.003797Z mysqld_safe Starting mysqld daemon with databases from /data/mysql2、查看进程/usr/local/mysql/bin/mysqld_safe是守护进程
[root@node01 ~]# ps -ef |grep mysql |grep -v grep
root 8797 87308 0 18:16 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf
mysql 9031 8797 3 18:16 pts/1 00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/mysql.err --pid-file=/data/mysql/mysql.pid --port=3306
配置环境变量
1、修改配置文件
[root@node01 ~]# vim .bash_profile
# .bash_profile# Get the aliases and functions
if [ -f ~/.bashrc ]; then. ~/.bashrc
fi# User specific environment and startup programsPATH=$PATH:$HOME/binexport PATHexport JAVA_HOME=/usr/local/cluster/jdk
export PATH=$PATH:$JAVA_HOME/bin
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin2、使环境变量生效
[root@node01 ~]# source .bash_profile
登录测试
[root@node01 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.1.0Copyright (c) 2000, 2023, 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.root@localhost [(none)]>
修改密码
首次登录数据库需要修改root密码root@localhost [(none)]> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.root@localhost [(none)]> select user,host from mysql.user;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
root@localhost [(none)]> use mysql;
No connection. Trying to reconnect...
Connection id: 12
Current database: *** NONE ***ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.root@localhost [(none)]> alter user 'root'@'localhost' identified by 'Oracle@123';
Query OK, 0 rows affected (1.29 sec)root@localhost [(none)]> flush privileges;
Query OK, 0 rows affected (0.14 sec)root@localhost [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
root@localhost [(mysql)]> select user,host from user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
使用配置文件启动
使用配置文件/usr/local/mysql/support-files/mysql.server启动[root@node01 ~]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL................ SUCCESS![root@node01 ~]# /usr/local/mysql/support-files/mysql.server statusSUCCESS! MySQL running (102015)[root@node01 ~]# /usr/local/mysql/support-files/mysql.server stop
Shutting down MySQL... SUCCESS!
配置systemctl服务
通过查看配置文件/usr/local/mysql/support-files/mysql.server,可以看到变量basedir和datadir和配置文件中的变量一致,不需要修改。配置文件默认是/etc/my.cnf,也不需要修改[root@node01 ~]# vim /usr/local/mysql/support-files/mysql.server
mysqld_pid_file_path=
if test -z "$basedir"
thenbasedir=/usr/local/mysqlbindir=/usr/local/mysql/binif test -z "$datadir"thendatadir=/data/mysqlfisbindir=/usr/local/mysql/binlibexecdir=/usr/local/mysql/bin
elsebindir="$basedir/bin"if test -z "$datadir"thendatadir="$basedir/data"fisbindir="$basedir/sbin"libexecdir="$basedir/libexec"
fi1、创建systemctl服务
[root@node01 ~]# vim /usr/lib/systemd/system/mysqld.service[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target[Install]
WantedBy=multi-user.target[Service]
User=mysql
Group=mysql
Type=forking
PIDFile=/data/mysql/mysql.pid
TimeoutSec=0# Execute pre and post scripts as root
PermissionsStartOnly=true# Start main service
ExecStart=/usr/local/mysql/support-files/mysql.server start# Use this to switch malloc implementation
#EnvironmentFile=-/etc/sysconfig/mysql# Sets open_files_limit
@LimitNOFILE = 5000Restart=on-failure
RestartPreventExitStatus=1PrivateTmp=false2、加载服务
[root@node01 ~]# systemctl daemon-reload3、设置开机自启
[root@node01 ~]# systemctl enable mysqld.service4、查看状态
[root@node01 ~]# systemctl status mysqld.service
● mysqld.service - MySQL ServerLoaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)Active: inactive (dead)Docs: man:mysqld(8)http://dev.mysql.com/doc/refman/en/using-systemd.htmlJul 27 19:20:44 node01 systemd[1]: [/usr/lib/systemd/system/mysqld.service:28] Unknown lvalue '@LimitNOFILE' in section 'Service'5、启动mysql
[root@node01 ~]# systemctl start mysqld.service
[root@node01 ~]# systemctl status mysqld.service
● mysqld.service - MySQL ServerLoaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)Active: active (running) since Thu 2023-07-27 19:24:00 CST; 2s agoDocs: man:mysqld(8)http://dev.mysql.com/doc/refman/en/using-systemd.htmlProcess: 113461 ExecStart=/usr/local/mysql/support-files/mysql.server start (code=exited, status=0/SUCCESS)Main PID: 113725 (mysqld)Tasks: 39Memory: 291.3MCGroup: /system.slice/mysqld.service├─113474 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/mysql.pid└─113725 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/li...Jul 27 19:23:59 node01 systemd[1]: Starting MySQL Server...
Jul 27 19:24:00 node01 systemd[1]: mysqld.service: Supervising process 113725 which is not our child. We'll most likely no... exits.
Jul 27 19:24:00 node01 systemd[1]: Started MySQL Server.
Hint: Some lines were ellipsized, use -l to show in full.6、查看进程
[root@node01 ~]# ps -ef |grep mysql |grep -v grep
mysql 113474 1 0 19:23 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/mysql.pid
mysql 113725 113474 4 19:23 ? 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/data/mysql/mysql.err --pid-file=/data/mysql/mysql.pid --port=3306
总结
1、之前一直使用rpm包和二进制包方式进行安装部署mysql,目前刚刚发布mysql8.1版本,尝试使用源码安装8.1
2、源码安装的先决条件比较多,如依赖包、gcc版本和cmake版本都需要升级,在执行cmake编译安装的过程中出现很多报错,具体报错都已经解决