Centos 7安装MySQL Server5.7

Step 1 — 寻找 CentOS 7 对应的 MySQL 社区版 repo Step 2 — 安装 MySQL 社区版 server Step 3 — 查询默认 MySQL 密码并修改 Step 4 — 创建 MySQL 用户和赋予权限 Step 5 — MySQL 运维

Step 1 — 寻找 CentOS 7 对应的 MySQL 社区版 repo

在 https://dev.mysql.com/downloads/repo/yum/ 寻找对应的 repo 并安装, 这里是:

wget https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
rpm -ivh mysql57-community-release-el7-11.noarch.rpm

或者直接:

rpm -Uvh https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm

Step 2 — 安装 MySQL 社区版 server

yum install mysql-server

启动服务和自启动:

service mysqld start
sudo chkconfig  mysqld on

Step 3 — 查询默认 MySQL 密码并修改

sudo grep 'temporary password' /var/log/mysqld.log

显示类似如下:

[root@alibaba]# sudo grep 'temporary password' /var/log/mysqld.log
2017-11-12T07:38:36.716385Z 1 [Note] A temporary password is generated for root@localhost: r273_1qHr:ee

进入 MySQL 修改密码:

mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Daguanren.cc@2018';

Step 4 — 创建 MySQL 用户和赋予权限

创建一个 daguanren 的用户和 daguanrendb 的数据库,将数据库的所有操作权限赋予 daguanren 用户(下面的 % 代表从任何链接过来均可访问,* 代表所有表)

mysql> CREATE USER 'daguanren'@'%' IDENTIFIED BY 'Daguanren.cc@2018';

mysql> CREATE DATABASE IF NOT EXISTS daguanrendb DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

mysql> GRANT ALL PRIVILEGES ON daguanrendb.* TO 'daguanren'@'%';

mysql> flush privileges;

Step 5 — MySQL 运维

a、查看mysql安装状态

sudo yum list installed | grep mysql

sudo rpm -qa | grep mysql*

b、打开 iptables 防火墙3306端口

 vi /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

# 增加 3306 端口
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

重启防火墙:

service iptables restart

c、 关闭CentOS 7系统自带防火墙

systemctl stop firewalld.service #停止firewall

d、删除MySQL

查找已安装的 MySQL

rpm -qa |grep -i mysql

删除 MySQL

yum remove mysql*

查找已安装的 MySQL 目录

find / -name mysql

删除 MySQL 目录

rm -rf /usr/share/mysql

主从同步可参考:Centos 7下配置MySQL Server5.7主从同步

文章来源:

Author:大官人
link:https://www.daguanren.cc/post/mysql_installation_guide.html