Home Database Mysql Tutorial mysql随Linux开机自动启动_MySQL

mysql随Linux开机自动启动_MySQL

Jun 01, 2016 pm 02:02 PM
linux mysql

  在MySQL的管理过程中,偶尔会遇到一些PC Server宕机或者重启,这时我需要在主机启动后再将MySQL服务启动。一般情况下,这项工作都是简单的。但是,当面临上百台或者更多的MySQL主机的时候,这种“偶尔”可能会很多,这种“偶尔”还会在半夜或者凌晨发生,如果每次都手动操作,这是很繁琐的。更重要的是,如果因此而打断了凌晨的美梦是不值得的。

  要实现随开启自动启动mysqld,我们需要搞定如下几个问题:

  1. Linux开机自动启动脚本放在哪儿?

  一般的,作为服务器使用的Linux一般会以“完全多用户模式(Multi-User Mode with Networking)”级别来启动,这种情况下Linux在启动时会运行/etc/rc.d/rc3.d/下的全部脚本。例如我们在这个目录下会看到脚本”/etc/rc.d/rc3.d/S90crond”,意味着开机启动时会运行S90crond脚本。

  2. Linux如何运行这些脚本?

  既然已经知道自动启动脚本该放在哪儿了,一切就好办。我们只需要将一个启动MySQL的脚本放过去就好了。下面是我们的一个简单的启动脚本v0.1 mysqldauto

  $vi mysqldauto

  #!/bin/sh

  # Version: 0.1 by orczhou@gmail.com

  /opt/mysql/bin/mysqld_safe --user=mysql &      #这里需要修改为你的mysqld_safe目录

  $chmod +x mysqldauto

  $mv mysqldauto /etc/rc.d/init.d/

  $ln -s /etc/rc.d/init.d/mysqldauto /etc/rc.d/rc3.d/S99mysqld

  这样我们就把创建的mysqldauto脚本放到了/etc/rc.d/rc3.d/下面(注意这里使用了link的方式),mysqld可以自动启动了。

  这有两个问题需要解释:

  * * * * * 为什么不直接在目录/etc/rc.d/rc3.d/下创建文件,而要创建一个软连接?这并不是必须的。但是这样做是有很多好处的(后面会解释),不过这样做至少会看起来更加专业。

  * * * * * 为什么文件名要用S99mysqld?这是规则,在rc3.d下面的脚本如果以字母S开头,那么执行时Linux会给它传递一个start参数(如果以字母K开头,则会传递stop参数);S后面接个数字,表示了这个脚本的启动顺序,如果目录rc3.d下还有S98*那么它会在S99之前运行。(看到这儿,你可以猜测一下前面我们为什么要创建软连接了)

  好了,自此你的mysqld就已经可以随Linux开机自动启动了。

  3. 怎样做更专业些?

  上面这样做,已经可以解决问题了,不过写法多少有些“山寨”,下面我们看看怎样改造一下。

  * * * * * 改造1:处理参数start。既然前面提到以字母S开头的脚本会自动传递一个参数start,K则会传递stop。那么我再做如下修改:

  $vi mysqldauto

  #!/bin/sh

  # Version: 0.2 by orczhou@gmail.com

  MYSQLHOME=/opt/mysql                         #这里需要修改为你的mysql安装目录

  if [ $# -ge 1 ];then

  mysqldProc=`ps -ef|grep -E "mysqld.+safe"|grep -v "grep"|wc -l`

  if [ $1 = "stop" ] ;then

  if [ $mysqldProc -eq 1 ];then

  $MYSQLHOME/bin/mysqladmin -uroot shutdown

  fi

  elif [ $1 = "start" ];then

  if [ $mysqldProc -eq 0 ];then

  $MYSQLHOME/bin/mysqld_safe --user=mysql &

  fi

  fi

  fi

  做了如此改造后,我们脚本需要接收两个参数start stop了。如果你想让你mysqld在关闭主机的时候自动关闭,那么stop参数就可以起作用了:

  $ln -s /etc/rc.d/init.d/mysqldauto /etc/rc.d/rc0.d/K20mysqld

  这里做软连接好处就体现出来了,启动和关闭只需要用一个脚本就可以了。

  * * * * * 改造2:当你面临几十上百台主机的时候,MySQL的启动参数可能会不一样,例如备库启动时可能需要mysqld_safe –user=mysql –read_olny=1 &,这种情况怎么办呢?这里提供一个解决思路。

  在主机上运行一个脚本,探测当前mysqld的启动参数,然后写到一个指定的文件里。最后,在你的启动脚本中,读取这个文件里面的启动参数来启动mysqld。It works。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

How to safely store JavaScript objects containing functions and regular expressions to a database and restore? How to safely store JavaScript objects containing functions and regular expressions to a database and restore? Apr 19, 2025 pm 11:09 PM

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

See all articles