MySQL管理_数据库启动与关闭

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL管理_数据库启动与关闭相关的知识,希望对你有一定的参考价值。

  mysql数据库服务器通常指的的是mysqld,而命令行mysql则是mysql客户端程序,这两个概念通常容易混淆。通常启动mysql服务器即是启动mysqld进程,mysqld启动后,可以通过mysql连接到mysql服务器。本文主要描述了mysql服务器的几种启动方式以及如何关闭mysql服务器。

1.直接使用mysqld启动

  mysqld 即是mysql服务器,可以调用该命令直接启动mysql服务器
  mysqld 从配置文件中读取[mysqld]以及[server]选项组的内容,也可以通过直接在命令行跟随参数。
  mysqld服务器读取配置文件的顺序,对于有多个配置文件存在,且同一参数有多个值的情形,以最后一次读取为准。
      命令行跟随参数具有最高优先级

  1.1当前mysql服务器运行环境

[root@rhel64a ~]# cat /etc/issue
 Red Hat Enterprise Linux Server release 6.4 (Santiago)
1.1当前mysql服务器运行环境

  1.2查看mysqld启动时的缺省选项

[root@rhel64a ~]# mysqld --print-defaults
  mysqld would have been started with the following arguments:
  --socket=/tmp/mysql3306.sock --port=3306 --pid-file=/var/lib/mysql/my3306.pid --user=mysql --server-id=3306 --federated 
 [root@rhel64a ~]# ps -ef|grep mysql
  root      2963  2840  0 14:10 pts/0    00:00:00 grep mysql
1.2查看mysqld启动时的缺省选项

  1.3查看mysqld启动配置文件的优先级

[root@rhel64a ~]# mysqld --verbose --help |grep -A 1 "Default options"
  Default options are read from the following files in the given order:
  /etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf 
1.3查看mysqld启动配置文件的优先级

  1.4查看当前的my.cnf配置文件

 [root@rhel64a ~]# grep -v ^# /etc/my.cnf|head -n 7
  [mysqld]
  socket = /tmp/mysql3306.sock
  port = 3306
  pid-file = /var/lib/mysql/my3306.pid
  user = mysql
  server-id=3306
  federated
1.4查看当前的my.cnf配置文件

  1.5清空当前mysql服务器的错误日志文件

[root@rhel64a ~]# cat /dev/null>/var/lib/mysql/rhel64a.ycdata.net.err 
  [root@rhel64a ~]# mysqld &           #启动mysqld服务器
  [1] 3480
  [root@rhel64a ~]# 2014-10-25 14:20:42 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. 
  Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
  2014-10-25 14:20:42 3480 [Note] InnoDB: Using atomics to ref count buffer pool pages
  2014-10-25 14:20:42 3480 [Note] InnoDB: The InnoDB memory heap is disabled
  2014-10-25 14:20:42 3480 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
  2014-10-25 14:20:42 3480 [Note] InnoDB: Memory barrier is not used
  2014-10-25 14:20:42 3480 [Note] InnoDB: Compressed tables use zlib 1.2.3
  2014-10-25 14:20:42 3480 [Note] InnoDB: Using Linux native AIO
  2014-10-25 14:20:42 3480 [Note] InnoDB: Using CPU crc32 instructions
  2014-10-25 14:20:42 3480 [Note] InnoDB: Initializing buffer pool, size = 128.0M
  2014-10-25 14:20:42 3480 [Note] InnoDB: Completed initialization of buffer pool
  2014-10-25 14:20:42 3480 [Note] InnoDB: Highest supported file format is Barracuda.
  2014-10-25 14:20:42 3480 [Note] InnoDB: 128 rollback segment(s) are active.
  2014-10-25 14:20:42 3480 [Note] InnoDB: Waiting for purge to start
  2014-10-25 14:20:42 3480 [Note] InnoDB: 5.6.21 started; log sequence number 1626087
  2014-10-25 14:20:42 3480 [Note] Server hostname (bind-address): \'*\'; port: 3306
  2014-10-25 14:20:42 3480 [Note] IPv6 is available.
  2014-10-25 14:20:42 3480 [Note]   - \'::\' resolves to \'::\';
  2014-10-25 14:20:42 3480 [Note] Server socket created on IP: \'::\'.
  2014-10-25 14:20:42 3480 [Note] Event Scheduler: Loaded 0 events
  2014-10-25 14:20:42 3480 [Note] mysqld: ready for connections.
  Version: \'5.6.21\'  socket: \'/tmp/mysql3306.sock\'  port: 3306  MySQL Community Server (GPL)l
