1. 添加MySQL Yum存储库
首先,将MySQL Yum存储库添加到系统的存储库列表中。跟着这些步骤:
- 1.转到https://dev.mysql.com/downloads/repo/yum/上的MySQL Yum存储库的下载页面 。
- 2.选择并下载适用于您平台的发行包。
- 3.使用以下命令安装下载的发行包,并替换 platform-and-version-specific-package-name 为下载的包的名称:
# 安装wget组件 shell> yum -y install wget # 下载yum库 el7是操作系统版本=rhel7 shell> wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm # 安装yum库 shell> sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
2. 选择版本
您还可以通过编辑/etc/yum.repos.d/mysql-community.repo
文件选择系列 。
enable=1即为启用,enable=0即为禁用,就是选择安装哪个版本的mysql
这是文件中发行系列子存储库的典型条目:
[mysql80-community] name=MySQL 8.0 Community Server baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/6/$basearch/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
# Enable to use MySQL 5.7 [mysql57-community] name=MySQL 5.7 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
您应该随时只为一个发行系列启用子存储库。如果启用了多个发行系列的子存储库,那么Yum将使用最新的系列。
通过运行以下命令并检查其输出来验证是否已启用和禁用正确的子存储库(对于启用了dnf的系统,请使用dnf替换 命令中的 yum):
yum repolist enabled | grep mysql
3. 安装MySQL
通过以下命令安装MySQL:
sudo yum install mysql-community-server
此步会报错,修改方案:将 /etc/yum.repos.d/mysql-community.repo
文件做如下修改
gpgcheck=1
改为 gpgcheck=0
忽略检查
[mysql55-community] name=MySQL 5.5 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql # Enable to use MySQL 5.6 [mysql56-community] name=MySQL 5.6 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql # Enable to use MySQL 5.7 [mysql57-community] name=MySQL 5.7 Community Server # baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/ baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/ enabled=1 # gpgcheck=1 忽略检查 gpgcheck=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql80-community] name=MySQL 8.0 Community Server baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
再次安装即可。
4. 启动服务
# 搜索mysql服务 systemctl list-unit-files | grep mysql # 启动服务 systemctl start mysqld
5. 密码设置
# 从mysql日志文件中获取,mysql5.7在安装时默认指定的临时密码,然后既可以登录mysql grep 'temporary password' /var/log/mysqld.log
# 修改密码,必须包含 大写字母 小写字母 符号 数字 且长度不能小于8位 ALTER USER 'root'@'localhost' IDENTIFIED BY 'ZHJzhj9!';
6. 远程权限
2中方式均可
# 允许在任意远程主机(%) 用root登录mysql,允许使用所有库的所有表(*.*) use mysql; GRANT ALL ON *.* TO root@'%' IDENTIFIED BY 'ZHJzhj9!' WITH GRANT OPTION; FLUSH PRIVILEGES;
# 直接改mysql的用户表也可以 use mysql; update user set host = '%' where user = 'root'; (使mysql root用户可以连接上任意的ip地址) # 刷新权限 FLUSH PRIVILEGES;
7. Mysql乱码
create database db_1909 default charset = utf8;
create table t_user(id int primary key auto_increment,name varchar(20))default charset=utf8;
jdbc:mysql://localhost:3306/db_fruit?useUnicode=true&characterEncoding=utf8&useSSL=false
8. Root密码找回
在 /etc/my.cnf下的
[mysqld]
中添加:skip-grant-tables
[mysqld]
skip-grant-tables
# 重启mysql [root@zhj ~]# systemctl restart mysql # 直接登录 [root@zhj ~]# mysql -u root # 修改密码为 "ZHJzhj99!" mysql> use mysql mysql> update user set authentication_string=password('ZHJzhj99!') where user='root'; mysql> exit
删除/etc/my.cnf下的 [mysqld]中:的 skip-grant-tables
# 重启mysql [root@zhj ~]# systemctl restart mysql
9.本地客户端数据乱码(了解)
/etc/my.cnf 中添加如下配置,即可 [client] default-character-set=utf8
10.导出数据
# 导出:将db1911_shiro库和所有数据 导出到一个 sql文件中 C:\Users\zhj> mysqldump -uroot -p111111 --databases db1911_shiro > d:/shiro.sql
# 导入: mysql> source d:/shiro.sql