一、linux centos7 安装MySQL
1、下载mysql源安装包
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
2、安装mysql源,若结尾出现complete!,则说明MySQL源安装完成
yum localinstall mysql57-community-release-el7-8.noarch.rpm
3、检测是否安装完成
yum repolist enabled | grep "mysql.*-community.*"
效果如下:
mysql-connectors-community/x86_64 MySQL Connectors Community 221 mysql-tools-community/x86_64 MySQL Tools Community 135 mysql57-community/x86_64 MySQL 5.7 Community Server 544
4、安装
yum install mysql-community-server
5、设置开机启动mysql服务
systemctl enable mysqld
systemctl daemon-reload
6、查看安装的mysql版本
rpm -aq | grep -i mysql
7、启动MySQL服务
systemctl start mysqld.service
重启MySQL
systemctl restart mysqld.service
停止mysql
systemctl stop mysqld.service
8、查看mysql启动状态
systemctl status mysqld.service
效果如下:
● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: active (running) since Sat 2021-11-20 11:50:37 CST; 6min ago
9、查看MySQL初始密码,这个密码是随机生成的
grep 'A temporary password' /var/log/mysqld.log
密码没有看到,然后直接到/var/log/mysqld.log查看mysqld.log文件发现文件是一片空白。
原因:是因为你原本的虚拟机上装有mysql,在卸载MySQL时还有残留的数据
二、卸载已经安装的mysql
1、查找已安装的myslq 版本
rpm -qa|grep mysql
如下所示
mysql-community-common-5.7.36-1.el7.x86_64 mysql57-community-release-el7-8.noarch mysql-community-libs-5.7.36-1.el7.x86_64 mysql-community-libs-compat-5.7.36-1.el7.x86_64 mysql-community-server-5.7.36-1.el7.x86_64 mysql-community-client-5.7.36-1.el7.x86_64
2、删除上面的文件夹
rpm -ev mysql-community-common-5.7.36-1.el7.x86_64 --nodeps rpm -ev mysql57-community-release-el7-8.noarch --nodeps rpm -ev mysql-community-libs-5.7.36-1.el7.x86_64 --nodeps rpm -ev mysql-community-libs-compat-5.7.36-1.el7.x86_64 --nodeps rpm -ev mysql-community-server-5.7.36-1.el7.x86_64 --nodeps rpm -ev mysql-community-client-5.7.36-1.el7.x86_64 --nodeps
3、查找之前老版本mysql的目录
find / -name mysql
效果如下:
/run/lock/subsys/mysql /usr/lib64/mysql /usr/share/mysql /usr/local/mysql /usr/local/mysql/include/mysql /usr/local/mysql/bin/mysql /usr/local/mysql/data/mysql /etc/selinux/targeted/active/modules/100/mysql /var/spool/mail/mysql
4、删除老版本mysql的文件和库
rm -rf /run/lock/subsys/mysql rm -rf /usr/lib64/mysql rm -rf /usr/share/mysql rm -rf /usr/local/mysql rm -rf /usr/local/mysql/include/mysql rm -rf /usr/local/mysql/bin/mysql rm -rf /usr/local/mysql/data/mysql rm -rf /etc/selinux/targeted/active/modules/100/mysql rm -rf /var/spool/mail/mysql
5、卸载后/etc/my.cnf不会删除,需要进行手工删除
rm -rf /etc/my.cnf
6、再次查找机器是否安装mysql
rpm -qa|grep -i mysql
无结果,说明已经卸载彻底,接下来直接安装mysql即可。
三、按第一步重新安装
msyql安装成功后,
1、当执行一下命令时,如果能找到密码,则按如下一步一步操作,如果报/var/log/mysqld.log找不到,则跳至末尾。
grep 'A temporary password' /var/log/mysqld.log
效果如下:
2021-11-20T05:34:39.441927Z 1 [Note] A temporary password is generated for root@localhost: S;UtWVTUx4rH
2、登录
mysql -u root -p
输入密码
3、选择mysql这个库,因为mysql的root密码存放在这个数据库里。
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
报错:
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
原因:密码太过简单的原因。
解决办法:
方法一:把密码设置复杂点(这是最直接的方法)
方法二:关闭mysql密码强度验证(validate_password)
编辑配置文件
vim /etc/my.cnf
增加如下
validate_password=off
重启mysql服务
systemctl restart mysqld.service
登录mysql,执行如下代码
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
结果如下
Query OK, 0 rows affected (0.00 sec)
4、设置mysql能够远程访问
登录进MySQL,给予root用户访问权限
MySQL默认root用户只能本地登录,如果要远程连接,要简单设置下,这里直接用root来远程登录不添加其他角色。
GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
flush privileges;
指定ip:%代表所有Ip,此处也可以输入Ip来指定Ip
5、添加3306端口
打开防火墙
systemctl start firewalld
查看防火墙状态
systemctl status firewalld
添加端口命令
firewall-cmd --zone=public --add-port=3306/tcp --permanent;
重启防火墙
firewall-cmd --reload
6、MySQL设置utf8
打开/etc/my.cnf也就是数据库的配置文件,然后在底部复制粘贴
[mysqld] character_set_server=utf8 init_connect='SET NAMES utf8'
采用navicat新建数据库时,需要将编码方式设置为,字符集:utf8 -- UTF-8 Unicode ,排序规则:utf8_general_ci
7、阿里云的防火墙中加入mysql连接的规则。这个很重要不然远程无法连接上。
此时用navicat就可以连接上mysql服务器。
报错
当运行如如下命令
yum install mysql-community-server
报错如下时:
修改vim /etc/yum.repos.d/mysql-community.repo 源文件,设置gpgcheck=0