MySQL Installation Guide

This article documents two ways to install MySQL, with detailed steps for Linux and Windows operating systems.

Contents

Installing MySQL from Generic Binary Files

Demonstration OS: Debian-based Linux distribution

Note: On systems without special requirements, you can install directly from a bundle to save time.

1. Download the Binary Files to the Server

To save time, Steps 1 and 2 can be performed simultaneously.

wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz

Note: Do not download the wrong architecture, or you may encounter an execution error
bash: /usr/install/mysql/bin/mysqld: cannot execute: required file not found

2. Install dependencies

As described in document https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html\
Generally, you need to install three dependencies: libaio, libnuma, and libncurses5

# 全程使用root用户 谨慎操作
su root 
# 或者 sudo su
# 使用apt包管理器 安装命令
apt isntall -y libaio1 libnuma-dev libncurses5
# 如果mysql8 只需要安装这两个
apt isntall -y libaio1 libncurses6

3. Extract the downloaded MySQL file and place it in an appropriate location

# 解压
tar -xvf mysql*.gz
# 删除压缩包
rm mysql*.gz
# 重命名
mv mysql.... mysql
# 移动到合适的位置
mv ./mysql /usr/local
# 切换到目录位置
cd /usr/local/mysql

4. Create a Configuration File

vim /usr/local/mysql/my.cnf

#文件内容:

[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port=3306
character-set-server=utf8mb4

5. Initialize the Database

# 创建mysql用户和组
groupadd mysql
# 参数解释: 系统账户,没有控制台交互
useradd -r -g mysql -s /bin/false mysql 

bin/mysqld --defaults-file=/usr/local/mysql/my.cnf --initialize --user=mysql 
(如果不是root用户,可能需要sudo来处理权限问题)

After execution completes, the console displays the default password for the root user. Be sure to save it.

2024-02-23T07:15:03.027122Z 1 [Note] A temporary password is generated for root@localhost: nh+v8EX&F6_m

6. Start the MySQL service

# 创建mysql.service文件
vim /etc/systemd/system/mysql.service

# 写入以下内容
[Unit]
Description=MySQL Server
After=network.target

[Service]
ExecStart=/usr/local/mysql/bin/mysqld
User=mysql
Group=mysql
Type=simple
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target

# 刷新服务列表
systemctl daemon-reload
# 启动mysql服务
systemctl start mysql
# 开机自启动
systemctl enable mysql

7. Test the Link and Change the Password

# 添加环境变量
echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile 
# 应用环境变量(后续打开新的终端不再需要,root用户除外)
source /etc/profile
# 连接mysql
mysql -u root -p
# 修改密码 123456 根据自己需要更改
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
# 新装mysql可能不允许外网访问,需要手动设置
update mysql.user set host='%' where user='root';
flush privileges;
# exit 退出再次使用新密码登录,安装至此结束

MySQL Bundle File Installation

https://dev.mysql.com/downloads/mysql/

Download the installation package

<img src="/api/file/eb192a382b08435bb9b68f921af18efc_0.7.jpg" alt="image-20240814172050499" style="zoom:50%;" />
# 下载
wget https://dev.mysql.com/get/Downloads/MySQL-8.4/mysql-server_8.4.2-1debian12_amd64.deb-bundle.tar
# 解压
tar -xvf *bundle.tar
# 安装  这个过程会报错
dpkg -i *.deb

# 修复缺少的依赖,并继续安装 
apt --fix-broken install 
# 如果弹出设置密码,即安装成功

systemctl status mysql

Installing Windows Binary Files

Installation via MSI is very simple, so it won’t be covered here. If the MSI installer works, don’t make things difficult for yourself. Time is money~

When you install MySQL on Windows using the command line, you may encounter an issue where the service cannot start.

One possible cause is the “MYSQL_HOME” environment variable, though this has not been verified. Installation via MSI may fail in some cases.

Confirmed: You need to configure the environment variable; otherwise, the MySQL path configured in the service may be incorrect, causing startup to fail.

After investigation, it turned out that the path to the executable in the created service was incorrect. Changing it to the correct path resolves the issue.

# regedit
\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\

Installation Command Reference (Simple Version)

  1. Configure Environment Variables

    MYSQL_HOME
    
  2. Run the installation command (with administrator privileges)

    # 推荐无密码,默认密码太复杂也记不住,都是要改,不设置密码更方便,省去查找密码的步骤
    mysqld --initialize-insecure (无默认密码)
    mysqld --initialize --console (有默认密码)
    
  3. Install the MySQL service

    mysqld install
    
  4. Start the MySQL service

    net start mysql
    

Installation complete. Test the connection?