ubuntu19.10开启与关闭mysql自启动和关闭防火墙

CentOS7开始使得以往老版本系统服务的/etc/init.d的启动脚本的方式就此改变,在CentOS7中所有对服务的管理都集中到了systemctl当中。

作者测试ubuntu继承了这一特性,使得以往在运行级别和/etc/init中设置关闭某个服务如mysql时常常关闭不掉.

systemctl是一个系统管理守护进程、工具和库的集合,用于取代以往的System V、service和chkconfig命令。

创建用于启动MySQL的配置文件

[root@localhost ~]# touch /usr/lib/systemd/system/mysqld.service

[root@localhost ~]# cd /usr/lib/systemd/system

注:mysql新版本中如8.0.17-0ubuntu2中此处的mysqld改为mysql,下同.

编辑mysqld.service文件,加入如下内容:

[root@localhost system]# vi mysqld.service

[Unit]

Description=MySQL Server

Documentation=man:mysqld(8)

Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html

After=network.target

After=syslog.target

[Install]

WantedBy=multi-user.target

[Service]

User=mysql

Group=mysql

ExecStart=/opt/mysql-5.7.18/bin/mysqld --defaults-file=/etc/my.cnf

LimitNOFILE = 5000

保存退出

备注:ExecStart=/opt/mysql-5.7.18/bin/mysqld (此处请对应修改为MySQL程序所在的路径)

查找mysqld路径,例如:

[root@localhost system]# which mysqld

/opt/mysql-5.7.18/bin/mysqld

通过systemctl方式启动mysql5.7:

[root@localhost system]# systemctl start mysqld

检查MySQL运行状态:

[root@localhost system]# mysql -p

关闭防火墙:

[root@localhost system]# systemctl stop firewalld.service

[root@localhost system]# systemctl disable firewalld.service

Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.

Removed symlink /etc/systemd/system/dbus-org.Fedoraproject.FirewallD1.service.

设置mysql的开机启动:

[root@localhost system]# systemctl enable mysqld

Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.

[root@localhost system]# systemctl list-unit-files | grep mysqld

mysqld.service enabled

取消mysql的开机自启动:

[root@localhost system]# systemctl disable mysqld

Removed symlink /etc/systemd/system/multi-user.target.wants/mysqld.service.

[root@localhost system]# systemctl list-unit-files | grep mysqld

mysqld.service disabled

此时systemctl disable mysql如果出错可能是之前已经尝试把所有运行级别的mysql服务都移除了,需要重新在运行级别中加上之后再进行此操作

添加方法为 20