时间:2021-07-01 10:21:17 帮助过:14人阅读
服务器配置如下:
CentOS 6.8,内存6G,磁盘60G。
内存4G,磁盘60G的情况下,编译不成功,内存不足;在内存6G,磁盘20G的情况下,编译也不成功,磁盘空间不足,所以编译安装MySQL 8.x还是需要一定的内存和磁盘空间的。
二、安装环境准备
1.安装基础依赖环境
yum -y install xz wget gcc-c++ ncurses ncurses-devel cmake make perl openssl openssl-devel gcc* libxml2 libxml2-devel curl-devel libjpeg* libpng* freetype* make gcc-c++ cmake bison-devel bison perl perl-devel perl perl-devel glibc-devel.i686 glibc-devel libaio ntpdate readline-devel zlib.x86_64 zlib-devel.x86_64 libcurl-* net-tool* sysstat lrzsz dos2unix telnet.x86_64 nethogs iftop iotop unzip ftp.x86_64 xfs* expect vim psmisc openssh-client* libaio libaio1 libnuma bzip2 epel-release
2.修改服务器配置
修改CentOS 6.8服务器配置
[root@binghe ~]# sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/g‘ /etc/selinux/config
3.添加MySQL用户
groupadd mysql && useradd -g -r mysql mysql
4.创建安装MySQL所需要的目录
mkdir -p /usr/local/mysql
mkdir -p /data/mysql/run
mkdir -p /data/mysql/data
mkdir -p /data/mysql/tmp
mkdir -p /data/mysql/conf
mkdir -p /data/mysql/log/bin_log
mkdir -p /data/mysql/log/error_log
mkdir -p /data/mysql/log/query_log
mkdir -p /data/mysql/log/general_log
mkdir -p /data/mysql/log/innodb_ts
mkdir -p /data/mysql/log/undo_space
mkdir -p /data/mysql/log/innodb_log
5.MySQL用户授予目录权限
chown -R mysql.mysql /usr/local/mysql
chown -R mysql.mysql /data/mysql
三、下载并安装MySQL
1.下载安装包
cd /usr/local/src/
wget https://mirrors.ustc.edu.cn/gnu/gmp/gmp-6.1.2.tar.xz
wget https://mirrors.ustc.edu.cn/gnu/mpfr/mpfr-4.0.2.tar.gz
wget https://mirrors.ustc.edu.cn/gnu/mpc/mpc-1.1.0.tar.gz
wget https://mirrors.ustc.edu.cn/gnu/gcc/gcc-9.2.0/gcc-9.2.0.tar.gz
wget https://github.com/Kitware/CMake/releases/download/v3.15.2/cmake-3.15.2.tar.gz
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.23.tar.gz
下载链接https://dev.mysql.com/downloads/mysql/
2.升级gcc
(1)先编译安装gmp->mpfr->mpc
编译gmp
cd /usr/local/src/
xz -d gmp-6.1.2.tar.xz
tar -xvf gmp-6.1.2.tar
cd gmp-6.1.2
./configure --prefix=/usr/local/gmp-6.1.2
make -j $(nproc) && make install
编译mpfr
cd /usr/local/src
tar -xvf mpfr-4.0.2.tar.gz
cd mpfr-4.0.2
./configure --prefix=/usr/local/mpfr-4.0.2 --with-gmp=/usr/local/gmp-6.1.2
make -j $(nproc) && make install
编译mpc
cd /usr/local/src
tar -xvf mpc-1.1.0.tar.gz
cd mpc-1.1.0
./configure --prefix=/usr/local/mpc-1.1.0 --with-mpfr=/usr/local/mpfr-4.0.2 --with-gmp=/usr/local/gmp-6.1.2
make -j $(nproc) && make install
(2)配置mpfr
把mpfr lib加入ld.so.conf 不然gcc编译报错
echo /usr/local/mpfr-4.0.2/lib >> /etc/ld.so.conf
ldconfig
(3)配置系统环境变量
# vim /etc/profile
export MPC_HOME=/usr/local/mpc-1.1.0
export GMP_HOME=/usr/local/gmp-6.1.2
export MPFR_HOME=/usr/local/mpfr-4.0.2
export LD_LIBRARY_PATH=\$MPC_LIB_HOME/lib:\$GMP_HOME/lib:\$MPFR_HOME/lib:\$LD_LIBRARY_PATH
EOF
source /etc/profile
(4)创建软链接
ln -s /usr/local/mpc-1.1.0/lib/libmpc.so.3.1.0 /lib64/libmpc.so.3
ln -s /usr/local/mpc-1.1.0/lib/libmpc.so.3.1.0 /lib/libmpc.so.3
ln -s /usr/local/mpc-1.1.0/lib/libmpc.so.3.1.0 /usr/lib64/libmpc.so.3
ln -s /usr/local/mpc-1.1.0/lib/libmpc.so.3.1.0 /usr/lib/libmpc.so.3
ln -s /usr/local/mpc-1.1.0/lib/libmpc.so.3.1.0 /usr/local/lib64/libmpc.so.3
ln -s /usr/local/mpc-1.1.0/lib/libmpc.so.3.1.0 /usr/local/lib/libmpc.so.3
(5)升级gcc
安装gcc
cd /usr/local/src/
tar -zxvf gcc-9.2.0.tar.gz
cd gcc-9.2.0
./configure --prefix=/usr/local/gcc-9.2.0 -enable-threads=posix -disable-checking -disable-multilib -enable-languages=c,c++ --with-gmp=/usr/local/gmp-6.1.2/ --with-mpfr=/usr/local/mpfr-4.0.2/ --with-mpc=/usr/local/mpc-1.1.0/ -with-tune=generic --with-arch_32=x86-64
make -j $(nproc) && make install -j $(nproc)
备份旧gcc的可执行文件
mv /usr/bin/gcc /usr/bin/gcc.old
mv /usr/bin/g++ /usr/bin/g++.old
mv /usr/bin/c++ /usr/bin/c++.old
mv /usr/bin/cpp /usr/bin/cpp.old
mv /usr/bin/gcov /usr/bin/gcov.old
创建最新gcc执行文件软链
ln -sf /usr/local/gcc-9.2.0/bin/* /usr/bin/
删除gcc/lib64/.py文件
rm -rf /usr/local/gcc-9.2.0/lib64/libstdc++.so.6.0.27-gdb.py
配置ld.so.conf文件
echo /usr/local/gcc-9.2.0/lib64 >> /etc/ld.so.conf
ldconfig
复制libstdc++.so.6.0.27文件到/lib64/
cp /usr/local/gcc-9.2.0/lib64/libstdc++.so.6.0.27 /lib64/
创建软链
cd /lib64
ln -sf libstdc++.so.6.0.27 libstdc++.so.6
查看是否最新版本
strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX
至此,gcc升级完成
(6)升级cmake
编译安装cmake
cd /usr/local/src/
tar -zxvf cmake-3.15.2.tar.gz
cd cmake-3.15.2
./configure --prefix=/usr/local/cmake-3.15.2
gmake -j $(nproc) && gmake install -j $(nproc)
注意:这里使用的是gmake命令
创建cmake 软链
ln -sf /usr/local/cmake-3.15.2/bin/cmake /bin/cmake3
至此,cmake升级完成
(7)编译安装MySQL
cd /usr/local/src
tar -zxvf mysql-8.0.23.tar.gz && rm -rf mysql-8.0.23.tar.gz
cd mysql-8.0.23
cmake3 -DCMAKE_INSTALL_PREFIX=/usr/local/mysql/ -DMYSQL_DATADIR=/data/mysql/data -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=ON -DWITH_INNODB_MEMCACHED=ON -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DCOMPILATION_COMMENT="binghe edition" -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/data/mysql/tmp -DMYSQL_UNIX_ADDR=/data/mysql/run/mysql.sock -DMYSQL_TCP_PORT=3306 -DSYSCONFDIR=/data/mysql/conf -DWITH_READLINE=1 -DFORCE_INSOURCE_BUILD=1 -DWITH_SSL=system -DWITH_ZLIB=system -DCMAKE_CXX_COMPILER=/usr/local/gcc-9.2.0/bin/g++ -DCMAKE_C_COMPILER=/usr/local/gcc-9.2.0/bin/gcc
gmake -j $(nproc) && gmake install -j $(nproc)
编译安装完成后,查看MySQL的安装目录
[root@binghe ~]# ll /usr/local/mysql/
total 956
drwxr-xr-x 2 mysql mysql 4096 Nov 23 02:15 bin
drwxr-xr-x 2 mysql mysql 4096 Nov 23 02:15 docs
drwxr-xr-x 3 mysql mysql 4096 Nov 23 02:14 include
drwxr-xr-x 6 mysql mysql 4096 Nov 23 02:15 lib
-rw-r--r-- 1 mysql mysql 408918 Sep 20 16:30 LICENSE
-rw-r--r-- 1 mysql mysql 102977 Sep 20 16:30 LICENSE.router
-rw-r--r-- 1 mysql mysql 408918 Sep 20 16:30 LICENSE-test
drwxr-xr-x 4 mysql mysql 4096 Nov 23 02:15 man
drwxr-xr-x 10 mysql mysql 4096 Nov 23 02:15 mysql-test
-rw-r--r-- 1 mysql mysql 687 Sep 20 16:30 README
-rw-r--r-- 1 mysql mysql 700 Sep 20 16:30 README.router
-rw-r--r-- 1 mysql mysql 687 Sep 20 16:30 README-test
drwxrwxr-x 2 mysql mysql 4096 Nov 23 02:15 run
drwxr-xr-x 28 mysql mysql 4096 Nov 23 02:15 share
drwxr-xr-x 2 mysql mysql 4096 Nov 23 02:15 support-files
drwxr-xr-x 3 mysql mysql 4096 Nov 23 02:15 var
说明,MySQL 8.0.23编译安装成功
四、配置MySQL
1.配置MySQL用户系统参数
[root@binghe ~]# vim /etc/security/limits.conf
mysql soft nproc 65536
mysql hard nproc 65536
mysql soft nofile 65536
mysql hard nofile 65536
2.配置系统环境变量
# vim /etc/profile
export MYSQL_HOME=/usr/local/mysql
export MPC_HOME=/usr/local/mpc-1.1.0
export GMP_HOME=/usr/local/gmp-6.1.2
export MPFR_HOME=/usr/local/mpfr-4.0.2
export LD_LIBRARY_PATH=\$MPC_LIB_HOME/lib:\$GMP_HOME/lib:\$MPFR_HOME/lib:\$LD_LIBRARY_PATH
export PATH=\$MYSQL_HOME/bin:\$PATH
EOF
source /etc/profile
3.配置/etc/ld.so.conf文件
echo /usr/local/mysql/lib >> /etc/ld.so.conf
ldconfig
4.复制MySQL启动文件
cp mysql.server /etc/init.d/mysqld
chmod 700 /etc/init.d/mysqld
5.配置MySQL开机自启动
chkconfig --level 35 mysqld on
6.配置MySQL配置文件my.cnf
在使用cmake预编译MySQL时,配置了如下参数
-DSYSCONFDIR=/data/mysql/conf
把my.cnf文件放到/data/mysql/conf目录下,如下所示
# vim /data/mysql/conf/my.cnf
[client]
port = 3306
socket = /data/mysql/run/mysql.sock
default-character-set = utf8mb4
# The MySQL server
[mysqld]
port = 3306
user = mysql
socket = /data/mysql/run/mysql.sock
pid_file = /data/mysql/run/mysqld.pid
basedir = /usr/local/mysql
datadir = /data/mysql/data
tmpdir = /data/mysql/tmp
open_files_limit = 65535
explicit_defaults_for_timestamp
server_id = 1
lower_case_table_names = 1
character_set_server = utf8mb4
safe_user_create
max_connections = 3000
max_user_connections=2980
secure_file_priv=/data/mysql/tmp
max_connect_errors = 100000
interactive_timeout = 86400
wait_timeout = 86400
sync_binlog=100
back_log=1024
max_binlog_cache_size=2147483648
max_binlog_size=524288000
default_storage_engine = InnoDB
log_slave_updates = 1
#*********** Logs related settings ***********
log_bin = /data/mysql/log/bin_log/mysql-bin
binlog_format= mixed
binlog_cache_size=32m
max_binlog_cache_size=64m
max_binlog_size=512m
long_query_time = 1
log_output = FILE
log_error = /data/mysql/log/error_log/mysql-error.log
slow_query_log = 1
slow_query_log_file = /data/mysql/log/query_log/slow_statement.log
log_queries_not_using_indexes=0
log_slave_updates=ON
log_slow_admin_statements=1
general_log = 0
general_log_file = /data/mysql/log/general_log/general_statement.log
binlog_expire_logs_seconds = 1728000
relay_log = /data/mysql/log/bin_log/relay-bin
relay_log_index = /data/mysql/log/bin_log/relay-bin.index
#****** MySQL Replication New Feature*********
master_info_repository=TABLE
relay-log-info-repository=TABLE
relay-log-recovery
#*********** INNODB Specific options ***********
innodb_buffer_pool_size = 4096M
transaction_isolation=REPEATABLE-READ
innodb_buffer_pool_instances = 8
innodb_file_per_table = 1
innodb_data_home_dir = /data/mysql/log/innodb_ts
innodb_data_file_path = ibdata1:2048M:autoextend
innodb_thread_concurrency = 8
innodb_log_buffer_size = 67108864
innodb_log_file_size = 1048576000
innodb_log_files_in_group = 4
innodb_max_undo_log_size=4G
innodb_undo_directory=/data/mysql/log/undo_space/
innodb_log_group_home_dir = /data/mysql/log/innodb_log/
innodb_adaptive_flushing=ON
innodb_flush_log_at_trx_commit = 2
innodb_max_dirty_pages_pct = 60
innodb_open_files=60000
innodb_purge_threads=1
innodb_read_io_threads=4
innodb_stats_on_metadata=OFF
innodb_flush_method=O_DIRECT
[mysql]
no-auto-rehash
default-character-set=utf8mb4
net-buffer-length=64K
unbuffered
max-allowed-packet = 2G
default-character-set = utf8mb4
#some var for mysql8
#log_error_verbosity=3
#innodb_print_ddl_logs=1
#binlog_expire_logs_seconds=259200
#innodb_dedicate_server=0
#
#innodb_status_file=1
#innodb_status_output=0
#innodb_status_output_locks=0
[mysqldump]
quick
max_allowed_packet=2G
log_error=/data/mysql/log/error_log/mysql_dump_error.log
net_buffer_length=8k
五、初始化MySQL并启动MySQL
初始化MySQL数据
mysqld --initialize --user=mysql --defaults-file=/data/mysql/conf/my.cnf
启动MySQL
systemctl start mysqld
六、登录MySQL并修改密码
初始化数据时,MySQL会为root账户自动生成一个临时密码保存在my.cnf文件log_error配置的文件中,在my.cnf中配置如下参数
log_error=/data/mysql/log/error_log/mysql-error.log
所以,MySQL为root账户自动生成的临时密码保存在/data/mysql/log/error_log/mysql-error.log文件中
查看MySQL为root账户自动生成的临时密码,如下所示。
cat "temporary password" /data/mysql/log/error_log/mysql-error.log
在mysql-error.log文件中,找到如下一行代码。
2019-11-22T19:05:02.240204Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ujbbqC?A+38g
可看到临时密码为:ujbbqC?A+38g
使用root账户和这个临时密码登录MySQL服务器
[root@binghe ~]# mysql -hlocalhost -uroot -pujbbqC?A+38g
mysql>
修改MySQL的root密码
mysql> ALTER USER ‘root‘@‘localhost‘ IDENTIFIED BY ‘@Binghe123456‘;
Query OK, 0 rows affected (0.03 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
退出MySQL,并使用root账户和修改后的密码登录MySQL,如下所示
mysql> exit
Bye
[root@binghe150 ~]# mysql -hlocalhost -uroot -p@Binghe123456
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql>
此时,使用root账户和修改后的密码成功登录MySQL,说明root账户密码修改成功
MySQL命令行执行SQL语句,如下所示
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql> USE mysql;
Database changed
mysql>
可以看到,修改root账户密码后,再次登录MySQL,可以在命令行正确执行SQL语句
至此,源码编译MySQL8.x+升级gcc+升级cmake完美完成
七、遇到的问题和解决方案
编译安装gcc时报错,报错信息如下所示
/usr/local/src/gcc-9.2.0/host-x86_64-pc-linux-gnu/gcc/xgcc -B/usr/local/src/gcc-9.2.0/host-x86_64-pc-linux-gnu/gcc/ -xc -nostdinc /dev/null -S -o /dev/null -fself-test=../.././gcc/testsuite/selftests
/usr/local/src/gcc-9.2.0/host-x86_64-pc-linux-gnu/gcc/cc1plus: error while loading shared libraries: libmpc.so.3: cannot open shared object file: No such file or directory
make[3]: *** [s-selftest-c++] Error 1
make[3]: *** Waiting for unfinished jobs....
/usr/local/src/gcc-9.2.0/host-x86_64-pc-linux-gnu/gcc/cc1: error while loading shared libraries: libmpc.so.3: cannot open shared object file: No such file or directory
make[3]: *** [s-selftest-c] Error 1
/usr/local/src/gcc-9.2.0/host-x86_64-pc-linux-gnu/gcc/cc1: error while loading shared libraries: libmpc.so.3: cannot open shared object file: No such file or directory
/bin/sh ../.././gcc/../move-if-change tmp-macro_list macro_list
echo timestamp > s-macro_list
rm gcc.pod
make[3]: Leaving directory `/usr/local/src/gcc-9.2.0/host-x86_64-pc-linux-gnu/gcc‘
make[2]: *** [all-stage1-gcc] Error 2
make[2]: Leaving directory `/usr/local/src/gcc-9.2.0‘
make[1]: *** [stage1-bubble] Error 2
make[1]: Leaving directory `/usr/local/src/gcc-9.2.0‘
make: *** [all] Error 2
/usr/local/src/gcc-9.2.0/host-x86_64-pc-linux-gnu/gcc/cc1: error while loading shared libraries: libgmp.so.10: cannot open shared object file: No such file or directory
/usr/local/src/gcc-9.2.0/host-x86_64-pc-linux-gnu/gcc/cc1: error while loading shared libraries: libgmp.so.10: cannot open shared object file: No such file or directory
make[3]: *** [s-selftest-c] Error 1
make[3]: *** Waiting for unfinished jobs....
/usr/local/src/gcc-9.2.0/host-x86_64-pc-linux-gnu/gcc/cc1plus: error while loading shared libraries: libgmp.so.10: cannot open shared object file: No such file or directory
/bin/sh ../.././gcc/../move-if-change tmp-macro_list macro_list
make[3]: *** [s-selftest-c++] Error 1
echo timestamp > s-macro_list
rm gcc.pod
make[3]: Leaving directory `/usr/local/src/gcc-9.2.0/host-x86_64-pc-linux-gnu/gcc‘
make[2]: *** [all-stage1-gcc] Error 2
make[2]: Leaving directory `/usr/local/src/gcc-9.2.0‘
make[1]: *** [stage1-bubble] Error 2
make[1]: Leaving directory `/usr/local/src/gcc-9.2.0‘
make: *** [all] Error 2
解决方案如下:
配置系统环境变量
cat >> /etc/profile <<EOF
export MPC_HOME=/usr/local/mpc-1.1.0
export GMP_HOME=/usr/local/gmp-6.1.2
export MPFR_HOME=/usr/local/mpfr-4.0.2
export LD_LIBRARY_PATH=\$MPC_LIB_HOME/lib:\$GMP_HOME/lib:\$MPFR_HOME/lib:\$LD_LIBRARY_PATH
EOF
source /etc/profile
创建系统软链接
ln -s /usr/local/mpc-1.1.0/lib/libmpc.so.3.1.0 /lib64/libmpc.so.3
ln -s /usr/local/mpc-1.1.0/lib/libmpc.so.3.1.0 /lib/libmpc.so.3
ln -s /usr/local/mpc-1.1.0/lib/libmpc.so.3.1.0 /usr/lib64/libmpc.so.3
ln -s /usr/local/mpc-1.1.0/lib/libmpc.so.3.1.0 /usr/lib/libmpc.so.3
ln -s /usr/local/mpc-1.1.0/lib/libmpc.so.3.1.0 /usr/local/lib64/libmpc.so.3
ln -s /usr/local/mpc-1.1.0/lib/libmpc.so.3.1.0 /usr/local/lib/libmpc.so.3
严格按照本文编译安装MySQL 8.x不会有这个问题
八、其他安装方式
如果下载的MySQL源码包不是mysql-8.0.23.tar.gz而是mysql-boost-8.0.23.tar.gz
因为mysql-boost-8.0.23.tar.gz中自带boost安装文件,在cmake时就不需要下载boost文件
mysql-boost-8.0.23.tar.gz按照如下方式编译安装MySQL
cd /usr/local/src
tar -zxvf mysql-boost-8.0.23.tar.gz && mysql-boost-8.0.23.tar.gz
cd mysql-8.0.23
cmake3 -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql/data -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=ON -DWITH_INNODB_MEMCACHED=ON -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DCOMPILATION_COMMENT="binghe edition" -DWITH_BOOST=/usr/local/src/mysql-8.0.23/boost/boost_1_70_0 -DMYSQL_UNIX_ADDR=/data/mysql/run/mysql.sock -DMYSQL_TCP_PORT=3306 -DSYSCONFDIR=/data/mysql/conf -DWITH_READLINE=1 -DFORCE_INSOURCE_BUILD=1 -DWITH_SSL=system -DWITH_ZLIB=system -DCMAKE_CXX_COMPILER=/usr/local/gcc-9.2.0/bin/g++ -DCMAKE_C_COMPILER=/usr/local/gcc-9.2.0/bin/gcc
gmake -j $(nproc) && gmake install -j $(nproc)
与编译安装mysql-8.0.23.tar.gz源码包相比,编译安装mysql-boost-8.0.23.tar.gz时,使用cmake命令预编译时,去掉参数-DDOWNLOAD_BOOST=1并且将参数-DWITH_BOOST=/data/mysql/tmp,修改为-DWITH_BOOST=/usr/local/src/mysql-8.0.23/boost/boost_1_70_0。也就是说,在mysql-boost-8.0.23.tar.gz源码包中,boost安装文件存放在解压后的mysql-8.0.23目录下的boost/boost_1_70_0目录下。
至此,整个编译安装与测试过程完美结束
九、下载文件
链接:https://download.csdn.net/download/l1028386804/11992200下载升级gcc与cmake所需的安装文件
链接:https://download.csdn.net/download/l1028386804/11992212下载MySQL 8.0.23源码安装文件
MySQL之升级gcc+升级cmake+源码编译MySQL 8.0
标签:databases stc ali sla lock mod status character ola