1、下载(免编译包安装)
https://dev.mysql.com/downloads
进入
选择如下:
进入
复制此链接地址,即可拿到下载地址https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.16-el7-x86_64.tar.gz
# cd /mydata
# wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.16-el7-x86_64.tar.gz
最后出现 100% 大小为 721603282 才是完整版
2、准备、安装依赖
1、 卸载系统自带的mariadb*
# rpm -qa|grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
# rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64 删除此
或者使用yum命令删除
# yum -y remove mariadb* boost-*
2、检查mysq用户、用户组是否存在,不存在就创建
[root@localhost /]# cat /etc/group | grep mysql
[root@localhost /]# cat /etc/passwd | grep mysql
[root@localhost /]#
[root@localhost /]# useradd -M -s /sbin/nologin mysql //创建名为mysq的用户并且没有登录权限 (不指定 -g默认创建了同名mysql的用户组)
3、删除my.cnf mysql (若存在)
[root@localhost /]# rm -f /etc/my.cnf
[root@localhost /]# rpm -qa | grep mysql
3、解压安装
此版本是免编译安装包,因此和二进制编译安装包略有不同。
# 解压到 /usr/local/mysql/ (即安装目录)
# cd /mydata
# mkdir -p /usr/local/mysql
#
# tar -zxvf mysql-8.0.16-el7-x86_64.tar.gz //解压
# mv /mydata/mysql-8.0.16-el7-x86_64/* /usr/local/mysql/ //移动到安装目录
创建如下目录
# mkdir /usr/local/mysql/database (数据库目录)
# mkdir /usr/local/mysql/log (数据库日志相关目录)
# mkdir /usr/local/mysql/log/binlog (二进制日志目录)
# mkdir /usr/local/mysql/log/tmp (临时文件存放目录)
# touch /usr/local/mysql/log/mysqld.log (安装日志文档)
# chmod -R 777 /usr/local/mysql/log/
//改变mysql的属组
# chown -R mysql:mysql /usr/local/mysql/
或者下面两句
# chown -R mysql /usr/local/mysql/
# chgrp -R mysql /usr/local/mysql/
安装
# cd /usr/local/mysql
# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/database/ --log-error=/usr/local/mysql/log/mysqld.log
此版本采用mysql_install_db 进行安装,不再使用cmake 编译安装
# /usr/local/mysql/bin/mysql_install_db //安装命令
--initialize //生成默认密码
--user=mysql //指定mysql用户
--basedir=/usr/local/mysql/ //安装目录
--datadir=/usr/local/mysql/database/ // 数据库目录
--log-error=/usr/local/mysql/log/mysqld.log // 指定日志文件,安装过程中数据库生成的初始密码就会保存到此文件中,如果不指定,生成的初始密码就是现实到安装屏幕上。(建议指定)
出现:
执行上面的命令后,什么都没有出现,是因为我们指定了 /usr/local/mysql/data/mysqld.log,安装过程都记得到此文件了,因此就不在前台显示了。
# cat /usr/local/mysql/log/mysqld.log
能看到此文件内容为:
并且 /usr/local/mysql/database/ 目录内容如下:
OK 安装成功
如果报错: /usr/local/mysql/bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
是因为有个依赖包没有安装:解决办法:
# yum remove libnuma.so.1
# yum -y install numactl.x86_64
再次执行 OK
4、创建配置文件my.cnf
# vim /etc/my.cnf
# ======================== 粘贴内容 start ========================
[client]
port=3306
default-character-set=utf8
[mysqld]
port=3306
basedir=/usr/local/mysql
datadir=/usr/local/mysql/database
user=mysql
default-storage-engine=INNODB
character-set-server=utf8
[mysqld_safe]
log-error=/usr/local/mysql/data/mysqld.log
# ======================== 粘贴内容 end ========================
//设置此文件可执行权限
# chmod 644 /etc/my.cnf
5、启动并添加到环境变量
#创建驱动文件
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
#修改变量
# vim /etc/init.d/mysqld
大约46行 ,给以下两个变量赋值
basedir=/usr/local/mysql
datadir=/usr/local/mysql/database
# chmod +x /etc/init.d/mysqld
# service mysqld start //启动
出现
Starting MySQL. [ OK ]
OK 启动成功
[root@qinser /]# netstat -tlnp | grep mysqld 查看端口号
tcp6 0 0 :::3306 :::* LISTEN 17093/mysqld
# 添加到环境变量
# vim /etc/profile
在此文件最后添加如下内容:
# MYSQL
export PATH="$PATH:/usr/local/mysql/bin"
//立即生效
# source /etc/profile
# echo $PATH
6、更改默认密码
// 获取初始密码
# cat /usr/local/mysql/log/mysqld.log
大约第二行
2019-08-19T03:39:44.923970Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: r0a/I%wWUD3w <------这个就是初始密码
//修改密码
[root@qinser mydata]# mysql -u root -p
Enter password: <------这里输入上面的初始密码
mysql> alter user 'root'@'localhost' identified by 'niu123456';
mysql> flush privileges; // 使用flush privileges在插入之后刷新系统权限相关表
Query OK, 0 rows affected (0.00 sec)
OK 新密码设置成功
7、设置开启启动
方法一:(推荐)
# chkconfig --add mysqld //加入系统中,可以是使用 systemctl status mysqld.service 命令了
# chkconfig mysqld on //开机自启动
方法二:
[root@VMTest mysql]# chkconfig --level 35 mysqld on
[root@VMTest mysql]# chkconfig --list mysqld
[root@VMTest mysql]# chkconfig --add mysqld
[root@VMTest mysql]# chkconfig --list mysqld
[root@VMTest mysql]# service mysqld status
8、设置远程访问
mysql> use mysql;
mysql> select host, user, plugin from user;
+-----------+------------------+-----------------------+
| host | user | plugin |
+-----------+------------------+-----------------------+
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
| localhost | root | caching_sha2_password |
+-----------+------------------+-----------------------+
可以看出,mysql8.0以后的加密方式是caching_sha2_password, 而我们的客户端工具使用的多大是mysql_native_password加密,因此首先需要对root的密码进行加密置换。
1、更换加密规则,重新设置密码
mysql> alter user 'root'@'localhost' identified with mysql_native_password by 'niu123456';
Query OK, 0 rows affected (0.01 sec)
2、更新root的host为%,% 表示通配所有 host,可以访问远程
mysql> update user set host='%' where user='root';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
3、授权
mysql> grant all privileges on *.* to 'root'@'%' with grant option;
mysql> flush privileges;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> select host, user, authentication_string, plugin from user;
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| host | user | authentication_string | plugin |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| % | root | *8A65E36585F4829F35AFBB9C494F9337539DECFA | mysql_native_password |
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
4 rows in set (0.00 sec)
OK 可以远程连接了
Mysql8.0.16 安装全部完成