在CentOS 7上安装MySQL server 5.7
前言
本文介绍了在CentOS 7 上以rpm包的方式安装MySQL 5.7,并测试从本地通过MySQL Workbench客户端远程连接访问mysql server。
安装方式:虽然MySQL提供了更为方便的Docker安装方法,但是考虑到数据的重要性,以及大多数数据库都还是安装在专门的server上,所以这里还是以rpm包的方式来安装。
MySQL版本:截止目前(2018年8月30日)MySQL server的最新版本是8.0.12,但是考虑到大多数应用使用的还是MySQL 5.x,所以这里使用MySQL 5.x中的最高版本MySQL 5.7.23。
下载
下载mysql-5.7.23-1.el7.x86_64.rpm-bundle.tar(含有多个mysql的rpm包):
https://dev.mysql.com/downloads/file/?id=479031
或用迅雷下载:
https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.23-1.el7.x86_64.rpm-bundle.tar
安装
解压bundle.tar包:
tar -xvf mysql-5.7.23-1.el7.x86_64.rpm-bundle.tar
bundle.tar 包中包含以下rpm包:
├── mysql-community-client-5.7.23-1.el7.x86_64.rpm
├── mysql-community-common-5.7.23-1.el7.x86_64.rpm
├── mysql-community-devel-5.7.23-1.el7.x86_64.rpm
├── mysql-community-embedded-5.7.23-1.el7.x86_64.rpm
├── mysql-community-embedded-compat-5.7.23-1.el7.x86_64.rpm
├── mysql-community-embedded-devel-5.7.23-1.el7.x86_64.rpm
├── mysql-community-libs-5.7.23-1.el7.x86_64.rpm
├── mysql-community-libs-compat-5.7.23-1.el7.x86_64.rpm
├── mysql-community-minimal-debuginfo-5.7.23-1.el7.x86_64.rpm
├── mysql-community-server-5.7.23-1.el7.x86_64.rpm
├── mysql-community-server-minimal-5.7.23-1.el7.x86_64.rpm
└── mysql-community-test-5.7.23-1.el7.x86_64.rpm
根据MySQL官网 https://dev.mysql.com/doc/refman/5.7/en/linux-installation-rpm.html 只需要安装以下rpm包:
Package Name | Summary |
---|---|
mysql-community-client-5.7.23-1.el7.x86_64.rpm | MySQL client applications and tools |
mysql-community-common-5.7.23-1.el7.x86_64.rpm | Common files for server and client libraries |
mysql-community-libs-5.7.23-1.el7.x86_64.rpm | Shared libraries for MySQL database client applications |
mysql-community-libs-compat-5.7.23-1.el7.x86_64.rpm | Shared compatibility libraries for previous MySQL installations |
mysql-community-server-5.7.23-1.el7.x86_64.rpm | Database server and related tools |
查看之前是否已经安装过mysql:
rpm -qa | grep -i mysql
如果已安装,通过
rpm -e <package>
来删除已安装的包。
安装mysql:
yum install mysql-community-{server,client,common,libs}-* --exclude='*minimal*'
启动mysql server
安装完成后,启动mysql:
systemctl enable mysqld
systemctl start mysqld
查看mysql运行状态:
systemctl status mysqld
注意:mysql的服务名是mysqld,不是mysql。
设置mysql的root账号密码
查看自动生成的初始密码
grep 'temporary password' /var/log/mysqld.log
执行下面命令登录mysql,按提示输入初始密码
mysql -uroot -p
重设密码:
ALTER USER 'root'@'localhost' IDENTIFIED BY '<new_password>';
注意:密码需要包括大小写字母、数字和特殊字符。
警告:在生产环境上不要设置被容易猜到的密码,避免安全隐患。
授权主机连接
为了安全起见,mysql server缺省不允许root账号远程连接。
需要授权给指定的主机,该主机才可以通过root账号远程连接mysql server。
可以选择授权任意主机连接,或只允许指定主机(IP)连接。
警告:在生产环境上,必须授权只有指定主机才可连接,防止密码泄露时,数据库被“脱库”。
授权任意主机可连接:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '<password>' WITH GRANT OPTION;
授权指定主机(IP)可连接:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'xxx.xxx.xxx.xx' IDENTIFIED BY '<password>' WITH GRANT OPTION;
刷新授权设置:
FLUSH PRIVILEGES;
退出mysql命令行:
quit
重启MySQL让授权设置修改生效:
systemctl restart mysqld
如果仍然不能远程连接,请运行
telnet <ip> 3306
测试3306端口是否已经打开。如果不能telnet通,可能是因为防火墙禁止了3306端口的访问,请参见下面的步骤在防火墙上打开3306端口。
参考文档:
在防火墙上打开3306端口
检查防火墙状态:
systemctl status firewalld
警告:在生产环境上不要关闭防火墙,而只需要打开3306端口。
在防火墙上打开3306端口:
firewall-cmd --permanent --add-port=3306/tcp
firewall-cmd --reload
再次运行telnet <ip> 3306
测试是否可以telnet通。
参考文档:
- https://www.rootusers.com/how-to-open-a-port-in-centos-7-with-firewalld/
- https://stackoverflow.com/questions/24729024/open-firewall-port-on-centos-7
使用MySQL Workbench连接mysql数据库服务器
打开 https://dev.mysql.com/downloads/workbench/
先安装下面两个插件
再安装MySQL Workbench客户端软件。
新建连接,访问mysql数据库服务器。
新建一个SQL file,输入命令:
show databases;
按下Ctrl + Enter键执行命令,来查询数据库列表。