当前位置: 首页 > article >正文

/etc/init.d/mysql

Since you’ve installed MySQL from source, you’ll need to create a custom init script to manage the MySQL server (start, stop, status) similarly to a service. Here’s a simple init.d script template for MySQL that you can use. This script assumes MySQL is installed in /usr/local/mysql and uses the mysql user.

1. Create the Init Script

Create a new script file in /etc/init.d/:

sudo vim /etc/init.d/mysql

2. Copy the Script Template

Paste the following content into the file:

#!/bin/bash
### BEGIN INIT INFO
# Provides:          mysql
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: MySQL Server
# Description:       MySQL Server
### END INIT INFO

# MySQL installation directory
basedir=/usr/local/mysql
# MySQL data directory
datadir=/usr/local/mysql/data
# MySQL binary path
mysqld=${basedir}/bin/mysqld
mysqladmin=${basedir}/bin/mysqladmin
# MySQL user
mysql_user=mysql
# MySQL root user for shutdown (update with correct root password)
mysql_root_password='root_password'

# Process ID file location
pidfile=${datadir}/mysqld.pid
# Log file
logfile=${datadir}/mysql.log

start_mysql() {
    echo "Starting MySQL server..."
    sudo -u $mysql_user $mysqld --user=$mysql_user --pid-file=$pidfile --log-error=$logfile --explicit_defaults_for_timestamp &
    echo "MySQL started."
}

stop_mysql() {
    echo "Stopping MySQL server..."
    sudo -u $mysql_user $mysqladmin -u root -p"$mysql_root_password" shutdown
    echo "MySQL stopped."
}

status_mysql() {
    if [ -f $pidfile ]; then
        echo "MySQL is running (PID: $(cat $pidfile))."
    else
        echo "MySQL is not running."
    fi
}

case "$1" in
    start)
        start_mysql
        ;;
    stop)
        stop_mysql
        ;;
    status)
        status_mysql
        ;;
    restart)
        stop_mysql
        start_mysql
        ;;
    *)
        echo "Usage: /etc/init.d/mysql {start|stop|status|restart}"
        exit 1
        ;;
esac

exit 0

3. Adjust Configuration

  • MySQL Root Password:
    Update the mysql_root_password variable with the actual root password of your MySQL installation. If you don’t want to hardcode the password, you can be prompted for it or use sudo without password, but it’s less convenient.

  • Paths:
    Ensure the basedir and datadir paths are correct. Adjust them if your MySQL installation or data directory is in a different location.

4. Set Permissions

Make the script executable:

sudo chmod +x /etc/init.d/mysql

5. Enable the Script

To enable the script to start at boot, use the following command:

sudo update-rc.d mysql defaults

6. Managing MySQL

Now you can manage MySQL using the init script:

  • Start MySQL:

    sudo /etc/init.d/mysql start
    
  • Stop MySQL:

    sudo /etc/init.d/mysql stop
    
  • Check MySQL status:

    sudo /etc/init.d/mysql status
    
  • Restart MySQL:

    sudo /etc/init.d/mysql restart
    

This script should work for basic management of your MySQL server.

When the initial root password for MySQL is expired, you’ll need to change it before you can perform any other operations. Here are the steps to update the expired password:

1. Log In to MySQL with the Expired Password

Use the --connect-expired-password option to log in as root with the expired password:

/usr/local/mysql/bin/mysql -u root -p --connect-expired-password

Enter the expired password ,wmdGF>ju3!d when prompted.

2. Change the Root Password

After logging in, change the password using the ALTER USER statement:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewStrongPassword';

Replace 'NewStrongPassword' with your new desired password. Make sure it meets MySQL’s password policy requirements (minimum length, mix of letters, numbers, special characters, etc.).

3. Flush Privileges

If required, you can flush the privileges to ensure the changes take effect immediately:

FLUSH PRIVILEGES;

4. Exit MySQL

Exit the MySQL command-line interface:

EXIT;

5. Verify the New Password

Log in again using the new password to verify that it has been updated successfully:

/usr/local/mysql/bin/mysql -u root -p

Enter your new password when prompted.

This process should update your expired MySQL root password successfully.


http://www.kler.cn/a/319962.html

相关文章:

  • SpringBoot+Vue小区智享物业管理系统(高质量源码,可定制,提供文档,免费部署到本地)
  • 【Git 】探索 Git 的魔法——git am 与补丁文件的故事
  • 【C++篇】红黑树的实现
  • QT 如何禁止QComboBox鼠标滚轮
  • 单片机存储器和C程序编译过程
  • 本地部署Web-Check网站检测与分析利器并实现远程访问实时监测
  • 将上一篇的feign接口抽取到公共api模块(包含feign接口示例)
  • 如何在 macOS 上恢复未保存的 Excel 文件 – 文件恢复的最佳方法
  • 运维工程师面试整理-安全常见安全漏洞及修复
  • 31214324
  • 【延时队列的实现方式】
  • Leetcode 1396. 设计地铁系统
  • CentOS 7 aarch64制作openssh 9.9p1 rpm包 —— 筑梦之路
  • 如何在数据分析中处理异常?
  • 模块化编程实战:光敏传感器控制蜂鸣器(江科大stm32练习)
  • 【C#】 EventWaitHandle的用法
  • EchoMimic模型部署教程
  • Swagger 教程(笔记) Knife4j
  • 怎么利用PHP发送彩信
  • layui如何获取当前地址栏的某个参数
  • String类常用的方法
  • linux命令之docker用法
  • uni-app安装插件
  • Rust 运算符快速了解
  • Uniapp时间戳转时间显示/时间格式
  • 每日OJ题_牛客_ 腐烂的苹果_多源BFS_C++_Java