CentOS 7 安装MySQL 笔记

一、检查系统是否安装老版本,有的话参见上一篇文章,清除旧版本。

#yum list installed | grep mysql

#yum -y remove mysql-libs.x86_64

二、下载并安装MySQL官方的 Yum Repository

具体选择什么版本查看http://repo.mysql.com来定

wget http://repo.mysql.com/mysql-community-release-el7-7.noarch.rpm


三、使用上面的命令就直接下载了安装用的Yum Repository,大概25KB的样子,然后就可以直接yum安装了。

yum -y install mysql-community-release-el7-7.noarch.rpm


四、之后就开始安装MySQL服务器

yum -y install mysql-community-server


这步可能会花些时间,安装完成后就会覆盖掉之前的mariadb,具体多久根据个人网速决定。

安装完成,接下来进行mysql的一些配置。

可能会遇到以下情况:

原因是MySQL的GPG升级了,需要重新导入,执行下面的命令即可:

rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
yum install mysql-community-server

MySQL数据库设置

(1)首先启动MySQL

systemctl start  mysqld.service

此时MySQL已经开始正常运行,不过要想进入MySQL还得先找出此时root用户的密码,通过如下命令可以在日志文件中找出密码:
 

grep "password" /var/log/mysqld.log

如下命令登录数据库

mysql -uroot -p

此时不能做任何事情,因为MySQL默认必须修改密码之后才能操作数据库,如下命令修改密码:

set password for root@localhost = password('China123');

是以为密码的复杂度不符合默认规定,如下命令查看mysql默认密码复杂度:

 SHOW VARIABLES LIKE 'validate_password%';

查看mysql是否自启动,并且设置开启自启动

chkconfig --list | grep mysqld
systemctl enable mysqld.service

mysql安全设置

mysql_secure_installation
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):<–初次运行,输入刚刚找到的初始密码,会强制你先修改密码才能进行下去
OK, successfully used password, moving on…
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] <– 是否设置root用户密码,输入y并回车或直接回车
New password: <– 设置root用户的密码
Re-enter new password: <– 再输入一次你设置的密码
Password updated successfully!
Reloading privilege tables..
… Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] <– 是否删除匿名用户,生产环境建议删除,所以直接回车
… Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] <–是否禁止root远程登录,根据自己的需求选择Y/n并回车,建议禁止
… Success!
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] <– 是否删除test数据库,直接回车
- Dropping test database…
… Success!
- Removing privileges on test database…
… Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] <– 是否重新加载权限表,直接回车
… Success!
Cleaning up…
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!

【可选步骤】

原来Linux下的MySQL默认是区分表名大小写的,通过如下设置,可以让MySQL不区分表名大小写:
1、用root登录,修改 /etc/my.cnf;
2、在[mysqld]节点下,加入一行:

lower_case_table_names=1


3、重启MySQL即可;

service mysqld restart

忘记密码处理方式

1. 关闭正在运行的MySQL服务。

sudo systemctl stop mysqld

2.无密码模式启动mysql

mysqld_safe --skip-grant-tables

3.登录mysql

mysql -u root

4.重置mysql密码

FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

5. 刷新权限:flush privileges; (不要忘记分号)

6.exit或者ctrl+c退出,进行重新登陆

允许远程访问

登录本地MySQL服务器

以管理员身份登录到MySQL服务器,输入MySQL root用户的密码:

   mysql -u root -p

登录成功后,切换到MySQL系统数据库:

   use mysql;

更新用户权限:
修改root用户的host字段(或其他需要远程访问的用户),允许任意远程主机(用 ‘%’ 表示)进行连接,但这一步可能涉及到安全风险,建议只针对需要远程访问的特定用户设置具体的远程IP地址,而不是使用通配符 % 开启所有远程访问:

   update user set host = '%' where user = 'root';

刷新权限

   flush privileges;

服务器的防火墙规则允许MySQL服务端口(默认为3306)上的入站流量。

查看所以已经开放的端口

firewall-cmd --list-ports

开启端口

firewall-cmd --zone=public --add-port=3306/tcp --permanent

配置生效

firewall-cmd --reload

查看监听的端口

netstat -tunlp

注意:centos7默认没有 netstat 命令,需要安装 net-tools 工具,yum install -y net-tools

检查端口被哪个进程占用

netstat -lnpt |grep 80

查看进程的详细信息

ps 6832


中止进程

kill -9 6832

附录:防火墙命令

firewall-cmd --state                           ##查看防火墙状态,是否是running
firewall-cmd --reload                          ##重新载入配置,比如添加规则之后,需要执行此命令
firewall-cmd --get-zones                       ##列出支持的zone
firewall-cmd --get-services                    ##列出支持的服务,在列表中的服务是放行的
firewall-cmd --query-service ftp               ##查看ftp服务是否支持,返回yes或者no
firewall-cmd --add-service=ftp                 ##临时开放ftp服务
firewall-cmd --add-service=ftp --permanent     ##永久开放ftp服务
firewall-cmd --remove-service=ftp --permanent  ##永久移除ftp服务
firewall-cmd --add-port=80/tcp --permanent     ##永久添加80端口 
iptables -L -n                                 ##查看规则,这个命令是和iptables的相同的
man firewall-cmd                               ##查看帮助

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值