一、RPM包安装
下载mysql相关安装包
[root@localhost ~]# wget https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql80-community-el7/mysql-community-server-8.0.18-1.el7.x86_64.rpm [root@localhost ~]# wget https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql80-community-el7/mysql-community-libs-8.0.18-1.el7.x86_64.rpm [root@localhost ~]# wget https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql80-community-el7/mysql-community-common-8.0.18-1.el7.x86_64.rpm [root@localhost ~]# wget https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql80-community-el7/mysql-community-client-8.0.18-1.el7.x86_64.rpm
安装mysql
[root@localhost ~]# mkdir mysql8 [root@localhost ~]# mv mysql* mysql8/ [root@localhost ~]# cd mysql8/ [root@localhost mysql8]# yum -y install msyql*
自定义配置mysql
[root@localhost mysql8]# vi /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql port=3306 skip-grant-tables log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
[root@localhost mysql8]# systemctl restart mysqld
[root@localhost mysql8]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 8.0.18 MySQL Community Server - GPL Copyright (c) 2000, 2019, 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初始密码
[root@localhost mysql8]# cat /var/log/mysqld.log|grep root@localhost 2020-02-02T13:40:45.775830Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: jtpYbwr7gu.S
(root@localhost:后面的就是初始密码)
修改mysql密码(初次登录必须修改密码,否则无法使用mysql命令)
mysql> alter user 'root'@'localhost' identified by 'Qwer!234'; Query OK, 0 rows affected (0.39 sec)
二、源码版安装
非root用户必须要有sudo权限
1.下载相关源码包
https://mirrors.tuna.tsinghua.edu.cn/gnu/gcc/gcc-9.2.0/gcc-9.2.0.tar.gz https://mirrors.tuna.tsinghua.edu.cn/gnu/gmp/gmp-6.2.0.tar.xz https://mirrors.tuna.tsinghua.edu.cn/gnu/m4/m4-latest.tar.gz https://mirrors.tuna.tsinghua.edu.cn/gnu/mpfr/mpfr-4.0.2.tar.gz https://mirrors.tuna.tsinghua.edu.cn/gnu/mpc/mpc-1.1.0.tar.gz
https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-8.0/mysql-8.0.19.tar.gz
http://dl.bintray.com/boostorg/release/1.70.0/source/boost_1_70_0.tar.gz
https://down.24kplus.com/linux/cmake/cmake-3.16.2.tar.gz
2.安装低版本的gcc、gcc-c++(必须要做,否则升级gcc安装m4/gmp/mpfr/mpc时configure会报错:configure: error: no acceptable C compiler found in $PATH)
到该网址下寻找gcc、gcc-c++及其依赖包(能连外网的可以直接使用命令:sudo yum -y install gcc gcc-c++)
https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/Packages/
3.安装源码版cmake
必须安装openssl和openssl-devel依赖,下载网址同上,否则bootstrap会报错:找不到openssl(能连外网的可以直接使用命令:sudo yum -y install openssl openssl-devel)
tar cmake-3.16.1.tgz cd cmake-3.16.1
sudo ./bootstrap
sudo make
sudo make check
sudo make install
4.升级gcc、gcc-c++
a.安装m4
tar -xzvf m4-latest.tar.gz
cd m4-1.4.17/
sudo ./configure --prefix=/usr/local(--prefix指定安装路径)
sudo make (编译)
sudo make check (检查编译是否有错,注意是否有Error,可以不做)
sudo make install (安装)
m4 --version (查看m4版本)
正确安装后可以看到如下结果
b.安装gmp
sudo ln -s /usr/local/bin/m4 /usr/bin (把m4做一个软连接到此路径下,否则configure时会报错:checking for suitable m4... configure: error: No usable m4 in $PATH or /usr/5bin (see config.log for reasons).)
tar -xvf gmp-6.2.0.tar.xz
cd gmp-6.2.0
sudo ./configure --prefix=/usr/local/gmp-6.2 (非管理员必须要sudo,否则会报错:Permission denied)
sudo make
sudo make check
sudo make install
c.安装mpfr
tar -xzvf mpfr-4.0.2.tar.gz cd mpfr-4.0.2 sudo ./configure --prefix=/usr/local/mpfr-4.0 --with-gmp=/usr/local/gmp-6.2 (--with-gmp为gmp的安装目录) sudo make sudo make check sudo make install
d.安装mpc
tar -xzvf mpc-1.1.0.tar.gz cd mpc-1.1 sudo ./configure --prefix=/usr/local/mpc-1.1 --with-gmp=/usr/local/gmp-6.2 --with-mpfr=/usr/local/mpfr-4.0 sudo make sduo make check sudo make install
e.加入库文件(/usr/local/mpfr-4.0/lib/必须加入库文件,其他两个可以不做,否则安装升级gcc编译时会报错:error while loading shared libraries: libmpfr.so.6: cannot open shared object file: No such file or directory)
sudo vi /etc/ld.so.conf
/usr/local/mpfr-4.0/lib/
/usr/local/gmp-6.2/lib
/usr/local/mpc-1.1/lib
sudo ldconfig (使上面的操作生效)
或者做软连接
sudo ln -s /usr/local/mpfr-4.0/lib/libmpfr.so.6 /usr/bin
或者复制
sudo cp /usr/local/mpfr-4.0/lib/libmpfr.so.6 /usr/bin/
f.安装其他依赖(系统未安装依赖包时,必须要装,否则升级gcc编译时会报错:mpc.h:没有那个文件或目录)
下载gmp-devel和libmpc-devel及其依赖包(能连外网的可以直接使用命令:sudo yum -y install gmp-devel libmpc-devel)
https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/Packages/gmp-6.0.0-15.el7.x86_64.rpm https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/Packages/gmp-devel-6.0.0-15.el7.x86_64.rpm https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/Packages/libmpc-1.0.1-3.el7.x86_64.rpm https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/Packages/libmpc-devel-1.0.1-3.el7.x86_64.rpm https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/Packages/mpfr-3.1.1-4.el7.x86_64.rpm https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/Packages/mpfr-devel-3.1.1-4.el7.x86_64.rpm
若还有依赖包需下载,请到https://mirrors.tuna.tsinghua.edu.cn自行下载
g.升级gcc
tar -xzvf gcc-9.2.0.tar.gz cd gcc-9.2.0
sudo ./configure --prefix=/usr/local/gcc-9.2 \
--enable-bootstrap\
--enable-checking=release \
--enable-languages=c,c++ \
--enable-threads=posix \
--disable-checking\
--disable-multilib\
--enable--long-long\
--with-gmp=/usr/local/gmp-6.2\
--with-mpfr=/usr/local/mpfr-4.0\
--with-mpc=/usr/local/mpc-1.1
sudo make (编译时间较长,1个小时的样子) sudo make check sudo make install
务必卸载低版本的gcc、gcc-c++
sudo rpm -e gcc-c++
sudo rpm -e gcc
sudo vi /etc/profile export PATH=$PATH:/usr/local/gcc/bin
source /etc/profile
查看gcc版本号
gcc --version
5.安装源码版mysql
安装ncurses-devel依赖,否则cmake报错:Curses library not found.Please install appropriate package(能连外网的可以直接使用命令:sudo yum -y install ncurses-devel,不能连上外网的下载地址同第2步一样)
tar -xzvf mysql-8.0.19.tar.gz cd mysql-8.0.19
cmake . -DCMAKE_INSTALL_PREFIX=/app/mysql\
-DDEFAULT_CHARSET=utf8\
-DDEFAULT_COLLATION=utf8_general_ci\
-DENABLED_LOCAL_INFILE=ON\
-DWITH_SSL=system\
-DMYSQL_DATADIR=/app/mysql/data\
-DSYSCONFDIR=/app/mysql/config\
-DMYSQL_TCP_PORT=3306\
-DMYSQL_UNIX_ADDR=/app/mysql/mysql.sock\
-DWITH_BOOST=/home/mcbadm/mysql8/\
-DFORCE_INSOURCE_BUILD=1
以上参数介绍如下(简单的参数,更详细参数请参考https://blog.51cto.com/laowafang/1294964):
-DCMAKE_INSTALL_PREFIX:安装目录
-DDEFAULT_CHARSET:设置字符集
-DDEFAULT_COLLATION:设置排序规则
-DENABLED_LOCAL_INFILE=ON:启用本地数据导入支持
-DWITH_SSL=system:启用ssl库支持
-DMYSQL_DATADIR:数据文件目录,可任意
-DSYSCONFDIR:配置文件目录,可任意
-DMYSQL_TCP_PORT:mysql监听的TCP端口
-DMYSQL_UNIX_ADDR:mysql.sock路径,可任意
-DWITH_BOOST:boost源码包所在目录
-DFORCE_INSOURCE_BUILD:强制建立未存在的资源目录
cmake若报错,请找到原因解决后删除CMakeCache.txt再次cmake
sudo rm -rf /usr/lib64/libstdc++.so.6* (删掉所有低版本c++的库文件)
sudo ln -s /usr/local/gcc-9.2/lib64 /usr/lib64 (把新安装的高本版c++库文件加入到系统库文件中)
sudo make (需要大概1小时) sudo make install
6.编写配置文件,建立目录并授权
sudo mkdir /app/mysql/config
sudo cd /app/mysql
sudo vi config/my.cnf
加入以下内容:
[mysqld] user=mysql port=3306 datadir=/app/mysql/data socket=/app/mysql/mysql.sock [mysqld_safe] log-error=/app/mysql/logs/mysql-err.log pid-file=/app/mysql/mysql.pid [client] socket=/app/mysql/mysql.sock
sudo useradd mysql -s /sbin/nologin
sudo mkdir -p /app/mysql/logs
sudo touch /app/mysql/logs/mysql-err.log
sudo chown -R mysql.mysql /app/mysql
7.初始化并登陆修改密码
初始化 sudo /app/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/app/mysql/data --basedir=/app/mysql (无初始密码的,若想要初始密码,使用--initialize) 启动 sudo /app/mysql/support-files/mysql.server start 登陆(若无初始密码直接回车即可) sudo /app/mysql/bin/mysql -p
修改密码
alter user 'root'@'localhost' identified by '密码';