一 前期准备
1.1 下载MySql5.7安装包
地址:MySQL :: Download MySQL Community Server
1.2 卸载 mariadb
Centos7 自带 mariadb ,在安装可能会有冲突,最好卸载掉 mariadb。
查看 mariadb
rpm -qa | grep mariadb
卸载 mariadb
rpm -e mariadb-libs-5.5.60-1.el7_5.x86_64
在命令最后添加参数说明:
--force强制安装
--nodeps就是安装时不检查依赖关系
二 安装MySql单机版
2.1 上传、解压
在/usr/local目录下创建mysql目录,将下载的安装包上传到该目录下
2.2 安装
rpm -ivh mysql-community-common-5.7.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.34-1.el7.x86_64.rpm
2.3 初始化和配置
mysqld --initialize
chown mysql:mysql /var/lib/mysql -R
# 启动
systemctl start mysqld.service
# 设置开机自起
systemctl enable mysqld
2.4 查询数据库默认密码
cat /var/log/mysqld.log | grep password
2.5 登录
# 密码是上一步输出的密码
mysql -uroot -pOS/imnkjk
2.6 修改密码
alter user 'root'@'localhost' identified with mysql_native_password by 'root';
2.7 远程访问授权
# 远程访问设置
create user 'root'@'%' identified with mysql_native_password by 'root';
grant all privileges on *.* to 'root'@'%' with grant option;
# 使设置成效
flush privileges;
2.8 修改my.cnf配置文件
一般基础配置如下:
# 端口号
port=3306
# 数据存放目录
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# 数据库字符集
character-set-server = utf8
# 数据库字符集对应一些排序等规则,注意要和character-set-server对应
collation-server = utf8_general_ci
# 是否对sql语句大小写敏感,1表示不敏感
lower_case_table_names = 1
# 最大连接数
max_connections = 400
2.9 常用命令
# 启动
systemctl start mysqld.service
# 停止
systemctl stop mysqld.service
# 查询状态
systemctl status mysqld.service
# 重启
service mysqld restart
2.10 开放端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --zone=public --remove-port=3306/tcp --permanent
firewall-cmd --reload
2.11 防火墙设置
# 查看状态
systemctl status firewalld
# 开启
systemctl start firewalld
# 关闭
systemctl stop firewalld