Linux环境下(CentOS 7)安装MySQL数据库
文章目录
- Linux环境下(CentOS 7)安装MySQL数据库
- 一、安装MySQL数据库
- 二、安装过程的中相关问题
- 三、如何卸载已安装的MySQL
- 四、参考链接
一、安装MySQL数据库
1、下载mysql源安装包(version: 5.7.41 MySQL Community Server)
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
2、安装mysql源,安装中途提示【Is this ok [y/d/N]: 】按y键继续,若结尾出现【Complete!】,则说明MySQL源安装完成。
yum localinstall mysql57-community-release-el7-8.noarch.rpm
3、 查看mysql相关资源
yum repolist enabled | grep "mysql.*-community.*"
效果如下:
4、yum方式安装mysql,安装中途提示【Is this ok [y/d/N]: 】按y键继续,若结尾出现【Complete!】,则说明MySQL源安装完成。
yum install mysql-community-server
5、设置开机启动mysql服务
# 设置开机启动mysql(systemctl enable mysqld)
systemctl enable mysqld.service
systemctl daemon-reload
# 检查是否已经安装了开机自动启动
systemctl list-unit-files | grep mysqld
# 设置开启服务
systemctl start mysqld.service
服务开启后,如下图所示
rpm -aq | grep -i mysql
7、MySQL服务相关命令
# 启动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时还有残留的数据
10、修改初始密码
-- 登陆mysql
mysql -uroot -p
-- 输入密码后回车,登陆进入(临时密码由第九步获取,我这里是【-XtbbG&lf3,h】)
use mysql;
-- 版本一
alter user root@localhost identified by '数据库密码';
-- 版本二
UPDATE mysql.user SET Password=PASSWORD('数据库密码') WHERE User='root';
简单密码无法设置解决办法
SHOW VARIABLES LIKE 'validate_password%'; -- 查看当前的密码策略
select @@validate_password_mixed_case_count; -- 查询密码认证强度要求
select @@validate_password_length; -- 查询密码长度最低要求
-- 修改默认密码后,修改密码校验参数的值
set global validate_password_policy=0;
set global validate_password_mixed_case_count=2;
11、查询mysql字符集编码&修改
show variables like '%char%'; -- 方法一
status;-- 方法二
对mysql字符集编码进行修改
# 退出数据库(sql命令)
exit;
# 停止mysql服务
systemctl stop mysqld.service
# 编辑my.cnf配置文件
vim /etc/my.cnf
# 如果配置文本没有[client]段,就自己添加上去
[client]
default-character-set=utf8
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
-- 编辑好配置文件后,重启MySQL服务;
systemctl start mysqld.service
修改前后对比
Variable_name | Value(修改前) | Value(修改后) |
---|---|---|
character_set_client | utf8 | utf8 |
character_set_connection | utf8 | utf8 |
character_set_database | latin1 | utf8 |
character_set_filesystem | binary | binary |
character_set_results | utf8 | utf8 |
character_set_server | latin1 | utf8 |
character_set_system | utf8 | utf8 |
character_sets_dir | /usr/share/mysql/charsets/ | /usr/share/mysql/charsets/ |
validate_password_special_char_count | 1 | 1 |
12、设置mysql能够远程访问**
登录进MySQL,给予root用户访问权限,MySQL默认root用户只能本地登录,如果要远程连接,要简单设置下,这里直接用root来远程登录不添加其他角色。(指定ip:%代表所有Ip,此处也可以输入Ip来指定Ip)
GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY '数据库密码' WITH GRANT OPTION;
flush privileges;
13、云主机防火墙相关配置
# 打开防火墙
systemctl start firewalld
# 查看防火墙状态
systemctl status firewalld
# 添加端口命令
firewall-cmd --zone=public --add-port=3306/tcp --permanent;
# 重启防火墙
firewall-cmd --reload
二、安装过程的中相关问题
1、msyql安装成功后,当执行一下命令时,如果能找到密码,则按如下一步一步操作;如果报/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登录命令,再输入密码即可。
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 '数据库密码';
结果如下
Query OK, 0 rows affected (0.00 sec)
4、MySQL设置utf8
打开/etc/my.cnf也就是数据库的配置文件,然后在底部复制粘贴
[client]
default-character-set=utf8
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
init_connect='SET NAMES utf8'
采用navicat新建数据库时,需要将编码方式设置为,字符集:utf8 – UTF-8 Unicode ,排序规则:utf8_general_ci
**5、 安装Mysql时,提示GPG keys有问题 **
The GPG keys listed for the “MySQL 5.7 Community Server” repository are already installed but they are not correct for this package.
Check that the correct key URLs are configured for this repository.
解决办法(以2022年为例)
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
6、阿里云的安全策略组中加入mysql连接的规则。这个很重要不然远程无法连接上。
yum install mysql-community-server
报错如下时:
修改vim /etc/yum.repos.d/mysql-community.repo 源文件,设置gpgcheck=0
三、如何卸载已安装的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
7、删除GPG 密钥
rm /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
无结果,说明已经卸载彻底,接下来直接按照第一步安装mysql即可。
四、参考链接
- https://tool.4xseo.com/article/240261.html
- https://www.cnblogs.com/miyiyangdexin/p/16534836.html