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

/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.logstart_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" shutdownecho "MySQL stopped."
}status_mysql() {if [ -f $pidfile ]; thenecho "MySQL is running (PID: $(cat $pidfile))."elseecho "MySQL is not running."fi
}case "$1" instart)start_mysql;;stop)stop_mysql;;status)status_mysql;;restart)stop_mysqlstart_mysql;;*)echo "Usage: /etc/init.d/mysql {start|stop|status|restart}"exit 1;;
esacexit 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.lryc.cn/news/451419.html

相关文章:

  • Qt_线程介绍与使用
  • 通讯方面的数据,人工智能 机器学习的时候,因为数字都接近于一,数据归一化的一种方法,做了一个简化版本的Z-score标准化
  • python itertools模块介绍
  • 【分布式微服务云原生】5分钟深入剖析Kafka:Leader与Follower分区的秘密及负载均衡的艺术
  • 在线代码编辑器
  • 深入了解 MPlayer:Linux 系统中的多功能多媒体播放器
  • Netty系列-7 Netty编解码器
  • OpenHarmony标准系统上实现对rk系列芯片NPU的支持(npu使用)
  • 大表性能优化的关键技术
  • 广联达 Linkworks办公OA Service.asmx接口存在信息泄露漏洞
  • 如何成为成功的AI产品经理:经验与策略分享
  • spring loCDI 详解
  • 遇到 Docker 镜像拉取失败的问题时该如何解决
  • 【C/C++】错题记录(三)
  • 深入理解Web浏览器与服务器的连接过程
  • 深入解析 https
  • NP-hard问题
  • 【Nacos架构 原理】内核设计之Nacos通信通道
  • 【单片机】单片机map表详细解析
  • 考研笔记之操作系统(三)- 存储管理
  • vim/vi常用命令大全
  • 什么是大语言模型,一句话解释
  • 【数据库】 MongoDB 撤销用户的角色和权限
  • vue2接入高德地图实现折线绘制、起始点标记和轨迹打点的完整功能(提供Gitee源码)
  • 【重学 MySQL】四十六、创建表的方式
  • WPS在表格中填写材料时,内容过多导致表格不换页,其余内容无法正常显示 以及 内容过多,导致表格换页——解决方法
  • 计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-01
  • 第一弹:C++ 的基本知识概述
  • 在职场,没人告诉你的人情世故
  • 激光切割机适用材质有哪些