今天遇到问题,虚机开启后,运行MySQL报错,
[root@XXX ~]# mysql
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket
‘/var/lib/mysql/mysql.sock’ (2)
很明显是,mysqld
服务没有开启
造成的,
于是尝试手工开启 mysqld 服务
[root@XXX ~]# systemctl status mysql
● mysqld.service - MySQL Community Server Loaded: loaded
(/usr/lib/systemd/system/mysqld.service; enabled; vendor preset:
disabled)Active: activating (start-post)
since 二 2022-02-01
09:20:07 CST; 6min ago Process: 1457 ExecStart=/usr/bin/mysqld_safe
–basedir=/usr (code=exited, status=0/SUCCESS) Process: 1018 ExecStartPre=/usr/bin/mysql-systemd-start pre (code=exited,
status=0/SUCCESS) Main PID: 1457 (code=exited, status=0/SUCCESS);
: 1458 (mysql-systemd-s) Memory: 78.9M
但报错,显示状态为: Active: activating (start-post)
去看日志
,找错误原因
[root@XXX ~]# cat /var/log/mysqld.log
2022-02-01 09:20:15 1719 [ERROR] Can’t start server: Bind on TCP/IP
port: Addressalready in use
2022-02-01 09:20:15 1719 [ERROR] Do you
already have another mysqld server running on port: 3306 ? 2022-02-01
09:20:15 1719 [ERROR] Aborting
日志记录显示,端口被占用了,
于是,想通过手工结束mysqld ,
使用命令 systemctl mysqld stop,
然后再手工开mysqld ,
systemctl start mysqld,
这样做是可行的,但是感觉,还是让MySQL自己开机的时候自启动要好些,于是,开始检查,MySQL相关文件是否正常,再着手让MySQL自启动
第一个要检查,mysql.sock
是不是正常在/var/lib/mysql
目录中
[root@XXX ~]# ll /var/lib/mysql
总用量 110604
-rw-rw----. 1 mysql mysql 56 1月 31 15:35 auto.cnf
-rw-rw----. 1 mysql mysql 12582912 2月 1 09:30 ibdata1
-rw-rw----. 1 mysql mysql 50331648 2月 1 09:30 ib_logfile0
-rw-rw----. 1 mysql mysql 50331648 1月 31 17:52 ib_logfile1 drwx------. 2 mysql mysql 4096 1月 31 15:35 mysql
srwxrwxrwx. 1 mysql mysql 0 2月 1 09:30mysql.sock
drwx------. 2 mysql mysql 4096 performance_schema
没问题它在,如果没它,MySQL就起不来,报
错误
ERROR 2002 (HY000): Can't connect to local MySQL
server through socket
'/var/lib/mysql/mysql.sock' (2)
上述正常,就开始进行MySQL自启动相关设置,
[root@XXX ~]# chkconfig
注:该输出结果只显示 SysV 服务,并不包含 原生 systemd 服务。SysV 配置数据 可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 ‘systemctl list-unit-files’。
查看在具体 target 启用的服务请执行
‘systemctl list-dependencies [target]’。
netconsole
0:关 1:关 2:关 3:关 4:关 5:关 6:关
network
0:关 1:关 2:开 3:开 4:开 5:开 6:关
喵的,发现chkconfig
没有列出 MySQL
再去查看MySQL端口3306
正常监听中,按道理chkconfig应该能列出mysql啊,
[root@XXX usr]# netstat -an | grep 3306
tcp6 0 0 :::
3306
LISTEN
再看看mysqld.service
是不是没被 chkconfig
管理到,
先看mysqld.service 都有哪些,
[root@XXX usr]# find / -name mysqld.service
/sys/fs/cgroup/blkio/system.slice/mysqld.service
/sys/fs/cgroup/memory/system.slice/mysqld.service
/sys/fs/cgroup/devices/system.slice/mysqld.service
/sys/fs/cgroup/cpu,cpuacct/system.slice/mysqld.service
/sys/fs/cgroup/systemd/system.slice/mysqld.service
/etc/systemd/system/multi-user.target.wants/mysqld.service
/usr/lib/systemd/system/mysqld.service
准备把这个 /usr/lib/systemd/system/mysqld.service
交给 chkconfig 管理,就需要把 /usr/lib/systemd/system/mysqld.service
放到 /etc/init.d/
里面,
先看看 /etc/init.d/ 里面都有啥
[root@XXX usr]# ll /etc/init.d/
总用量 44
-rw-r–r–. 1 root root 17500 5月 3 2017 functions
-rwxr-xr--
. 1 root root 1073 2月 1 09:43mysql
-rwxr-xr-x. 1 root root 4334 5月 3 2017 netconsole
-rwxr-xr-x. 1 root root 7293 5月 3 2017 network
-rw-r–r–. 1 root root 1160 8月 5 2017 README
里面有一个MySQL了,但权限少个执行权限,x
用命令给它加上 执行权限
[root@XXX usr]# chmod +x /etc/init.d/mysql
[root@XXX usr]# ll /etc/init.d/
总用量 44
-rw-r–r–. 1 root root 17500 5月 3 2017 functions
-rwxr-xr-x
. 1 root root 1073 2月 1 09:43 mysql
-rwxr-xr-x. 1 root root 4334 5月 3 2017 netconsole
-rwxr-xr-x. 1 root root 7293 5月 3 2017 network
-rw-r–r–. 1 root root 1160 8月 5 2017 README
再看还是没有,看来不是权限的问题,
[root@XXX usr]# chkconfig
注:该输出结果只显示 SysV 服务,并不包含 原生 systemd 服务。SysV 配置数据 可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 ‘systemctl list-unit-files’。
查看在具体 target 启用的服务请执行
‘systemctl list-dependencies [target]’。
netconsole
0:关 1:关 2:关 3:关 4:关 5:关 6:关
network
0:关 1:关 2:开 3:开 4:开 5:开 6:关
既然没有,就尝试往 chkconfig 里添加一下,试试
[root@XXX usr]# chkconfig --add mysql
服务 mysql 不支持 chkconfig
喵的直接报错,不支持
于是网查资料,涉及到 rc.d
的知识,放上来跟大家分享一下
每个被chkconfig管理的服务需要在对应的init.d下的脚本(指的就是
/etc/init.d/
里的那些文件)加上两行
或者更多行的注释。
第一行
告诉chkconfig缺省启动的运行级
以及启动和停止
的优先级。
如果某服务缺省不在任何运行级启动,那么使用 - 代
替运行级。
第二行对服务进行描述,可以用\ 跨行注释。 例如,random.init包含三行:
# chkconfig: 2345 20 80
# description: Saves and restores system entropy pool for\
# higher quality random number generation.附加介绍一下Linux系统的运行级的概念:
Linux OS 将操作 环境分为以下7个等级,
即
0:关机
1:单用户模式(单用户、无网络)
2:无网络支持的多用户模式(多用户、无网络)
3:有网络支持的多用户模式(多用户、有网络)
4:保留,未使用
5:有网络支持有X-Window支持的多用户模式(多用户、有网络、X-Window界面)
6:重新引导系统,即重启Linux中有多种运行级,常见的就是多用户的2,3,4,5 ,
很多人知道5是运行X-Windows的级别,而0就是关机了。
运行级的改变可以通过init
命令来切换。
例如,假 设你要维护系统进入单用户状态,那么,可以使用init1
来切换。
在Linux的运行级的切换过程中,系统会自动寻找对应运行级的目录/etc/rc
[0-6
].d下的K和S
开头的文件,
按后面的数字顺序,执行这些脚本。
对这些脚本的维护,是很繁琐的一件事情,Linux提供了chkconfig命令用来更新和查询不同运行级上的系统服务。
rc.d
的意思是: runlevel control directory 大多数的Linux
发行版本中,启动脚本都被放在/etc/rc.d/init.d。这些脚本被ln 命令来连接到 /etc/rc.d/rcn.d 目录。(这里的n
就是运行级0-6) 例如/etc/rc.d/rc2.d缺省的运行级,如下:
0 - 停机(千万不要把initdefault 设置为0 )
1 - 单用户模式
2 - 多用户,但是没有 NFS
3 - 完全多用户模式
4 - 没有用到
5 - X11
6 - 重新启动 (千万不要把initdefault 设置为6 )
Init
进程是系统启动之后的第一个用户进程,所以它的pid(进程编号)始终为1。
网上资料大概意思是,放到 /etc/init.d/
这里的服务还得编辑一下,才能被 chkconfig 识别和管理到,
于是,在 /etc/init.d/
里对 已经存在的mysql
服务进行编辑
需要添加两个以#
开头的,注释语句
想列出 /etc/init.d
文件夹里有没有 mysql,发现它只是个链接,
指向 /etc/rc.d/init.d
[root@XXX usr]# ll /etc/init.d
lrwxrwxrwx. 1 root root 11 8月 8 2020 /etc/init.d
->
rc.d/init.d
[root@XXX ~]# cd /etc/rc.d/init.d
[root@XXX init.d]# dir
functions
mysql
netconsole network README
[root@XXX init.d]# pwd
/etc/rc.d/init.d
再看看没添加注释之前的 mysql都有啥
[root@XXX init.d]# vi mysql
# Simple MySQL systemd service file
# systemd supports lots of fancy features, look here (and linked docs) for a full list: #
http://www.freedesktop.org/software/systemd/man/systemd.exec.html
# Note: this file ( /usr/lib/systemd/system/mysql.service ) # will be overwritten on package upgrade, please copy the file to
# /etc/systemd/system/mysql.service
# to make needed changes.
# systemd-delta can be used to check differences between the two mysql.service files.
[Unit] Description=MySQL Community Server After=network.target
After=syslog.target[Install] WantedBy=multi-user.target Alias=mysql.service
[Service] “mysql” 48L, 1073C
开干,给mys 加上 两行注释,让chkconfig 能管理到 MySQL
[root@XXX init.d]# vi mysql
# Simple MySQL systemd service file
# systemd supports lots of fancy features, look here (and linked docs) for a full list:
# http://www.freedesktop.org/software/systemd/man/systemd.exec.html
# Note: this file ( /usr/lib/systemd/system/mysql.service )
# will be overwritten on package upgrade, please copy the file to
# /etc/systemd/system/mysql.service
# to make needed changes.
# systemd-delta can be used to check differences between the two mysql.service files.
# chkconfig: 2345 20 20
# description:February the 01st tuesday 2022
[Unit] Description=MySQL Community Server After=network.target
After=syslog.target
[Install] WantedBy=multi-user.target Alias=mysql.service “mysql” 50L, 1142C
注释里的 3个数字的含义,
chkconfig: 2345 20 20
【2345是启动级别,前面的20表示开机后第20个启动程序,后面的20表示此程序由系统来自动关闭应用程序时它的顺位是第20个】
[root@XXX init.d]# chkconfig --add mysql
成功.
但,加参数 --list 和 直接 chkconfig 都列不出mysql,
[root@XXX init.d]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含 原生 systemd 服务。SysV 配置数据 可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 ‘systemctl list-unit-files’。
查看在具体 target 启用的服务请执行
‘systemctl list-dependencies [target]’。
netconsole
0:关 1:关 2:关 3:关 4:关 5:关 6:关
network
0:关 1:关 2:开 3:开 4:开 5:开 6:关
不显示,mysql ,那有没有可能是chkconfig 没操作过 mysql 所以看不到呢,让 chkconfig 管理一遍 mysql,再看看,
[root@XXX ~]# chkconfig mysql on
注意:
正在将请求转发到“systemctl enable mysql.service
”。
Failed
to execute
operation:Too many levels
of symbolic links
symbolic 英 [sɪmˈbɒlɪk]
美 [sɪmˈbɑːlɪk]
adj. 象征的; 使用象征的; 作为象征的
两个重点,
1,chkconfig mysql on
就是去执行 systemctl enable mysql.service
2,报错,Too many levels
那么不通过 chkconfig ,咱们手工执行一下 systemctl enable mysql.service
看看报错不,
[root@XXX ~]# systemctl enable mysql.service
Failed
to execute operation:Too many levels
of symbolic links
结果,同款报错
那问题就一定出在命令 systemctl enable mysql.service
上了,
mysql.service
,这个看起来有点怪呀,
正常咱们开关/重启 MySQL 的时候,都是使用的
systemctl mysqld
呀,
要不试试 mysqld
.service
[root@XXX ~]# systemctl enable mysqld.service
[root@XXX ~]#
果然,是 linux系统不认识 mysql.service ,
那就好办了,
给 /etc/init.d/mysql
改名成/etc/init.d/mysqld
[root@XXX ~]# cd /etc/init.d
[root@XXX init.d]# dir
functions
mysql
netconsole network README
[root@XXX init.d]# ll
总用量 44
-rw-r–r–. 1 root root 17500 5月 3 2017 functions
-rwxr-xr-x. 1 root root 1142 2月 1 10:26mysql
-rwxr-xr-x. 1 root root 4334 5月 3 2017 netconsole
-rwxr-xr-x. 1 root root 7293 5月 3 2017 network
-rw-r–r–. 1 root root 1160 8月 5 2017 README
[root@XXX init.d]# cp mysql mysqld
[root@XXX init.d]# ll
总用量 48
-rw-r–r–. 1 root root 17500 5月 3 2017 functions
-rwxr-xr-x. 1 root root 1142 2月 1 10:26mysql
-rwxr-xr-x. 1 root root 1142 2月 1 10:55mysqld
-rwxr-xr-x. 1 root root 4334 5月 3 2017 netconsole
-rwxr-xr-x. 1 root root 7293 5月 3 2017 network
-rw-r–r–. 1 root root 1160 8月 5 2017 README
为排除干扰,干掉mysql
[root@XXX init.d]# rm mysql
rm:是否删除普通文件 “mysql”?
y
[root@XXX init.d]# ll
总用量 44
-rw-r–r–. 1 root root 17500 5月 3 2017 functions
-rwxr-xr-x. 1 root root 1142 2月 1 10:55mysqld
-rwxr-xr-x. 1 root root 4334 5月 3 2017 netconsole
-rwxr-xr-x. 1 root root 7293 5月 3 2017 network
-rw-r–r–. 1 root root 1160 8月 5 2017 README
[root@XXX init.d]# chkconfig --list mysqld
注:该输出结果只显示 SysV 服务,并不包含 原生 systemd 服务。SysV 配置数据 可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 ‘systemctl list-unit-files’。
查看在具体 target 启用的服务请执行
‘systemctl list-dependencies [target]’。
mysqld
0:关 1:关 2:开 3:开 4:开 5:开 6:关
终于出来了,太不容易了
但,要注意,chkconfig 列出 MySQL 是需要加参数–list 和指明 MySQL的名字的.
额外的,rcn.d的说明,rc0.d~rc6.d
,7个 run-level control directory 文件,
作用不同,
[root@XXX init.d]# ll /etc/rc.d/
rc3
.d |grep mysqld
lrwxrwxrwx. 1 root root 16 2月 1 10:56
S20
mysqld -> …/init.d/mysqld
[root@XXX init.d]# ll /etc/rc.d/
rc2
.d |grep mysqld
lrwxrwxrwx. 1 root root 10:56
S20
mysqld -> …/init.d/mysqld
[root@XXX init.d]# ll /etc/rc.d/
rc1
.d |grep mysqld
lrwxrwxrwx. 1 root root 16 2月 1 10:56
K20
mysqld -> …/init.d/mysqld
[root@XXX init.d]# ll /etc/rc.d/
rc0
.d |grep mysqld lrwxrwxrwx. 1 root root 16 2月 1 10:56
K20
mysqld …/init.d/mysqld
[root@XXX init.d]# ll /etc/rc.d/
rc4
.d |grep mysqld
lrwxrwxrwx. 1 root root 16 2月 1 10:56
S20
mysqld -> …/init.d/mysqld
[root@XXX init.d]# ll /etc/rc.d/
rc5
.d |grep
mysqld lrwxrwxrwx. 1 root > root 16 2月 1 10:56
S20
mysqld -> …/init.d/mysqld
[root@XXX init.d]# ll /etc/rc.d/
rc6
.d |grep mysqld
lrwxrwxrwx. 1 root root 16 2月 1 10:56
K20
mysqld -> …/init.d/mysqld
[root@XXX init.d]# ll /etc/rc.d/
rc7
.d |grep mysqld
ls: 无法访问/etc/rc.d/rc7.d:没有
那个文件或目录
注:利用yum,rqm安装的服务,启动命令都会自动放在init.d下面,并且接受chkconfig管理
最后,再补充一点,
到这里,其实仍然不能通过 一个 chkconfig 来列出 MySQL的,
必须让 chkconfig 带着参数–list 再加上MySQL的名称一起,才能正常列出 MySQL来,并且 MySQL服务的名称必须正确 为:mysqld
[root@XXX init.d]# chkconfig
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@XXX init.d]# chkconfig --list
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@XXX init.d]# chkconfig --list mysqld
mysqld
0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@XXX init.d]# chkconfig --list mysql
服务 mysql 信息读取出错:没有那个文件或目录
后来又查了点资料,可能说明了这个问题产生的原因,
“chkconfig 在centos 7 里已经不怎么用了,原因是,服务都已经移交给
systemctl
来管理了,所以,在使用chkconfig 列出 开机自启动服务的时候会遇到一些小问题.”
February the 01st 2022 tuesday
今天是大年初四,
来补充一下,
这几天在跟 mysql开机启动慢
的问题较劲,
查阅了好多资料,
最后发现根本原因
是之前装的docker引起的端口3306被占用,导致mysql启动慢的问题
,
同时也对这个 chkconfig 设置各种服务的开机启动项有了点了解,
实际上,
[root@bogon mysqld]# systemctl status mysqld
● mysqld.service - MySQL Community Server Loaded: loaded
(/usr/lib/systemd/system/mysqld.service;enabled
; vendor preset:
disabled) Active: active (running) since Fri 2022-02-04 10:23:36
CST; 2h 5min ago
这里的回显结果中的,enabled
就已经表示是开机自动启动了,所以没必要再去,像上面一样繁琐的配置 chkconfig 来让 服务开机自启动了,
让服务达到开机自启动的enabled
状态,的命令是,
[root@bogon mysqld]# systemctl enable mysqld
请注意,命令中是enable ,而不是 enabled
同时,也要知道,实际上 chkconfig 要做的, 也只是将开机启动的操作传递给 systemctl ,由systemctl 去做,
所以,通过chkconfig 去设置各种服务开机启动是很麻烦的,也已经基本不用了,现在都是直接通过 systemctl去操作.