yum install mysql


一、安装 MySQL 的 yum 源

https://dev.mysql.com/downloads/repo/yum/

# 安装源,会在 /etc/yum.repos.d/ 下生成 mysql-community.repo 文件
rpm -ivh https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm

# 查看 yum 源中是否有 mysql 安装包
yum repolist all | grep mysql

mysql-community.repo

# Enable to use MySQL 5.5
[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/7/$basearch/
enabled=1
gpgcheck=1
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

[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-tools-community]
name=MySQL Tools Community
baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-tools-preview]
name=MySQL Tools Preview
baseurl=http://repo.mysql.com/yum/mysql-tools-preview/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-cluster-7.5-community]
name=MySQL Cluster 7.5 Community
baseurl=http://repo.mysql.com/yum/mysql-cluster-7.5-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-cluster-7.6-community]
name=MySQL Cluster 7.6 Community
baseurl=http://repo.mysql.com/yum/mysql-cluster-7.6-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-cluster-8.0-community]
name=MySQL Cluster 8.0 Community
baseurl=http://repo.mysql.com/yum/mysql-cluster-8.0-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
View Code

mysql-community-source.repo

[mysql55-community-source]
name=MySQL 5.5 Community Server - Source
baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/SRPMS
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql56-community-source]
name=MySQL 5.6 Community Server - Source
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/SRPMS
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql57-community-source]
name=MySQL 5.7 Community Server - Source
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/SRPMS
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql80-community-source]
name=MySQL 8.0 Community Server - Source
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/SRPMS
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-connectors-community-source]
name=MySQL Connectors Community - Source
baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/SRPMS
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-tools-community-source]
name=MySQL Tools Community - Source
baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/SRPMS
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-tools-preview-source]
name=MySQL Tools Preview - Source
baseurl=http://repo.mysql.com/yum/mysql-tools-preview/el/7/SRPMS
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-cluster-7.5-community-source]
name=MySQL Cluster 7.5 Community - Source
baseurl=http://repo.mysql.com/yum/mysql-cluster-7.5-community/el/7/SRPMS
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-cluster-7.6-community-source]
name=MySQL Cluster 7.6 Community - Source
baseurl=http://repo.mysql.com/yum/mysql-cluster-7.6-community/el/7/SRPMS
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-cluster-8.0-community-source]
name=MySQL Cluster 8.0 Community - Source
baseurl=http://repo.mysql.com/yum/mysql-cluster-8.0-community/el/7/SRPMS
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
View Code

 

二、选择 MySQL 的版本并安装

# 安装管理工具
yum install -y yum-utils

# 禁用 mysql 8.0
yum-config-manager --disable mysql80-community
# 启用 mysql 5.7
yum-config-manager --enable mysql57-community

# 安装
yum install -y mysql-community-server

 

三、相关设置与启动

启动服务

# 开启服务
systemctl start mysqld.service

# 开机自启动
systemctl enable mysqld.service

# 检查MySQL服务器的状态
systemctl status mysqld.service

# 查看端口
netstat -ln | grep 3306

# 从日志文件中获取 root 账户初始密码
grep "password" /var/log/mysqld.log

初始化服务

# 直接用该密码登陆会要求改密码,最好先进行向导操作

# 安全向导
mysql_secure_installation

# 输入 root 初始密码,初始密码中有特殊字符时需转义
Enter password for user root:
# 设置新密码,密码太简单会提示错误:Your password does not satisfy the current policy requirements
New password
# 是否确认修改root密码
Change the password for root
# 是否删除匿名用户
Remove anonymous users
# 是否禁止root远程登录
Disallow root login remotely
# 是否删除test数据库
Remove test database and access to it
# 是否现在刷新权限
Reload privilege tables now

设置简单密码

官方 在新版本中对密码设置要求比较严格

-- 查看密码策略
SHOW VARIABLES LIKE 'validate_password%';

-- 修改密码策略,修改密码检查强度和密码长度就可以
-- 8.0 之前
set global validate_password_policy=0;
set global validate_password_length=4;
-- 8.0 之后
set global validate_password.policy=0;
set global validate_password.length=4;

-- 修改当前用户密码
ALTER USER USER() IDENTIFIED BY '123456';
-- 修改指定用户密码
ALTER user 'root'@'localhost' IDENTIFIED BY '123456';

忘记 root 密码情况下设置密码

# 编辑配置文件
vim /etc/my.cnf
# 文件最后添加免密登陆
skip-grant-tables

# 重启 mysql 服务
systemctl restart mysqld.service
# 登陆 mysql,不用密码
mysql -uroot

清空密码

-- 清空指定账户密码
use mysql;
update user set authentication_string='' where user='root';

-- 退出 mysql,删除免密登陆配置,重启 mysql 服务,用空密码登陆,然后进行修改密码操作

添加远程访问权限

首先开放本机 3306 端口或关闭防火墙

# iptables
# 开放端口
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
# 保存配置
service iptables save

# firewalld
# 开放端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
# 重启 firewall
firewall-cmd --reload
# 查看已开放端口
firewall-cmd --list-ports

# 关闭防火墙
systemctl stop firewalld.service
# 关闭开机自启
systemctl disable firewalld.service

为已有用户添加远程访问权限

-- 选择mysql数据库
use mysql;

-- 修改指定用户登录位置,%为任意地址,可配置指定IP
update user set host='%' where user='root';

-- 修改登陆密码和加密规则,8 版本默认的认证插件为Caching_sha2_password,原来是mysql_native_password,不修改 Navicat 等工具连接会认证失败
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';

-- 刷新权限
flush privileges;

创建新用户并添加远程访问权限

-- 创建用户(user1为用户名,%为登陆地址任意ip,也可指定,123456为登录密码)
CREATE USER 'user1'@'%' IDENTIFIED WITH mysql_native_password BY '123456';

-- 默认创建的用户是无权限,只能登录而已,(all:所有权限,有select,update等等权限,后面的*.*:指定数据库.指定表,这里指所有,to后面是用户)
grant all on *.* to 'user1'@'%';

-- 刷新权限
flush privileges;

查看登陆权限

use mysql;
select host,user from user;

 

四、配置(/etc/my.cnf)

修改 datadir 路径到其它磁盘后可能会出现服务启动失败的情况,这时关闭 selinux 即可。

[mysqld]
# 关闭密码验证,可设置简单密码
validate_password=off

# 存储引擎
default-storage-engine=INNODB

# 编码
character-set-server=utf8

# 排序规则
collation-server=utf8_general_ci

# 免密登陆
skip-grant-tables

 

五、常用命令

-- 查看版本
select version();

-- 设置编码
SET NAMES utf8;

-- 查看编码
show variables like '%character%';

-- 查看排序规则
show variables like 'collation%';

-- 查看储存引擎,Support列,YES表示支持,DEFAULT表示默认,NO表示不支持
show engines;
show variables like '%storage_engine%';

-- 设置储存引擎
SET default_storage_engine=InnoDB;

-- 查看日志文件位置
select @@log_error;

-- 查看数据文件位置
select @@datadir;

 


https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/

https://dev.mysql.com/doc/


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM