阿里云推出免费试用6个月的活动,申请了免费试用,切换到centos7后发现了一些改变。

本来已经有一台阿里云服务器(使用的女朋友学生证申请的学生机),但是考虑到6月份女朋友毕业后,资费就恢复正常了。 打算将服务器从她的账号下迁移出来,因此申请了免费试用,将部分业务提前切到新的服务器。

阿里云的低配服务器(单核1G内存40G硬盘1M带宽),做博客托管和ssh隧道等已经足够了。支持多种操作系统,本来打算继续使用centos6版本。但是看到新的镜像支持centos7,打算进行一次尝试。

上学的时候,一直使用的ubuntu系统学习和熟悉linux。工作后一直停留在centos 6.4 6.5 6.6 这几个版本。

切换到centos7后发现了一些改变。

部署

1.systemctl

修改ssh的默认端口后,打算重启服务的时候发现/etc/init.d/sshd没有脚本。搜索发现centos7使用systemctl做为服务管理。

systemctl是systemd工具。主要控制systemd和服务管理器。systemctl接受服务(.service),挂载点(.mount),套接口(.socket)和设备(.device)作为单元。

  • 列出所有服务可用单元
systemctl list-unit-files
  • 列出所有运行中的单元
systemctl list-units
  • 如果没有启动,则需要启动该服务(以ssh为例)
systemctl start sshd.service
  • 重启服务(以ssh为例)
systemctl restart sshd.service
  • 设置服务开机自启(以ssh为例)
systemctl enable sshd.service
  • 禁止服务开机自启(以ssh为例)
systemctl disable sshd.service

2.防火墙

centos7默认使用firewall防火墙。习惯了iptables,还是打算切回。

  • 关闭firewall
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
  • 安装iptables防火墙
yum install iptables-services #安装
vi /etc/sysconfig/iptables #编辑防火墙配置文件

3.mysql部署(yum)

centos7 yum源中默认没有mysql,需要先下载mysql的repo源。

  • 下载mysql的repo源
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
  • 安装mysql-community-release-el7-5.noarch.rpm包
rpm -ivh mysql-community-release-el7-5.noarch.rpm

安装这个包后,会获得两个mysql的yum repo源。/etc/yum.repos.d/mysql-community.repo,/etc/yum.repos.d/mysql-community-source.repo。

  • 安装mysql
yum install mysql-server
  • 重置密码
mysql -u root
chown -R root:root /var/lib/mysql
service mysqld restart
mysql -u root
mysql > use mysql;
mysql > update user set password=password('111111') where user='root';
mysql> flush privileges;#使修改生效
mysql > exit;

如果不允许从远程登陆,更改数据库里user表里的host项,从localhost改称%

mysql -u root	
mysql> use mysql;

mysql> update user set host = '%' where user = 'root' and host='localhost'; #如果不带and host='localhost'会报错,ERROR 1062 (23000): Duplicate entry ‘%-root’ for key 'PRIMARY'
mysql> flush privileges;
mysql > exit;

登录时可能报错

ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’

原因是/var/lib/mysql的访问权限问题。