百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程文章 > 正文

centos 7.9 安装配置mysql5.7

qiyuwang 2024-11-23 21:42 25 浏览 0 评论

系统配置

# 禁用防火墙与SELINUX
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

# 开启防火墙启用MySQL 3306端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
# 重载配置
firewall-cmd --reload
# 查看端口号是否开启(返回yes开启)
firewall-cmd --query-port=3306/tcp
# 获取防火墙列表
firewall-cmd --list-all

# 磁盘分区(数据库数据盘)
mkfs.xfs /dev/sdb
mkdir /data
mount /dev/sdb /data

# 设置自动挂载
# 获取磁盘UUID
[root@mysql8 ~]# blkid | grep sdb
/dev/sdb: UUID="dc71382a-b53a-40ff-a0fe-2c0422105ddd" TYPE="xfs"
# 追加至/etc/fstab
echo "UUID=dc71382a-b53a-40ff-a0fe-2c0422105ddd       /data           xfs            defaults        0 0" >> /etc/fstab

2.配置安装源

yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

3.安装mysql5.7

yum install mysql-community-server
# 启动服务&设置开机自启动
systemctl start mysqld && systemctl enable mysqld && systemctl status mysqld初始化数据库

4.初始化数据库

# 获取数据库临时密码
grep 'A temporary password' /var/log/mysqld.log | tail -1
# 初始化数据库
mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: **********

The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100
# 修改root管理员密码
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

New password: ******************

Re-enter new password: ******************

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
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? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

# 禁止root账号远程登录
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
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? (Press y|Y for Yes, any other key for No) : y
 - 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? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

修改数据库存放位置

数据库存放在系统盘始终是不安全的,所以一般我们会配置一个数据盘,用于单独存放数据库文件,在前面的【系统通用配置】中,我们已经挂载了磁盘/dev/sdb至/data,这个就是专用用于数据库数据存储的,详细MySQL修改方法如下:

