环境说明:

CentOS Linux release 7.4.1708 (Core)
内核版本: 3.10.0-693.el7.x86_64
工作目录:/usr/local/src/
mysql 安装目录:/apps/program
其它程序安装目录:/usr/local/程序名+版本号

安装源码准备

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://cdn.mysql.com//Downloads/MySQL-8.0/mysql-boost-8.0.17.tar.gz

安装依赖

yum -y install epel-release
yum -y group install "Development Tools"
mysql 依赖
yum -y install ncurses-devel openssl openssl-devel bison

安装gcc 源码编译依赖

## 先编译gmp->mpfr->mpc

cd  /usr/local/src/
编译: gmp-6.1.2
tar -xvf gmp-6.1.2.tar.xz
cd gmp-6.1.2
./configure --prefix=/usr/local/gmp-6.1.2
make -j $(nproc)
make install
cd ../
编译:mpfr-4.0.2
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
cd ../
编译:mpc-1.1.0
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
# 把mpfr lib 加入 ld.so.conf 不然gcc 编译报错
echo /usr/local/mpfr-4.0.2/lib  >> /etc/ld.so.conf
ldconfig

编译安装gcc

cd /usr/local/src/
tar -xvf 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/
 ## 删除lib64 目录下.py 文件不然ldconfig 报错
 rm -rf /usr/local/gcc-9.2.0/lib64/libstdc++.so.6.0.27-gdb.py
 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/
# 创建软链 libstdc++.so.6
cd /lib64
ln  -sf libstdc++.so.6.0.27 libstdc++.so.6
## 查看是否最新版本
strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX

编译安装cmake

cd  /usr/local/src/
tar -xvf 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)
# 创建cmake 软链
 ln -sf /usr/local/cmake-3.15.2/bin/cmake /bin/cmake3

编译mysql

cd   /usr/local/src/
tar -xvf mysql-boost-8.0.17.tar.gz
cd mysql-boost-8.0.17
cmake3 . -DCMAKE_INSTALL_PREFIX=/apps/program/mysql \
                -DMYSQL_DATADIR=/apps/program/data \
                -DSYSCONFDIR=/etc \
                -DENABLED_LOCAL_INFILE=ON \
                -DWITH_INNODB_MEMCACHED=ON \
                -DWITH_INNOBASE_STORAGE_ENGINE=1 \
                -DWITH_ARC\H\I\G\VE_STORAGE_ENGINE=1 \           # 请删除\ 跟\G是敏感词发不出去
                -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
                -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
                -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
                -DWITH_READLINE=1 \
                -DFORCE_INSOURCE_BUILD=1 \
                -DMYSQL_UNIX_ADDR=/apps/program/tmp/mysql.sock \
                -DWITH_SSL=system \
                -DWITH_ZLIB=system \
                -DWITH_BOOST=./boost/boost_1_69_0 \
                -DDEFAULT_CHARSET=utf8 \
                -DDEFAULT_COLLATION=utf8_general_ci
gmake -j $(nproc)
gmake install -j $(nproc)

## 说明: [ 36%] Building CXX object storage/innobase/CMakeFiles/innobase.dir/buf/buf0buf.cc.o 
                /usr/src/mysql-8.0.17/storage/innobase/buf/buf0buf.cc: In function ‘void buf_pool_create(buf_pool_t*, ulint, ulint, std::mutex*, dberr_t&)’: 
                /usr/src/mysql-8.0.17/storage/innobase/buf/buf0buf.cc:1220:44: error: ‘SYS_gettid’ was not declared in this scope 
                1220 | setpriority(PRIO_PROCESS, (pid_t)syscall(SYS_gettid), -20); 
                | ^~~~~~~~~~ 
                make[2]: *** [storage/innobase/CMakeFiles/innobase.dir/buf/buf0buf.cc.o] Error 1 
                make[1]: *** [storage/innobase/CMakeFiles/innobase.dir/all] Error 2 
                make: *** [all] Error 2
                                这样的错误的话检测gcc 软链是否创建lib64 是否t添加到ld.so.conf 并且生效
