MySQL 8.0 安装与配置技术文档
目录
- 环境准备
- 下载 MySQL 安装包
- 检查是否已安装 MySQL
- 彻底卸载 MySQL
- 安装 MySQL
- 配置 MySQL
- 创建用户并允许外网访问
- 修改 root 用户密码
- 参考链接
1. 环境准备
确保系统为 Ubuntu 22.04,并安装了以下基础工具:
sudo apt-get update
sudo apt-get install wget tar dpkg
2. 下载 MySQL 安装包
从官网获取安装包:
- 下载地址: MySQL 8.0 安装包
- MySQL 官方网站: https://dev.mysql.com/downloads/mysql/
下载并解压:
wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-server_8.0.40-1ubuntu22.04_amd64.deb-bundle.tar
mkdir -p ~/mysql-deb
tar -xvf mysql-server_8.0.40-1ubuntu22.04_amd64.deb-bundle.tar -C ~/mysql-deb
cd ~/mysql-deb
3. 检查是否已安装 MySQL
检查系统是否存在 MySQL:
dpkg -l | grep mysql
ps -ef | grep mysql
ls /etc/mysql
ls /var/lib/mysql
4. 彻底卸载 MySQL
如果已安装 MySQL,可以按以下步骤卸载:
-
停止 MySQL 服务:
sudo systemctl stop mysql
-
卸载所有相关包:
sudo dpkg --purge mysql-server mysql-client mysql-common mysql-community-server sudo apt-get autoremove --purge
-
清理残余文件:
sudo rm -rf /etc/mysql /var/lib/mysql /var/log/mysql /var/run/mysqld
-
再次确认卸载完成:
dpkg -l | grep mysql
5. 安装 MySQL
安装步骤:
sudo dpkg -i mysql-common_8.0.40-1ubuntu22.04_amd64.deb
sudo dpkg -i libmysqlclient21_8.0.40-1ubuntu22.04_amd64.deb
sudo dpkg -i libmysqlclient-dev_8.0.40-1ubuntu22.04_amd64.deb
sudo dpkg -i mysql-community-client-core_8.0.40-1ubuntu22.04_amd64.deb
sudo dpkg -i mysql-community-client_8.0.40-1ubuntu22.04_amd64.deb
sudo dpkg -i mysql-client_8.0.40-1ubuntu22.04_amd64.deb
sudo dpkg -i mysql-community-server-core_8.0.40-1ubuntu22.04_amd64.deb
sudo dpkg -i mysql-community-server_8.0.40-1ubuntu22.04_amd64.deb
sudo dpkg -i mysql-server_8.0.40-1ubuntu22.04_amd64.deb
6. 配置 MySQL
编辑 MySQL 配置文件:
sudo nano /etc/mysql/my.cnf
添加以下内容,优化网络连接:
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
skip-name-resolve
保存后重启 MySQL:
sudo systemctl restart mysql
7. 创建用户并允许外网访问
进入 MySQL 控制台:
mysql -u root -p
运行以下命令:
CREATE USER 'root'@'%' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
8. 修改 root 用户密码
在 MySQL 控制台中,运行以下命令:
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
ALTER USER 'root'@'%' IDENTIFIED BY '新密码';
FLUSH PRIVILEGES;
如果忘记密码,可按以下步骤重置:
-
停止 MySQL:
sudo systemctl stop mysql
-
以跳过权限表模式启动:
sudo mysqld_safe --skip-grant-tables &
-
登录 MySQL:
mysql -u root
-
重置密码:
UPDATE mysql.user SET authentication_string = PASSWORD('新密码') WHERE User = 'root'; FLUSH PRIVILEGES;
-
重启 MySQL:
sudo systemctl start mysql
9. 参考链接
- MySQL 官方下载页面
- MySQL 8.0 安装包下载地址