1.5清空当前mysql服务器的错误日志文件

  1.6查询mysqld进程

  [root@rhel64a ~]# ps -ef|grep mysql|grep -v grep
  mysql    18240  2641  1 20:25 pts/2    00:00:00 mysqld
  [root@rhel64a ~]# mysql -uroot -pxxx -P3306 --protocol=tcp
  root@localhost[(none)]> show variables like \'version\';
  +---------------+--------+
  | Variable_name | Value  |
  +---------------+--------+
  | version       | 5.6.21 |
  +---------------+--------+
1.6查询mysqld进程

 

2.mysql安全启动(mysqld_safe)

   mysqld_safe是一个shell 脚本,会调用mysqld启动mysql服务器,并监听服务器。
   如果mysqld进程异常终止,mysqld_safe将自动重启mysqld
   mysql_safe 从配置文件中读取[mysqld],[server],[mysqld_safe]等选项,为保证向后兼容,mysql_safe还读取[safe_mysql]选项。

  2.1使用mysqladmin来闭之前启动的mysql服务器

[root@rhel64a ~]# mysqladmin -uroot -pxxx shutdown -S /tmp/mysql3306.sock 
 [root@rhel64a ~]# ps -ef|grep mysql
  root      4262  3418  0 14:40 pts/4    00:00:00 grep mysql
使用mysqladmin关闭之前启动的mysql服务器

  2.2使用mysqld_safe启动实例

[root@rhel64a ~]# mysqld_safe --user=mysql &
  [1] 4273
  [root@rhel64a ~]# 141025 14:40:34 mysqld_safe Logging to \'/var/lib/mysql/rhel64a.ycdata.net.err\'.
  141025 14:40:34 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
使用mysqld_safe启动实例

  2.3  查看启动后的后台进程

[root@rhel64a ~]# ps -ef|grep mysql|grep -v grep #mysqld的进程为4438,其父进程为4273,即mysqld_safe
  root      4273  3418  0 14:40 pts/4    00:00:00 /bin/sh /usr/bin/mysqld_safe --user=mysql
  mysql     4438  4273  0 14:40 pts/4    00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql 
    --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/rhel64a.ycdata.net.err
    --pid-file=/var/lib/mysql/my3306.pid --socket=/tmp/mysql3306.sock --port=3306

[root@rhel64a ~]# mysql -uroot -pxxx -P3306 --protocol=tcp
  root@localhost[(none)]> system kill -9 4438  #强制kill mysqld进程
  root@localhost[(none)]> /usr/bin/mysqld_safe: line 166:  4438 Killed    #下面的nohup为mysql启动进程   
    nohup /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin 
    --user=mysql --log-error=/var/lib/mysql/rhel64a.ycdata.net.err --pid-file=/var/lib/mysql/my3306.pid 
    --socket=/tmp/mysql3306.sock --port=3306 < /dev/null >> /var/lib/mysql/rhel64a.ycdata.net.err 2>&1
  141025 14:42:29 mysqld_safe Number of processes running now: 0
  141025 14:42:29 mysqld_safe mysqld restarted  #提示该进程已经自动重新启动
2.3查看启动后的后台进程

  2.4校验mysqld是否已经成功重启

