1、centos7安装mysql8
- mysql官网
https://dev.mysql.com/downloads/mysql/
- 示例2个版本的下载地址
#5.7.30下载地址
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.30-1.el7.x86_64.rpm-bundle.tar
#8.0.22下载地址
wget https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar
1、卸载mariadb
mariadb 这个是 Linux 系统自带的数据库系统,跟 MySQL 冲突,所以一般先卸载这个没用的数据库
[root@k8s-master2 ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@k8s-master2 ~]# rpm -ev --nodeps mariadb-libs-5.5.68-1.el7.x86_64
软件包准备中...
mariadb-libs-1:5.5.68-1.el7.x86_64
[root@k8s-master2 ~]# rpm -qa | grep mariadb
[root@k8s-master2 ~]#
2、创建目录并上传
#创建目录
mkdir -vp /usr/local/software/mysql
#进入目录
cd /usr/local/software/mysql
#下载文件
wget https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar
#解压缩
tar -xvf mysql-8.0.31-1.el7.x86_64.rpm-bundle.tar
#得到以下文件
mysql-community-client-8.0.31-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.31-1.el7.x86_64.rpm
mysql-community-common-8.0.31-1.el7.x86_64.rpm
mysql-community-debuginfo-8.0.31-1.el7.x86_64.rpm
mysql-community-devel-8.0.31-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.31-1.el7.x86_64.rpm
mysql-community-icu-data-files-8.0.31-1.el7.x86_64.rpm
mysql-community-libs-8.0.31-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.31-1.el7.x86_64.rpm
mysql-community-server-8.0.31-1.el7.x86_64.rpm
mysql-community-server-debug-8.0.31-1.el7.x86_64.rpm
mysql-community-test-8.0.31-1.el7.x86_64.rpm
3、正式安装
注意:需要按照以下安装顺序进行安装,否则会出问题
安装顺序:COMMON --> LIB --> LIB-COMPAT --> CLIENT --> SERVER
依次执行以下命令,提示100%字样表示安装成功
rpm -ivh mysql-community-common-8.0.31-1.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-libs-8.0.31-1.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-libs-compat-8.0.31-1.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-client-8.0.31-1.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-server-8.0.31-1.el7.x86_64.rpm --nodeps --force
#查看安装目录
[root@k8s-master1 mysql]# rpm -qa | grep mysql
mysql-community-libs-8.0.31-1.el7.x86_64
mysql-community-client-8.0.31-1.el7.x86_64
mysql-community-common-8.0.31-1.el7.x86_64
mysql-community-libs-compat-8.0.31-1.el7.x86_64
mysql-community-server-8.0.31-1.el7.x86_64
- 具体的安装过程
[root@k8s-master1 mysql]# rpm -ivh mysql-community-common-8.0.31-1.el7.x86_64.rpm --nodeps --force
警告:mysql-community-common-8.0.31-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-common-8.0.31-1.e################################# [100%]
[root@k8s-master1 mysql]# rpm -ivh mysql-community-libs-8.0.31-1.el7.x86_64.rpm --nodeps --force
警告:mysql-community-libs-8.0.31-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-libs-8.0.31-1.el7################################# [100%]
[root@k8s-master1 mysql]# rpm -ivh mysql-community-libs-compat-8.0.31-1.el7.x86_64.rpm --nodeps --force
警告:mysql-community-libs-compat-8.0.31-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-libs-compat-8.0.3################################# [100%]
[root@k8s-master1 mysql]# rpm -ivh mysql-community-client-8.0.31-1.el7.x86_64.rpm --nodeps --force
警告:mysql-community-client-8.0.31-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-client-8.0.31-1.e################################# [100%]
[root@k8s-master1 mysql]# rpm -ivh mysql-community-server-8.0.31-1.el7.x86_64.rpm --nodeps --force
警告:mysql-community-server-8.0.31-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-server-8.0.31-1.e################################# [100%]
[root@k8s-master1 mysql]# rpm -qa | grep mysql
mysql-community-libs-8.0.31-1.el7.x86_64
mysql-community-client-8.0.31-1.el7.x86_64
mysql-community-common-8.0.31-1.el7.x86_64
mysql-community-libs-compat-8.0.31-1.el7.x86_64
mysql-community-server-8.0.31-1.el7.x86_64
[root@k8s-master1 mysql]#
4、初始化mysql
mysqld --initialize;
chown mysql:mysql /var/lib/mysql -R;
systemctl start mysqld.service;
systemctl enable mysqld;
注意:此处会生成mysql的默认文件/etc/my.cnf
5、设置密码
#查看数据库默认随记密码
cat /var/log/mysqld.log | grep password
#此句也能查看
#grep 'temporary password' /var/log/mysqld.log
#使用随记密码登录 MySQL
mysql -uroot -p
#修改 MySQL 密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourPassword';
6、授权root远程登录
如果root用户仅在 localhost主机下去授权用户在别的主机上的权限也是不可以的,比如 ‘root’@'localhost’ 去授权别的主机用户 ‘username’@‘otherhost’ 也会报错,所以修改root用户的 host 为可以访问所有主机(打造一个真正的超级管理员),然后更新权限为拥有所有的权限,这里是必须要更新的,要不然还会报错,(对于为何要再次更新权限,我是这样理解的,只要更改了 user,host,或者authentication_string中的任何一个就要刷新权限)(对于为何要重新给 修改后的root用户重新赋予权限)因为没有刷新之前的记录的话新的权限没有覆盖
#选择数据库
use mysql;
#更新访问主机为所有主机
#如果Host=%,表示所有IP都有连接权限。
#Host=localhost,表示只能通过本机客户端去访问
#Host=192.168.1.1。这里的意思就是说root用户只能通过192.168.1.1的客户端去访问
update user set host='%' where user='root';
#8.0之后的mysql不支持 授权的时候就进行用户创建,所以创建 之后才能授权;
#允许远程登录
grant all privileges on *.* to 'root'@'%';
#刷新权限
flush privileges;
#修改加密规则,其中yourpassword是你原来的密码,就是上面查询的默认密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'yourpassword' PASSWORD EXPIRE NEVER;
- 添加用户、允许远程访问
#创建一个名为neruser的用户,设置其密码为password 并允许该用户从任何主机连接mysql
CREATE USER 'newuser'@'%' IDENTIFIED BY 'password';
#这将授予"newuser"用户对"test"数据库的所有权限
GRANT ALL PRIVILEGES ON test.* TO 'newuser'@'%';
#刷新权限
FLUSH PRIVILEGES;
#删除用户
#DROP USER 'newuser'@'%';
7、开放端口
#开放端口进行远程链接
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
#查看开放端口
firewall-cmd --list-ports
8、mysql: error while loading shared libraries: libncurses .so.5:cannot open shared obiectfile :No such file or directory
yum install libncurses* -y