# 创建数据库存放目录
mkdir /data/mysql
# 配置权限
chown -vR mysql:mysql /data/mysql/
chmod -vR 700 /data/mysql/
# 停止数据库服务
systemctl stop mysqld
# 复制原数据库至新目录
cp -av /var/lib/mysql/* /data/mysql/

# 修改/etc/my.cnf配置,如下所示:
[root@localhost ~]# cat /etc/my.cnf | grep -v "^#" | grep -v "^#34;
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[client]
port=3306
socket=/data/mysql/mysql.sock

[mysqld_safe]
socket=/data/mysql/mysql.sock
nice=0

# 启动数据库服务
systemctl start mysqld && systemctl status mysqld

# 确保数据库启动成功以后,我们就可以删除不再需要的原数据库文件了
rm -rf /var/lib/mysql

设置允许远程连接

远程连接可以设置root账号,也可以单独创建一个账号,专门用于远程管理,毕竟root账号的权限过大,一旦密码泄漏严重影响数据库安全。

# 登录MySQL
[root@localhost ~]# mysql -u root -p
# 进入mysql库
mysql> use mysql;
# 更新域属性,'%'表示允许任何主机访问
mysql> update user set host="%" where user ="root";
# 全局特权所有数据库
mysql> grant all privileges on *.* to 'root'@'%' with grant option;
# 生效配置
mysql> FLUSH PRIVILEGES;

MySQL基础操作

这里简单说一下单独创建用户,并允许远程连接,以及添加、删除用户&数据库的操作。

# 登录MySQL
[root@localhost ~]# mysql -u root -p

# 创建数据库
mysql> CREATE DATABASE test;

# 创建用户db_test并允许远程连接
mysql> CREATE USER 'db_test'@'%' IDENTIFIED BY 'password';

# 授权允许远程访问test数据库的所有表
mysql> GRANT ALL ON test.* TO 'db_test'@'%';

# 查询所有用户
mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
+------------------------------------+
| query                              |
+------------------------------------+
| User: 'db_test'@'%';               |
| User: 'mysql.session'@'localhost'; |
| User: 'mysql.sys'@'localhost';     |
| User: 'root'@'localhost';          |
+------------------------------------+
6 rows in set (0.01 sec)

# 查询数据库
mysql> show databases;
+-----------------------+
| Database              |
+-----------------------+
| information_schema    |
| mysql                 |
| performance_schema    |
| sys                   |
| test                  |
+-----------------------+
6 rows in set (0.00 sec)

# 删除数据库
mysql> drop database test;
Query OK, 0 rows affected (0.00 sec)

# 更新指定用户密码
mysql> UPDATE user SET authentication_string=PASSWORD('密码') WHERE User='db_test' and Host='%';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 1

# 删除用户
mysql> drop user 'db_test'@'%';
Query OK, 0 rows affected (0.00 sec)

# 生效配置
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

# 退出数据库
mysql> exit
Bye

相关推荐

基于Docker方式安装与部署Camunda流程引擎

1Camunda简介官网:https://docs.camunda.org/manual/7.19/installation/docker/Camunda是一个轻量级、开源且高度灵活的工作流和决策自...

宝塔Linux面板如何部署Java项目?(宝塔面板 linux)

通过宝塔面板部署Java还是很方便的,至少不需要自己输入tomcat之类的安装命令了。在部署java项目前,我还是先说下目前的系统环境,如果和我的系统环境不一样,导致部署不成功,那你可能需要去找其他资...

浪潮服务器如何用IPMI安装Linux系统

【注意事项】此处以浪潮服务器为例进行演示所需使用的软件:Chrome浏览器个人PC中需要预先安装java,推荐使用jdk-8u181-windows-x64.exe【操作步骤】1、在服务器的BIOS中...

Centos7环境Hadoop3集群搭建(hadoop集群环境搭建实验报告)

由于项目需要存储历史业务数据,经过评估数据量会达到100亿以上,在原有mongodb集群和ES集群基础上,需要搭建Hbase集群进行调研,所以首先总结一下Hadoop集群的搭建过程。一、三个节点的集群...

Hadoop高可用集群搭建及API调用(hadoop高可用原理)

NameNodeHA背景在Hadoop1中NameNode存在一个单点故障问题,如果NameNode所在的机器发生故障,整个集群就将不可用(Hadoop1中虽然有个SecorndaryNameNo...

使用Wordpress搭建一个属于自己的网站

现在开源的博客很多,但是考虑到wordpress对网站的seo做的很好,插件也多。并且全世界流量排名前1000万的网站有33.4%是用Wordpress搭建的!所以尝试用Wordpress搭建一个网站...

Centos 安装 Jenkins(centos 安装ssh)

1、Java安装查看系统是否已安装Javayumlistinstalled|grepjava...

Java教程:gitlab-使用入门(java中的git)

1导读本教程主要讲解了GitLab在项目的环境搭建和基本的使用,可以帮助大家在企业中能够自主搭建GitLab服务,并且可以GitLab中的组、权限、项目自主操作...

Dockerfile部署Java项目(docker部署java应用)

1、概述本文主要会简单介绍什么是Docker,什么是Dockerfile,如何安装Docker,Dockerfile如何编写,如何通过Dockerfile安装jar包并外置yaml文件以及如何通过do...

如何在Eclipse中搭建Zabbix源码的调试和开发环境

Zabbix是一款非常优秀的企业级软件,被设计用于对数万台服务器、虚拟机和网络设备的数百万个监控项进行实时监控。Zabbix是开放源码和免费的,这就意味着当出现bug时,我们可以很方便地通过调试源码来...

Java路径-02-Java环境配置(java环境搭建及配置教程)

1Window环境配置1.1下载...

35.Centos中安装python和web.py框架

文章目录前言1.Centos7python:2.Centos8python:3.进行下载web.py框架然后应用:4.安装好之后进行验证:5.总结:前言...

《我的世界》服务器搭建(我的世界服务器如何搭建)

1.CentOS7环境1.1更改YUM源#下载YUM源文件curl-o/etc/yum.repos.d/CentOS-Base.repohttps://mirrors.aliyun.com...

CentOS 7 升级 GCC 版本(centos7.4升级7.5)

1.GCC工具介绍GCC编译器:...

Linux安装Nginx详细教程(linux安装配置nginx)

环境准备1.因为Nginx依赖于gcc的编译环境,所以,需要安装编译环境来使Nginx能够编译起来。命令:yuminstallgcc-c++显示完毕,表示安装完成:2.Nginx的http模块需要...

取消回复欢迎 发表评论: