MySQL 8.0 CENTOS 7环境编译安装

这个blog,我们来聊聊MySQL 8.0在CENTOS 7 环境下的安装

Linux先安装MySQL的方法有多种:
1.仓库安装(yum,apt,zypper)
2.二进制包安装(rpm,deb)
3.docker安装
4.压缩包解压安装
5.编译安装

这里我们选择最后一种,编译安装。

一.下载软件

官网选择最新版本下载

image.png

二.环境准备

1.安装所需的基本的包

yum -y install ntpdate gcc gcc-c++ ncurses ncurses-devel cmake readline-devel zlib.x86_64 zlib-devel.x86_64 bison libcurl-* net-tool* tree nmap sysstat lrzsz dos2unix telnet.x86_64 nethogs iftop iotop unzip ftp.x86_64 xfs* expect vim wget psmisc openssh-client* libaio libaio1 libnuma openssl-devel bzip2

2.检查系统是否已经安装了mysql
centos 7开始默认安装的是mariadb了,存在的话,先卸载

-- 检查是否已经安装mysql
[root@**** ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[root@**** ~]# rpm -e mariadb-libs-5.5.56-2.el7.x86_64 --nodeps
[root@**** ~]# 

3.关掉防火墙和SElinux

systemctl stop firewalld
systemctl disabled firewalld
systemctl stop postfix
systemctl disable postfix
sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config && cat /etc/selinux/config >> ${tmplog_dir}/base.log 2>&1
setenforce 0
getenforce

4.升级gcc

-- 升级比较慢,需要耐心等待
mkdir /usr/local/tools && cd /usr/local/tools
wget http://mirrors.nju.edu.cn/gnu/gcc/gcc-5.5.0//gcc-5.5.0.tar.xz
tar xf gcc-5.5.0.tar.xz
cd gcc-5.5.0
./contrib/download_prerequisites
mkdir build && cd build
../configure --enable-checking=release --enable-languages=c,c++ --disable-multilib
make && make install

5.升级cmake

cd /usr/local/tools
wget https://github.com/Kitware/CMake/releases/download/v3.14.5/cmake-3.14.5.tar.Z
tar -xvf cmake-3.14.5.tar.Z
cd cmake-3.14.5/
./bootstrap
make && make install

如果出现以下错误
ibstdc++.so.6: version `CXXABI_3.4.20' not found (required by ./destructor)
处理如下:

find / -name "libstdc++.so*"
mv /usr/lib64/libstdc++.so.6.0.19 /tmp/
cp /usr/local/tools/gcc-5.5.0/build/x86_64-unknown-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6.0.21 /usr/lib64/
mv /usr/lib64/libstdc++.so.6 /tmp/
cd /usr/lib64/
ln -s libstdc++.so.6.0.21 libstdc++.so.6

6.创建用户及目录

-- 创建用户
groupadd mysql
useradd -d /home/mysql -g mysql -m mysql
passwd mysql
-- 创建目录
mkdir  -p /u01/my3306/data
mkdir  -p /u01/my3306/log/iblog
mkdir  -p /u01/my3306/log/binlog
mkdir  -p /u01/my3306/run
mkdir  -p /u01/my3306/tmp
-- 授权
chown -R mysql:mysql /u01/my3306
chmod -R 755 /u01/my3306

7.配MySQL环境变量

su - mysql
vi .bash_profile
PATH  :/u01/my3306/bin

source .bash_profile 

8.上传并解压文件

tar -zxvf mysql-boost-8.0.20.tar.gz -C /usr/local

三.安装

-- 上一步解压的位置
cd /usr/local/mysql-8.0.20
--cmake编译安装
cmake . \
-DWITH_BOOST=/usr/local/mysql-8.0.20/boost/boost_1_70_0 \
-DCMAKE_INSTALL_PREFIX=/u01/my3306 \
-DMYSQL_DATADIR=/u01/my3306/data \
-DWITHOUT_CSV_STORAGE_ENGINE=1 \
-DWITHOUT_FEDERATED_STORAGE_ENGINE=1 \
-DWITHOUT_ARCHIVE_STORAGE_ENGINE=1 \
-DFORCE_INSOURCE_BUILD=1 \
-DWITH_SSL=system \
-DCMAKE_CXX_COMPILER=/usr/local/bin/g++ \
-DCMAKE_C_COMPILER=/usr/local/bin/gcc

make -j 8 && make install

四.配置文件及初始化

配置文件:
路径:/u01/my3306/my.cnf

[client]
port=3306
socket=/u01/my3306/mysql.sock

[mysql]
pid_file=/u01/my3306/run/mysqld.pid

[mysqld]
basedir = /u01/my3306
datadir = /u01/my3306/data
port = 3306
socket = /u01/my3306/mysql.sock

#必填项
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

#binlog
log_bin=/u01/my3306/log/binlog/binlog
binlog_format=row
expire_logs_days=7
log_slave_updates=ON
sync_binlog=100

#logging
log_error=/u01/my3306/log/error.log
slow_query_log_file=/u01/my3306/log/slow.log
log_queries_not_using_indexes=0
slow_query_log=1
log_slave_updates=ON
log_slow_admin_statements=1
long_query_time=1

初始化:

chown -R mysql:mysql /u01/my3306
chown -R mysql:mysql /usr/local/mysql-8.0.20
su - mysql
/usr/local/mysql-8.0.20/bin/mysqld --basedir=/u01/my3306 --initialize --datadir=/u01/my3306/data --user=mysql

生成的临时密码一定要保留,后面登陆需要使用

[10:36:47] [mysql@*** data]$ /usr/local/mysql-8.0.20/bin/mysqld --basedir=/u01/my3306 --initialize --datadir=/u01/my3306/data --user=mysql
[10:36:47] 2020-05-29T02:36:59.773440Z 0 [Warning] [MY-010139] [Server] Changed limits: max_open_files: 1024 (requested 8161)
[10:36:47] 2020-05-29T02:36:59.773474Z 0 [Warning] [MY-010142] [Server] Changed limits: table_open_cache: 431 (requested 4000)
[10:36:47] 2020-05-29T02:36:59.774430Z 0 [System] [MY-013169] [Server] /usr/local/mysql-8.0.20/bin/mysqld (mysqld 8.0.20) initializing of server in progress as process 27145
[10:36:47] 2020-05-29T02:36:59.790675Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
[10:36:48] 2020-05-29T02:37:00.416071Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
[10:36:50] 2020-05-29T02:37:01.867872Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: eIzp%ZzE-3Rs

检查data目录文件是否已生成

[mysql@**** ~]$ /usr/local/mysql-8.0.20/bin/mysqld --defaults-file=/u01/my3306/my.cnf --initialize --datadir=/u01/my3306/data --user=mysql
[mysql@*** ~]$ cd /u01/my3306/data/
[mysql@*** data]$ ll
total 45040
-rw-r----- 1 mysql mysql       56 May 28 22:07 auto.cnf
-rw------- 1 mysql mysql     1676 May 28 22:07 ca-key.pem
-rw-r--r-- 1 mysql mysql     1112 May 28 22:07 ca.pem
-rw-r--r-- 1 mysql mysql     1112 May 28 22:07 client-cert.pem
-rw------- 1 mysql mysql     1676 May 28 22:07 client-key.pem
-rw-r----- 1 mysql mysql   491520 May 28 22:07 #ib_16384_0.dblwr
-rw-r----- 1 mysql mysql  8880128 May 28 22:07 #ib_16384_1.dblwr
drwxr-x--- 2 mysql mysql        6 May 28 22:07 #innodb_temp
drwxr-x--- 2 mysql mysql        6 May 28 22:07 mysql
-rw-r----- 1 mysql mysql 15728640 May 28 22:07 mysql.ibd
drwxr-x--- 2 mysql mysql     8192 May 28 22:07 performance_schema
-rw------- 1 mysql mysql     1676 May 28 22:07 private_key.pem
-rw-r--r-- 1 mysql mysql      452 May 28 22:07 public_key.pem
-rw-r--r-- 1 mysql mysql     1112 May 28 22:07 server-cert.pem
-rw------- 1 mysql mysql     1676 May 28 22:07 server-key.pem
-rw-r----- 1 mysql mysql 10485760 May 28 22:07 undo_001
-rw-r----- 1 mysql mysql 10485760 May 28 22:07 undo_002

五.MySQL启动和登陆

启动MySQL

mysqld_safe --defaults-file=/u01/my3306/my.cnf --user=mysql &

修改密码:

-- 这个地方的密码是第四步生成的临时密码,必须修改了才能正常使用mysql
[mysql@*** my3306]$ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.20 Source distribution

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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> 
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'test';
Query OK, 0 rows affected (0.01 sec)

mysql> 
mysql> 
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

六.FAQ

1.gcc、cmake下载较慢
软件下载较慢,可以通过其它途径下载,然后传到服务器即可

2.初始化密码忘记保存
删除数据文件后重新初始化

cd /u01/my3306/data/
rm -rf *
/usr/local/mysql-8.0.20/bin/mysqld --basedir=/u01/my3306 --initialize --datadir=/u01/my3306/data --user=mysql

3.Can't connect to local MySQL server through socket '/tmp/mysql.sock'
登陆的时候报错,/tmp/mysql.sock不存在,创建软链接即可

ln -s /u01/my3306/mysql.sock /tmp/mysql.sock

4.启动报错
启动报错,多半是my.cnf配置错误,查看指定的errlog进行排查
more /u01/my3306/log/error.log

你可能感兴趣的:(MySQL 8.0 CENTOS 7环境编译安装)