MySQL 在 Linux 环境中的安装方式有多种,比较常见的安装方式是使用 MySQL 二进制文件安装,无须编译,直接解压即可。
下载地址
官方下载地址:http://dev.mysql.com/downloads/mysql/ 选择 Linux Generic 版本,也可以在搜狐镜像地址下载 http://mirrors.sohu.com/mysql。
安装前确保系统中没有其它版本的MySQL,删除 MySQL 相关的配置文件,如/etc/my.cnf,/etc/mysql 等目录。同时,MySQL需要依赖libaio
库,如果没有安装此库,MySQL在Data目录初始化和随后的服务器启动过程中会失败。
yum install libaio # Yum-based systems
apt-get install libaio1 # APT-based systems
安装初始化
MySQL下载后解压安装在/usr/local目下:
tar -zxvf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.7.18-linux-glibc2.5-x86_64 mysql
解压后目录文件结构为:
├── bin
├── COPYING
├── docs
├── include
├── lib
├── man
├── README
├── share
└── support-files
创建mysql用户/用户组:
groupadd mysql # 添加组
useradd -r -g mysql -s /bin/false mysql # 添加系统用户mysql, 同时归属mysql用户组,禁用登录
cd /usr/local/mysql
mkdir mysql-files
chmod 750 mysql-files
chown -R mysql .
chgrp -R mysql .
bin/mysqld --initialize --user=mysql # 初始化mysql
初始化完成后,生成临时密码
mkdir data # 创建数据目录
chown -R root .
chown -R mysql data mysql-files
bin/mysqld_safe --user=mysql & (还可以指定mysql的配置文件 --defaults-file=file_name,此选项要放在第一位)
cp support-files/mysql.server /etc/init.d/mysql.server
/etc/init.d/mysql.server start # 启动mysql
### 安全设置
启动之后,为MySQL处理一些安全相关的设置。
* 修改密码
* 移除匿名用户
* 不允许远程root等
* 移除测试数据库
* 重新加载权限表
mysql> CREATE DATABASE webdb;
## CREATE USER ##
mysql> CREATE USER 'webdb_user'@'10.0.15.25' IDENTIFIED BY 'password123'; # 如果是允许所有ip远程连过来使用 ‘%’代替ip
## GRANT PERMISSIONS ##
mysql> GRANT ALL ON webdb.* TO 'webdb_user'@'10.0.15.25';
## FLUSH PRIVILEGES, Tell the server to reload the grant tables ##
mysql> FLUSH PRIVILEGES;
如果希望root用户远程登录操作,那么可以修改:
默认情况下CentOS开启了防火墙,远程客户端没法链接过来的,因此需要添加新的防火墙规则。
systemctl stop firewalld
systemctl mask firewalld
yum install iptables-services
systemctl enable iptables
vim /etc/sysconfig/iptables
# 添加一行
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
</pre>
重启iptables
<pre style="box-sizing: border-box; overflow: auto; font-family: Consolas, monaco, monospace; font-size: 14px; display: block; padding: 20px 40px; margin: 20px 0px; line-height: 24px; color: rgb(255, 255, 255); word-break: break-all; word-wrap: break-word; background: rgb(51, 51, 51); border: none; border-radius: 0px; font-weight: 400;">service iptables restart
## OR ##
/etc/init.d/iptables restart
</pre>
当然你也可以直接把防火墙关闭,但是不建议生成环境这样做,非常不安全。
<pre style="box-sizing: border-box; overflow: auto; font-family: Consolas, monaco, monospace; font-size: 14px; display: block; padding: 20px 40px; margin: 20px 0px; line-height: 24px; color: rgb(255, 255, 255); word-break: break-all; word-wrap: break-word; background: rgb(51, 51, 51); border: none; border-radius: 0px; font-weight: 400;"># 禁止firewall开机启动
systemctl disable firewalld
# 停止防火墙
systemctl stop firewalld
# 防火墙状态
systemctl status firewalld
# 设置防火墙开机启动
systemctl enable firewalld
# 启动防火墙
systemctl start firewalld
</pre>
启动成功后,为MySQL的用户指定密码
<pre style="box-sizing: border-box; overflow: auto; font-family: Consolas, monaco, monospace; font-size: 14px; display: block; padding: 20px 40px; margin: 20px 0px; line-height: 24px; color: rgb(255, 255, 255); word-break: break-all; word-wrap: break-word; background: rgb(51, 51, 51); border: none; border-radius: 0px; font-weight: 400;">mysql> SET PASSWORD FOR 'root'@'%' = PASSWORD('new_password');
</pre>
参考: