CentOs7.9安装mysql8教程

环境说明

  • 操作系统 :CentOS 7.9 64位
  • mysql版本:8.0.13

步骤

1、查看centos7中是否有MariaDBMariaDBMySQL关系请自行查阅

rpm -qa | grep mariadb

CentOs7.9安装mysql8教程_第1张图片

2、如果有MariaDB,需要将 步骤1 中查询到的mairadb全部卸载,否则MySQL安装会出现问题

rpm -e --nodeps 软件名

在这里插入图片描述

 3、查看本机是否已经安装过MySQL

rpm -qa | grep -i mysql

CentOs7.9安装mysql8教程_第2张图片

4、如果安装过MySQL,请确定是否有数据需要备份(如果有需要备份的数据请自行查阅相关操作进行备份,否则数据丢失概不负责)

  如果没有安装过MySQL,请跳转到安装步骤

5、卸载MySQL

将步骤3中查询到的mysql安装包全部卸载

rpm -e --nodeps 软件名

查找mysql文件,并将其删除

find / -name mysql
whereis mysql

  将find命令和whereis命令查询到的mysql文件全部删除

rm -rf 查询到的mysql路径

  删除mysql配置文件

rm /etc/my.cnf

确认是否全部删除mysql

rpm -qa | grep -i mysql

安装 

方法一:

1、从网易镜像下载mysql安装包

wget https://mirrors.163.com/mysql/Downloads/MySQL-8.0/mysql-8.0.26-1.el7.x86_64.rpm-bundle.tar

方法二:

1、链接地址:MySQL :: Download MySQL Community Server

查询服务器是x86_64架构还是arm架构

uname -m
或者是
arch
# 我的服务器是x86_64的

 CentOs7.9安装mysql8教程_第3张图片

CentOs7.9安装mysql8教程_第4张图片

点击 Download,如下图:

CentOs7.9安装mysql8教程_第5张图片

 没有登陆,点击 “No thanks,just start my downlaod.” 即可下载

将下载的mysql-8.0.26-1.el7.x86_64.rpm-bundle.tar上传到服务器

2、解压mysql安装包

tar -xvf mysql-8.0.26-1.el7.x86_64.rpm-bundle.tar

3、按顺序安装

必须安装(注意顺序)

rpm -ivh mysql-community-common-8.0.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.26-1.el7.x86_64.rpm

非必要安装(注意顺序)

rpm -ivh mysql-community-libs-compat-8.0.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-embedded-compat-8.0.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-devel-8.0.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-test-8.0.26-1.el7.x86_64.rpm

如何报以下错误:

1、出现问题:
[root@VM-0-14-centos mysql]# rpm -ivh mysql-community-embedded-compat-8.0.26-1.el7.x86_64.rpm
warning: mysql-community-embedded-compat-8.0.26-1.el7.x86_64.rpm: Header V3 DSA/SHA256 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
libnuma.so.1()(64bit) is needed by mysql-community-embedded-compat-8.0.26-1.el7.x86_64
libnuma.so.1(libnuma_1.1)(64bit) is needed by mysql-community-embedded-compat-8.0.26-1.el7.x86_64
libnuma.so.1(libnuma_1.2)(64bit) is needed by mysql-community-embedded-compat-8.0.26-1.el7.x86_64

解决办法:

yum install numactl

2、出现问题:
error:
Failed dependencies:
pkgconfig(openssl) is needed by mysql-community-devel-8.0.26-1.el7.x86_64

解决办法:

yum install openssl-devel.x86_64 openssl.x86_64 -y

3、出现问题:
[root@VM-0-14-centos mysql]# rpm -ivh mysql-community-test-8.0.26-1.el7.x86_64.rpm
warning: mysql-community-test-8.0.26-1.el7.x86_64.rpm: Header V3 DSA/SHA256 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
perl(Data::Dumper) is needed by mysql-community-test-8.0.26-1.el7.x86_64

解决办法:

yum -y install autoconf


4、出现问题:
[root@VM-0-14-centos mysql]# rpm -ivh mysql-community-test-8.0.26-1.el7.x86_64.rpm
warning: mysql-community-test-8.0.26-1.el7.x86_64.rpm: Header V3 DSA/SHA256 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
perl(JSON) is needed by mysql-community-test-8.0.26-1.el7.x86_64

解决办法:

yum install perl.x86_64 perl-devel.x86_64 -y
yum install perl-JSON.noarch -y

5、出现问题:
[root@VM-0-14-centos mysql]# rpm -ivh mysql-community-test-8.0.26-1.el7.x86_64.rpm
warning: mysql-community-test-8.0.26-1.el7.x86_64.rpm: Header V3 DSA/SHA256 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
perl(Test::More) is needed by mysql-community-test-8.0.26-1.el7.x86_64

解决办法:

yum install perl-Test-Simple

6、出现问题:

