务实派丶 發表於 2025-5-1 16:38:00

Linux系统搭建单机MySQL8.0.26版本

<h2 id="概述">概述</h2>
<p>本文主要是写Ubuntu22.04搭建MySQL8.0.26版本</p>
<h2 id="环境信息">环境信息</h2>
<table>
<thead>
<tr>
<th>IP</th>
<th>系统</th>
<th>规格</th>
</tr>
</thead>
<tbody>
<tr>
<td>10.0.0.10</td>
<td>Ubuntu22.04</td>
<td>2c4g</td>
</tr>
</tbody>
</table>
<h2 id="数据库服务安装步骤">数据库服务安装步骤</h2>
<h3 id="下载前置依赖">下载前置依赖</h3>
<pre><code># 下载libtinfo5、libnuma1依赖
# apt update -y &amp;&amp; apt install -y libtinfo5 libnuma1
</code></pre>
<h3 id="服务下载">服务下载</h3>
<h4 id="方式一进入官网下载并上传到宿主机中适合离线环境">方式一:进入官网下载,并上传到宿主机中,适合离线环境</h4>
<p>官网下载地址: https://downloads.mysql.com/archives/community/<br>
<img src="https://img2024.cnblogs.com/blog/3468887/202505/3468887-20250501153744113-1900835310.png" alt="image" loading="lazy"></p>
<h4 id="方式二直接在宿主机中使用wget进行下载适合宿主机联网环境">方式二:直接在宿主机中使用wget进行下载,适合宿主机联网环境</h4>
<pre><code>wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz
</code></pre>
<h3 id="服务解压并创建软链接">服务解压并创建软链接</h3>
<pre><code># 服务解压至家目录
# tar -xvf mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz

# 创建软链接
# ln -s mysql-8.0.26-linux-glibc2.12-x86_64 mysql

# 查看
# ll | grep mysql
lrwxrwxrwx1 root root      35 May1 15:43 mysql -&gt; mysql-8.0.26-linux-glibc2.12-x86_64/
drwxr-xr-x9 root root      4096 May1 15:43 mysql-8.0.26-linux-glibc2.12-x86_64/
-rw-r--r--1 root root 914806904 Jul22021 mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz
</code></pre>
<h3 id="配置环境变量">配置环境变量</h3>
<pre><code># /root/mysql/bin根据你安装的实际地址来进行替换
# echo 'export PATH=/root/mysql/bin:$PATH' &gt;&gt; /etc/profile
# source /etc/profile
</code></pre>
<h3 id="检查环境变量是否配置正确">检查环境变量是否配置正确</h3>
<pre><code># mysql -V
mysqlVer 8.0.26 for Linux on x86_64 (MySQL Community Server - GPL)
</code></pre>
<h3 id="创建mysql的虚拟用户">创建MySQL的虚拟用户</h3>
<pre><code># useradd -s /sbin/nologin -M mysql
</code></pre>
<h3 id="创建数据存储目录并修改目录的拥有者">创建数据存储目录,并修改目录的拥有者</h3>
<pre><code># mkdir -p /data/mysql/data
# chown -R mysql.mysql /data/mysql/data
</code></pre>
<h3 id="初始化数据库">初始化数据库</h3>
<pre><code># 初始化数据库,没有报错即代表成功
# mysqld --initialize-insecure--user=mysql--datadir=/data/mysql/data--basedir=/root/mysql
2025-05-01T07:59:04.638615Z 0 /root/mysql-8.0.26-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.26) initializing of server in progress as process 132486
2025-05-01T07:59:04.656989Z 1 InnoDB initialization has started.
2025-05-01T07:59:05.254538Z 1 InnoDB initialization has ended.
2025-05-01T07:59:05.950015Z 0 A deprecated TLS version TLSv1 is enabled for channel mysql_main
2025-05-01T07:59:05.950635Z 0 A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
2025-05-01T07:59:06.009584Z 6 root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
</code></pre>
<p>参数解释:</p>
<ul>
<li>--initialize-insecure:不安全的初始化,表示数据库启动后没有密码信息,</li>
<li>--initialize:安全初始化,表示数据库启动后,会有默认的密码信息</li>
<li>--user:指定用户</li>
<li>--datadir:指定数据存储目录</li>
<li>--basedir:指定MySQL安装的目录</li>
</ul>
<p>数据库初始化成功之后数据目录会有以下文件</p>
<pre><code># ll /data/mysql/data/
total 177608
-rw-r----- 1 mysql mysql   196608 May1 15:59 '#ib_16384_0.dblwr'
-rw-r----- 1 mysql mysql8585216 May1 15:59 '#ib_16384_1.dblwr'
drwxr-x--- 2 mysql mysql   4096 May1 15:59 '#innodb_temp'/
drwxr-xr-x 6 mysql mysql   4096 May1 15:59./
drwxr-xr-x 3 rootroot      4096 May1 15:56../
-rw-r----- 1 mysql mysql       56 May1 15:59auto.cnf
-rw------- 1 mysql mysql   1676 May1 15:59ca-key.pem
-rw-r--r-- 1 mysql mysql   1112 May1 15:59ca.pem
-rw-r--r-- 1 mysql mysql   1112 May1 15:59client-cert.pem
-rw------- 1 mysql mysql   1680 May1 15:59client-key.pem
-rw-r----- 1 mysql mysql   5995 May1 15:59ib_buffer_pool
-rw-r----- 1 mysql mysql 50331648 May1 15:59ib_logfile0
-rw-r----- 1 mysql mysql 50331648 May1 15:59ib_logfile1
-rw-r----- 1 mysql mysql 12582912 May1 15:59ibdata1
drwxr-x--- 2 mysql mysql   4096 May1 15:59mysql/
-rw-r----- 1 mysql mysql 26214400 May1 15:59mysql.ibd
drwxr-x--- 2 mysql mysql   4096 May1 15:59performance_schema/
-rw------- 1 mysql mysql   1676 May1 15:59private_key.pem
-rw-r--r-- 1 mysql mysql      452 May1 15:59public_key.pem
-rw-r--r-- 1 mysql mysql   1112 May1 15:59server-cert.pem
-rw------- 1 mysql mysql   1680 May1 15:59server-key.pem
drwxr-x--- 2 mysql mysql   4096 May1 15:59sys/
-rw-r----- 1 mysql mysql 16777216 May1 15:59undo_001
-rw-r----- 1 mysql mysql 16777216 May1 15:59undo_002
</code></pre>
<h3 id="编写mysql配置文件">编写MySQL配置文件</h3>
<pre><code># cat /etc/my.cnf