# 复制启动文件
cp support-files/mysql.server /etc/init.d/mysqld
chmod 700 /etc/init.d/mysqld

# 添加mysql 到环境变量
echo export PATH=$PATH:/apps/program/mysql/bin >>/etc/profile
source /etc/profile
echo /apps/program/mysql/lib >> /etc/ld.so.conf
ldconfig

# 创建mysql 账号
 useradd mysql -s /sbin/nologin -M
 # 创建mysql 所需文件加
 mkdir /apps/program/{data,tmp,logs} -p
 mkdir -p /apps/program/logs/{mysqld,backup_mysqld}
 # /apps/program mysql 账号权限
 chown -R mysql:mysql /apps/program 

gcc  9.2.0 编译安装mysql 8.0.17
编辑my.cnf 文件

vi /etc/my.cnf

[client]
port    = 3306
socket  = /apps/program/tmp/mysql.sock

[mysql]
no-auto-rehash
no-beep
default-character-set = utf8mb4
socket=/apps/program/tmp/mysql.sock
#prompt="\\@\\h \\d \\R:\\m:\\s> "
#tee="/apps/logs/mysql/audit.log" 
#pager="less -i -n -S" 
net-buffer-length=64K
unbuffered
max-allowed-packet = 2G # add by alading on 20151110

[mysqld]
user=mysql
bind-address=0.0.0.0
port=3306
mysqlx-port=33060
server-id=5
basedir=/apps/program/mysql
pid-file=/apps/program/tmp/mysql.pid
socket=/apps/program/tmp/mysql.sock
datadir=/apps/program/data
tmpdir=/apps/program/tmp
plugin-dir=/apps/program/mysql/lib/plugin
character-set-server = utf8mb4
skip-name-resolve = 1
# 为了兼容客户段程序不支持caching_sha2_password加密修改默认加密为mysql_native_password
default-authentication-plugin=mysql_native_password
default-time-zone = "+8:00"
#******security
safe-user-create
max-user-connections=2980
max-connect-errors=100000
secure-file-priv=/apps/program/tmp

open-files-limit    = 65535
back-log = 1024
max-connections = 512
max-connect-errors = 1000000
table-open-cache = 1024
table-definition-cache = 1024
table-open-cache-instances = 64
thread-stack = 512K
external-locking = FALSE
max-allowed-packet = 32M
sort-buffer-size = 4M
join-buffer-size = 4M
thread-cache-size = 768
interactive-timeout = 600
wait-timeout = 600
tmp-table-size = 32M
max-heap-table-size = 32M
slow-query-log = 1
log-timestamps = SYSTEM
slow-query-log-file = /apps/program/logs/mysqld/slow.log
log-error = /apps/program/logs/mysqld/error.log
long-query-time = 0.1
log-queries-not-using-indexes =1
log-throttle-queries-not-using-indexes = 60
min-examined-row-limit = 100
log-slow-admin-statements = 1
log-slow-slave-statements = 1
log-bin = /apps/program/logs/mysqld/mysql-bin
sync-binlog = 1
binlog-cache-size = 4M
max-binlog-cache-size = 2G
max-binlog-size = 1G

expire-logs-days = 3

master-info-repository = TABLE
relay-log-info-repository = TABLE
gtid-mode = on
enforce-gtid-consistency = 1
log-slave-updates
slave-rows-search-algorithms = 'INDEX_SCAN,HASH_SCAN'
binlog-format = row
binlog-checksum = 1
relay-log-recovery = 1
relay-log-purge = 1
key-buffer-size = 32M
read-buffer-size = 8M
read-rnd-buffer-size = 4M
bulk-insert-buffer-size = 64M
myisam-sort-buffer-size = 128M
myisam-max-sort-file-size = 10G
myisam-repair-threads = 1
lock-wait-timeout = 3600
explicit-defaults-for-timestamp = 1
innodb-thread-concurrency = 0
innodb-sync-spin-loops = 100
innodb-spin-wait-delay = 30