root@localhost[(none)]> system ps -ef|grep mysql|grep -v grep
  root      4273  3418  0 14:40 pts/4    00:00:00 /bin/sh /usr/bin/mysqld_safe --user=mysql
  root      4517  3418  0 14:41 pts/4    00:00:00 mysql -uroot -px x -P3306 --protocol=tcp
  mysql     4553  4273  0 14:42 pts/4    00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql 
  --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/rhel64a.ycdata.net.err
   --pid-file=/var/lib/mysql/my3306.pid --socket=/tmp/mysql3306.sock --port=3306

root@localhost[(none)]> select databases();
  ERROR 2013 (HY000): Lost connection to MySQL server during query #由于mysqld进程被杀掉,所以连接已经被断开

root@localhost[(none)]> connect  #重新连接
  Connection id:    1
  Current database: *** NONE ***
  
 root@localhost[(none)]> select database();
  +------------+
  | database() |
  +------------+
  | NULL       |
  +------------+
2.4校验mysqld是否已经成功重启


3.mysql(mysql.server)服务方式启动

  mysql.server为一个shell脚本,他会调用mysqld_safe,该脚本会读取配置文件的[mysql.server]以及[mysqld]选项。
  为向后兼容,也会读取[mysql_server]选项

  3.1首先使用mysqladmin关闭已存在的mysqld

root@localhost[(none)]> system mysqladmin -uroot -pxxx shutdown -S /tmp/mysql3306.sock 
  Warning: Using a password on the command line interface can be insecure.
  141025 14:47:18 mysqld_safe mysqld from pid file /var/lib/mysql/my3306.pid ended
  root@localhost[(none)]> exit
  Bye
  [1]+  Done                    mysqld_safe --user=mysql
[root@rhel64a ~]# ls -hltr /etc/init.d/mysql             #/etc/init.d目录下已经配置了mysql启动脚本,在安装时添加
  -rwxr-xr-x 1 root root 11K Sep 11 22:18 /etc/init.d/mysql       #如果为rpm方式安装会自动添加该启动脚本
[root@rhel64a ~]# /etc/init.d/mysql --help               #获取mysql启动脚本的使用帮助
  Usage: mysql  {start|stop|restart|reload|force-reload|status}  [ MySQL server options ]
3.1首先使用mysqladmin关闭已存在的mysqld

  3.2启动或关闭mysqld

[root@rhel64a ~]# /etc/init.d/mysql start                #启动mysqld
  Starting MySQL.[  OK  ]
[root@rhel64a ~]# /etc/init.d/mysql stop                 #停止mysqld 
  Shutting down MySQL..[  OK  ]
3.2启动或关闭mysqld

  3.2缺省情况下mysql已经作为一个服务添加到系统目录已经配置了自启动,如下

[root@rhel64a ~]# chkconfig --list |grep mysql
  mysql           0:off   1:off   2:on    3:on    4:on    5:on    6:off
  
root@localhost[(none)]> system ps -ef|grep mysql |grep -v grep
  root      5127     1  0 14:49 pts/4    00:00:00 /bin/sh /usr/bin/mysqld_safe 
    --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/my3306.pid
  mysql     5306  5127  0 14:49 pts/4    00:00:07 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql
   --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/rhel64a.ycdata.net.err
   --pid-file=/var/lib/mysql/my3306.pid --socket=/tmp/mysql3306.sock --port=3306
  root      5340  3418  0 14:49 pts/4    00:00:00 mysql -uroot -px x -P3306 --protocol=tcp
  
自启动

  #通过服务的方式启动mysql服务器,mysql.server调用mysqld_safe,然后mysql_safe调用了mysqld
    #从上面的方式可知,无论何种启动方式,mysqld最终都会被调用。因为mysqld就是mysql服务器。

4.关闭mysql服务器

青云Xenon管理mysql主从复制测试

MySQL笔记01: MySQL入门_1.3 MySQL启动停止与登录

MySQL数据库应用管理实战

mysql启动关闭与登录

mysql数据库

片段未与片段管理器关联。 (对话片段)