# 数据库文件的存储路径
datadir=/data/mysql/data
# 服务器监听的端口号
port=3306
# 字符集设置
character-set-server=utf8mb4
# 排序规则
collation-server=utf8mb4_general_ci
# 允许的最大连接数
max_connections=1000
# MySQL服务器的缓存大小,用于缓存数据和索引
innodb_buffer_pool_size=1G
# 日志文件的路径
log_error=/var/log/mysql/error.log
# 慢查询日志文件的路径
slow_query_log_file=/var/log/mysql/slow-query.log
# 开启慢查询日志,1表示开启,0表示关闭
slow_query_log=1
# 设定慢查询的时间阈值,单位为秒,超过此时间的查询将被记录到慢查询日志中
long_query_time=3
# 设置用户
user=mysql
# 设置socket连信息
socket=/tmp/mysql.sock


# 客户端默认字符集
default-character-set=utf8mb4


# MySQL命令行工具的默认字符集
default-character-set=utf8mb4

</code></pre>
<h3 id="创建日志目录">创建日志目录</h3>
<pre><code># mkdir -p /var/log/mysql/
# chown -R mysql.mysql /var/log/mysql/
</code></pre>
<h3 id="修改mysql安装目录中的启动文件">修改MySQL安装目录中的启动文件</h3>
<p>拷贝启动文件至<code>/etc/init.d</code>目录下</p>
<pre><code># cp /root/mysql/support-files/mysql.server /etc/init.d/mysqld
# chmod +x /etc/init.d/mysqld
</code></pre>
<p>修改启动文件的内容<br>
<code>vim /etc/init.d/mysqld</code><br>
<img src="https://img2024.cnblogs.com/blog/3468887/202505/3468887-20250501161112813-2003052276.png" alt="image" loading="lazy"></p>
<h3 id="启动mysql服务">启动MySQL服务</h3>
<pre><code># 没报错即代表启动成功
# /etc/init.d/mysqld start
Starting mysqld (via systemctl): mysqld.service.

# 检查端口号
# ss -lntup | grep 3306
tcp   LISTEN 0      70               *:33060            *:*    users:(("mysqld",pid=144896,fd=21))
tcp   LISTEN 0      1000               *:3306             *:*    users:(("mysqld",pid=144896,fd=24))
</code></pre>
<h3 id="修改root用户密码">修改root用户密码</h3>
<pre><code># mysqladmin -u root password "huangsir"
mysqladmin: Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
</code></pre>
<h3 id="登录mysql服务">登录MySQL服务</h3>
<pre><code># mysql -uroot -phuangsir
mysql: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.26 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql&gt;
</code></pre>
<h2 id="远程连接mysql">远程连接MySQL</h2>
<p>MySQL默认的root用户是无法远程登录的,所以我们需要创建一个用户用于远程登录</p>
<pre><code># 创建用户,10.0.0.0/24代表网段,只有在这个网段之内的IP才能连接数据库,密码设置为huangsir
mysql&gt; CREATE USER 'root'@'10.0.0.0/24' IDENTIFIED BY 'huangsir';
Query OK, 0 rows affected (0.01 sec)

# 授权,*.*代表所有的库和表
mysql&gt; GRANT ALL PRIVILEGES ON *.* TO 'root'@'10.0.0.0/24' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

# 刷新权限
mysql&gt; FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
</code></pre>
<p>使用navicat连接数据库测试<br>
<img src="https://img2024.cnblogs.com/blog/3468887/202505/3468887-20250501163705181-585631185.png" alt="image" loading="lazy"></p>
<h2 id="补充配置mysql-systemd配置文件">补充:配置mysql systemd配置文件</h2>
<pre><code>vim /usr/lib/systemd/system/mysqld.service

Description=MySQL Server
Documentation=mysqld.service
After=network.target
After=syslog.target


WantedBy=multi-user.target


User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000

# 加载配置文件
systemctl daemon-reload

# 启动
systemctl start mysql
# 停止
systemctl stop mysql
</code></pre>


</div>
<div id="MySignature" role="contentinfo">
    <p>本文来自博客园,作者:huangSir-devops,转载请注明原文链接:https://www.cnblogs.com/huangSir-devops/p/18848445,微信Vac6666666,欢迎交流</p><br><br>
来源:https://www.cnblogs.com/huangSir-devops/p/18848445
頁: [1]
查看完整版本: Linux系统搭建单机MySQL8.0.26版本