[root@VM-0-14-centos mysql]# rpm -ivh mysql-community-devel-8.0.26-1.el7.x86_64.rpm
warning: mysql-community-devel-8.0.26-1.el7.x86_64.rpm: Header V3 DSA/SHA256 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
pkgconfig(openssl) is needed by mysql-community-devel-8.0.26-1.el7.x86_64

解决办法:

yum install openssl-devel

4.初始化数据库

mysqld --initialize --console

5.目录授权,否则启动失败

chown -R mysql:mysql /var/lib/mysql/

6.启动msyql服务

systemctl start mysqld

备注:

  对应的重启、关闭mysql命令如下

  systemctl stop mysqld

  systemctl restart mysqld

 查询mysql状态

 systemctl status mysqld7.查看msyql服务的状态

CentOs7.9安装mysql8教程_第6张图片
 7.在/var/log/mysqld.log下查看临时密码

cat /var/log/mysqld.log | grep password

用临时密码登录到数据库

mysql -u root -p + 回车键

输入临时密码(输入时不会显示出来,输入完直接回车)


8.执行下列mysql命令 修改密码
 

alter USER 'root'@'localhost' IDENTIFIED BY '新密码(必须包含:数字大小写字母特殊字符)';

 依次执行下列mysql命令 授权远程连接

查看所有用户是否可以远程连接,依次执行下列命令:

show databases;
use mysql;
select host, user, authentication_string, plugin from user;

host为localhsot,说明用户只能本地连接msyql服务

  • 修改root用户host值,使root用户可以远程登录
    update user set host = "%" where user='root';

  • 刷新
    flush privileges;

9.使用mysql客户端登录数据库报错如下图

 

解决办法:

mysql -u root -p+ 回车键+输入root用户的密码 登录 ;
show databases;
use mysql;
alter USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root用户的密码';
flush privileges;

备注:
mysql8 之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是caching_sha2_password。
解决问题方法有两种:
方法1.升级navicat驱动;
方法2.把mysql用户登录密码加密规则还原成mysql_native_password.。
此处我使用的是方法2。

10.创建用户和授权

用户创建:

create user 'quanran'@'%' identified by 'QuanRan123:';

授权:

grant all privileges on *.* to 'quanran'@'%' with grant option;


查看用户权限:

select host, user, authentication_string, plugin from user;


备注:
1.创建用户

命令:
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
说明:
username: 你将创建的用户名
host: 指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost,如果想让该用户可以从任意远程主机登陆,可以使用通配符%
password: 该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器
例子:
CREATE USER 'dog'@'localhost' IDENTIFIED BY '123456';
CREATE USER 'quanran'@'192.168.1.101_' IDENDIFIED BY '123456';
CREATE USER 'quanran'@'%' IDENTIFIED BY '123456';
CREATE USER 'quanran'@'%' IDENTIFIED BY '';
CREATE USER 'quanran'@'%';
2.授权

命令:
GRANT privileges ON databasename.tablename TO 'username'@'host'
说明:
privileges:用户的操作权限,如SELECT,INSERT,UPDATE等,如果要授予所的权限则使用ALL
databasename:数据库名
tablename:表名,如果要授予该用户对所有数据库和表的相应操作权限则可用表示,如.*
例子:
GRANT SELECT, INSERT ON test.user TO 'quanran'@'%';
GRANT ALL ON *.* TO 'quanran'@'%';
GRANT ALL ON maindataplus.* TO 'quanran'@'%';
注意:
用以上命令授权的用户不能给其它用户授权,如果想让该用户可以授权,用以下命令:
GRANT privileges ON databasename.tablename TO 'username'@'host' WITH GRANT OPTION;
3.设置与更改用户密码

命令:
SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword');
如果是当前登陆用户用:
SET PASSWORD = PASSWORD("newpassword");
例子:
SET PASSWORD FOR 'quanran'@'%' = PASSWORD("123456");
4.撤销用户权限

命令:
REVOKE privilege ON databasename.tablename FROM 'username'@'host';
说明:
privilege, databasename, tablename:同授权部分
例子:
REVOKE SELECT ON *.* FROM 'quanran'@'%';
注意:
1.如果用 GRANT SELECT ON test.user TO 'quanran'@'%' 给用户’quanran’@’%‘授权,则使用 REVOKE SELECT ON . FROM 'quanran'@'%'; 并不能撤销该用户对test数据库中user表的SELECT 操作。
2.如果用 GRANT SELECT ON . TO 'quanran'@'%'; 给’quanran’@’%'授权,则使用 REVOKE SELECT ON test.user FROM 'quanran'@'%'; 命令也不能撤销该用户对test数据库中user表的Select权限。
3.具体信息可以用命令SHOW GRANTS FOR 'quanran'@'%'; 查看。
5.删除用户

命令:DROP USER 'username'@'host'

你可能感兴趣的:(数据库,centos,python,linux)