transaction-isolation = REPEATABLE-READ
#innodb-additional-mem-pool-size = 16M
innodb-buffer-pool-size = 4096M  # #for system 60%mem 
innodb-buffer-pool-instances = 4  # change from 4 to default value 8 on 20150210
innodb-buffer-pool-load-at-startup = 1
innodb-buffer-pool-dump-at-shutdown = 1
innodb-data-file-path = ibdata1:1G:autoextend
innodb-flush-log-at-trx-commit = 1
innodb-log-buffer-size = 32M
innodb-log-file-size = 2G
innodb-log-files-in-group = 2
innodb-max-undo-log-size = 4G
innodb-undo-directory = /apps/program/undo_space
innodb-undo-tablespaces = 95

innodb-io-capacity = 4000  # ssd 可以调整更大
innodb-io-capacity-max = 8000  # ssd 可以调整更大
innodb-flush-sync = 0
innodb-flush-neighbors = 0
innodb-write-io-threads = 8
innodb-read-io-threads = 8
innodb-purge-threads = 4
innodb-page-cleaners = 4
innodb-open-files = 65535
innodb-max-dirty-pages-pct = 50
innodb-flush-method = O_DIRECT
innodb-lru-scan-depth = 4000
innodb-checksum-algorithm = crc32
innodb-lock-wait-timeout = 10
innodb-rollback-on-timeout = 1
innodb-print-all-deadlocks = 1
innodb-file-per-table = 1
innodb-online-alter-log-max-size = 4G
innodb-stats-on-metadata = 0
default-storage-engine  = InnoDB

# some var for MySQL 8
log-error-verbosity = 3
innodb-print-ddl-logs = 1
binlog-expire-logs-seconds = 259200
#innodb-dedicated-server = 0

innodb-status-file = 1
innodb-status-output = 0
innodb-status-output-locks = 0

#performance-schema
performance-schema = 1
performance-schema-instrument = '%memory%=on'
performance-schema-instrument = '%lock%=on'

#innodb monitor
innodb-monitor-enable="module_innodb"
innodb-monitor-enable="module_server"
innodb-monitor-enable="module_dml"
innodb-monitor-enable="module_ddl"
innodb-monitor-enable="module_trx"
innodb-monitor-enable="module_os"
innodb-monitor-enable="module_purge"
innodb-monitor-enable="module_log"
innodb-monitor-enable="module_lock"
innodb-monitor-enable="module_buffer"
innodb-monitor-enable="module_index"
innodb-monitor-enable="module_ibuf_system"
innodb-monitor-enable="module_buffer_page"
innodb-monitor-enable="module_adaptive_hash"
#**************************federated********************
#federated

[mysqldump]
quick
max-allowed-packet = 2G
log-error=/apps/program/logs/mysqld/dump.log
net-buffer-length=8k

[mysqladmin]
default-character-set = utf8mb4
socket=/apps/program/tmp/mysql.sock

初始化数据库

/apps/program/mysql/bin/mysqld --initialize-insecure  --user=mysql
# 说明: --initialize-insecure 是初始化无需生成默认密码直接mysql 能进去不过记得一定要设置密码 --initialize 初始化设置默认密码
/apps/program/mysql/bin/mysqld --initialize  --user=mysql
#启动mysql
/etc/init.d/mysqld start
在/apps/program/logs/mysqld/error.log 查找密码
[root@xxx mysqld]# cat /apps/program/logs/mysqld/error.log | grep  root@localhost
2019-08-30T12:32:40.034868+08:00 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: h3zuqsQhPO&4
这个就是密码h3zuqsQhPO&4
这些密码必须修改不然任何事都不能做
修改密码:
alter user user()identified by"123456"; 
flush privileges;
关闭mysql 
shutdown;
启动mysql 
/etc/init.d/mysqld start
mysql -p
# 输入刚刚设置密码 即可登陆MySQL

mysql 创建用户

mysql 8.0 不能使用GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY '123456'; FLUSH PRIVILEGES; 这样的方式创建账号密码

create user 'root'@'%' identified by '123456';
flush privileges;
grant all privileges on *.* to 'root'@'%';
flush privileges;

在远程使用Navicat  或者启动mysql 客户端工具进行连接
没意外的话就能正常登陆
mysql 8.0.17 源码编译安装完成