MySQL启动关闭

1.1 MySQL启动

/etc/init.d/mysqld 或 systemctl start mysqld 等于 (/usr/local/mysql/support-file/mysql.server)
                                  ↓调用
		/usr/local/mysql/bin/mysqld_safe
                                  ↓调用
                     /usr/local/mysql/bin/mysqld

如图:
image

1.2 启动说明

  1. /etc/init.d/mysqld 是一个普通的shell脚本和/usr/local/mysql/support-file/mysql.server一模一样
    (mysql.sever 也是一个shell脚本,提供了日常的启动,关闭,重启,查看状态等功能)
  2. /etc/init.d/mysqld 启动时调用的是/usr/local/mysql/bin/mysqld_safe
  3. /usr/local/mysql/bin/mysqld_safe(也是shell脚本) 调用的是真正的启动程序 /usr/local/mysql/bin/mysqld
    1.2.1 mysqld_safe简介
    mysqld_safe是一个shell脚本,使用该脚本调用mysqld的一些好处:
1.会记录启动等相关日志信息到文件中
2.会自动监控mysqld的运行状态,如果mysqld进程出现异常宕掉,mysqld_safe会尝试拉起mysqld

1.2.2 mysqld & 简介
/usr/local/mysql/bin/mysqld &

1.可以单独启动,默认会把启动的相关流程日志信息全部输出到屏幕上,会很混乱
2.该启动方法没有带关闭功能,可以使用如下关闭方法:
	2.1:登录到MySQL中,执行:mysql> shutdown;
	2.2 mysqladmin -uroot -p -S /tmp/mysql.sock shutdown
3.数据库启动不了,且未记录日志的时候,可以使用该启动方法

1.3 MySQL关闭
1.systemctl stop mysqld 只能关闭 systemctl start mysqld方式启动的实例
2./etc/init.d/mysqld stop 除了无法关闭"mysqld &" 方式启动的,其他方式启动的都可以关闭
也可以登录到MySQL中,执行shutdown;进行关闭
也可以使用mysqladmin方法进行关闭,如:

mysqladmin -uroot -p -S /tmp/mysql.sock shutdown
  1. mysqld_safe方式启动,可以使用/etc/init.d/mysqld stop方法进行关闭,也可以使用mysqladmin方法进行关闭,如:
mysqladmin -uroot -p -S /tmp/mysql.sock shutdown

1.4 MySQL忘记密码
停数据库

/etc/init.d/mysqld stop

2.安全模式启动

mysqld_safe --skip-grant-tables --skip-networking &

3.改密码

flush privileges; # 刷新内存表
alter user 'root'@'localhost' indentified with mysql_native_password by '123';

4.重启数据库

/etc/init.d/mysqld restart

例如:

[root@localhost ~]# mysql -uroot -p123   
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@localhost ~]# /etc/init.d/mysqld stop  # 停止数据库
Shutting down MySQL. SUCCESS! 
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# mysqld_safe --skip-grant-tables --skip-networking & # 安全模式启动
[1] 25328
[root@localhost ~]# 2021-12-21T15:56:19.333795Z mysqld_safe Logging to '/data/3306/data/localhost.err'.
2021-12-21T15:56:19.359056Z mysqld_safe Starting mysqld daemon with databases from /data/3306/data

[root@localhost ~]# mysql  # 此时可以不输入密码,直接登录
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.24 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> flush privileges;  # 登录成功,刷新内存表
Query OK, 0 rows affected (0.02 sec)

mysql> alter user 'root'@'localhost' identified with mysql_native_password by '123';
Query OK, 0 rows affected (0.02 sec) # 修改密码

mysql> quit
Bye
[root@localhost ~]# /etc/init.d/mysqld restart # 重启服务
Shutting down MySQL. SUCCESS! 
Starting MySQL.2021-12-21T15:57:59.296853Z mysqld_safe mysqld from pid file /data/3306/data/localhost.pid ended
 SUCCESS! 
[1]+  Done                    mysqld_safe --skip-grant-tables --skip-networking
[root@localhost ~]# 
[root@localhost ~]# mysql -uroot -p123  # 登录验证
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.24 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

1.5 小结
1.mysql.sever 也是一个shell脚本,提供了日常的启动,关闭,重启,查看状态等功能
2.mysqld_safe 和 mysqld共同特点:启动时候都可以加一些简单的参数
3. /etc/init.d/mysqld 方式启动,可以直接停止,启动,重启,看状态

posted @ 2021-12-22 23:09  红桃Z  阅读(482)  评论(0编辑  收藏  举报