linux centos7安装mysql8

时间:2022-05-20 02:01:42

一、RPM版安装

查看是否有其他版本的数据库,若有,删除干净

非root用户必须要有sudo权限

1.下载mysql相关安装包

https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql80-community-el7/mysql-community-server-8.0.18-1.el7.x86_64.rpm

https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql80-community-el7/mysql-community-libs-8.0.18-1.el7.x86_64.rpm

https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql80-community-el7/mysql-community-common-8.0.18-1.el7.x86_64.rpm

https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql80-community-el7/mysql-community-client-8.0.18-1.el7.x86_64.rpm

2.其他依赖(可以先跳过该步,看后面安装mysql时缺少什么依赖就安装什么依赖)

到该网址下去寻找gcc、gcc-c  、openssl、perl及其依赖包
https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/Packages/

 

3.安装mysql(未报错则表示安装成功)

linux centos7安装mysql8

4.自定义配置mysql(如果不想改变mysql的默认相关目录,跳至第6步)

修改Mysql默认的配置文件内容

sudo vi /etc/my.cnf
删除所有内容,加入以下内容:
[mysqld] user
=mysql port=3306 datadir=/app/mysql/data socket=/app/mysql/mysql.sock log-error=/app/mysql/log/mysqld.log pid-file=/app/mysql/mysqld.pid [client] socket=/app/mysql/mysql.sock

5.创建配置文件中的相关目录并修改权限

sudo mkdir /app/mysql/data /app/mysql/log -p

sudo chown mysql:mysql /app/mysql -R

6.初始化mysql

linux centos7安装mysql8

7.启动mysql

若启动出错:

a.请查看mysql相关目录的所属用户和所属组是否是mysql

b.请查看selinux是否关闭:执行sudo getenforce得到的结果若不是Permissive,执行命令:sudo setenforce 0

c.查看端口是否被占用

d.若还是无法启动:请查看mysql错误日志以及sudo systemctl status mysqld或者journalctl -xe

linux centos7安装mysql8

8.登录mysql

查看Mysql的初始密码([email protected]:后面的就是初始密码)
sudo cat /app/mysql/log |grep [email protected] (做过第4步使用该命令)
sudo cat /var/log/mysqld.log|grep [email protected] (未做过第4步使用该命令)

登录mysql,复制上面的密码
mysql -p

9.修改mysql密码(初次登录必须修改密码,否则无法使用mysql命令)

 

alter user root@localhost identified by 你的密码;

 

 

二、源码版安装

非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版本)

正确安装后可以看到如下结果

linux centos7安装mysql8

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 ‘密码‘;