在CentOS 7/CentOS 6上安装MySQL Server

时间:2020-02-23 14:31:20  来源:igfitidea点击:

本指南可在CentOS 7/CentOS 6上安装MySQL Server。该指南将向我们展示如何安装MySQL 5.5/5.6/5.7和最新稳定版本的MySQL,本文撰写之时为MySQL 8.0。

要开始在CentOS 7/6上安装MySQL服务器,我们需要将官方的MySQL社区存储库添加到系统中。运行以下命令将其添加,

sudo yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm

在CentOS上安装MySQL 8

现在添加了Repo,因为默认情况下启用了8的Repo,所以我们可以安装MysQL 8而无需编辑存储库内容。

sudo yum --enablerepo=mysql80-community install mysql-community-server

在CentOS上安装MySQL 5.7

要安装MySQL 5.7,我们需要禁用mysql80-community存储库,然后下载它。

sudo yum --disablerepo=mysql80-community --enablerepo=mysql57-community install mysql-community-server

在CentOS上安装MySQL 5.6

要安装MySQL 5.6,我们需要禁用mysql80-community存储库,然后下载它。

sudo yum --disablerepo=mysql80-community --enablerepo=mysql56-community install mysql-community-server

启动MySQL服务

对于CentOS 7,使用systemd启动mysql服务:

sudo systemctl enable  --now mysqld.service

对于CentOS 6,请使用服务命令行工具。

sudo /etc/init.d/mysql start 
sudo chkconfig --levels 235 mysqld on

设置MySQL root密码

在CentOS 6上安装MySQL会为我们生成一个临时密码。我们可以通过运行以下命令获取它:

grep 'A temporary password is generated for theitroad@localhost' /var/log/mysqld.log |tail -1

它将如下所示:

# theitroad@localhost: ?h(UwcrvQ7jr

更改mysql root用户密码

# mysqladmin -u root password [your_password_here]
or
# mysql_secure_installation

配置防火墙

使用iptables:

sudo iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
sudo service iptables restartt

防火墙:

sudo firewall-cmd --add-service mysql --permanent
sudo firewall-cmd --reload

测试设置:

$mysql -u root -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names Jan be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;