一、清除以前安装的MySQL或者系统自带的MySQL
1.查看是否已安装MySQL
rpm -qa|grep mysql
2.若存在则卸载掉
rpm -e --nodeps mysql-libs
二、安装Mysql
1.使用wget命令下载到本地
sudo wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
2.安装
sudo rpm -ivh mysql80-community-release-el7-1.noarch.rpm
sudo yum install mysql-server
遇到需要确定的地方,直接 y 确定就行。
3.检查是否安装成功
mysqladmin -V //大写V
4.开启Mysql
sudo service mysqld start
5.查看状态
sudo service mysqld status
6.登录
1)MySQL的初始值不再是空了, 首次登录需要去获取MySQL给的初始值(在my.cnf文件中设置的日志文件位置)
sudo cat /var/log/mysqld.log
2)获取密码后就可以登录
sudo mysql -uroot -p
3)修改密码
set global validate_password.policy = 0;
set global validate_password.length = 1;
alter user user() identified by '新密码';
7.远程登录
1)使用Ctrl+Z退出MySQL
Ctrl+Z
2)修改配置i文件my.cnf
sudo vi /etc/my.cnf
1:去掉default-authentication-plugin=mysql_native_password前面的注释
2:添加一行port=3306 (不确定mysqld.log看到的33060端口是什么)
注:修改完my.cnf 文件,重新启动MySQL ,所有数据,配置好像都会重置(目前我操作两次都重置了)(特别注意数据会丢失),所有配置又要从第六步 登录 开始配置。所以,如果,在安装完MySQL,而且已使用且存在数据。那么在修改my.cnf文件之前,要先备份数据。
我是在配置完成,navicat连接也正常后,启动项目时,提示有表不存在。实际数据库中存在此表,只不过数据库中是小写表名,代码中是大写表名。
后来在my.cnf文件中添加了 lower_case_table_names=1 重新启动mysql后,所有数据都没了,类似于初始化。所以,如果,代码中不需要区分大小写,可以在此处设置远程连接时,一起配置。
3)修改mysql数据库下的user表中host的值
mysql -u root -p use mysql;
alter user 'root'@'localhost' identified with mysql_native_password by 'xxxxxx';
flush privileges;
update user set host='%' where user='root';
flush privileges;
4)关闭防火墙
CentOS7默认使用的是firewall作为防火墙
步骤一:关闭firewall防火墙
sudo systemctl stop firewalld.service
sudo systemctl disable firewalld.service
sudo systemctl mask firewalld.service
步骤二:安装iptables防火墙
sudo yum install iptables-services -y
sudo systemctl enable iptables
sudo systemctl start iptables
sudo vim /etc/sysconfig/iptables
加入一行
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
重启防火墙
sudo systemctl enable iptables.service
sudo systemctl start iptables.service
注:实际操作过程中,我没有按此步骤操作,我在防火墙配置文件里直接添加的开放端口参数
步骤三:将mysql 服务加入开机启动项,并启动mysql进程
sudo systemctl enable mysqld.service
sudo systemctl start mysqld.service
步骤四:使用Navicat远程连接MySQL
重启mysql服务
sudo systemctl restart mysqld.service
查看端口
sudo netstat -apn|grep 3306
Navicat 链接