1、安装依赖
sudo apt update
sudo apt -y dist-upgrade
sudo apt -y install vim net-tools wget gcc make cmake lrzsz
sudo apt -y install libmecab2 libjson-perl
2、下载rpm文件
https://dev.mysql.com/downloads/mysql/
https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-server_8.0.40-1ubuntu22.04_amd64.deb-bundle.tar
3、执行命令安装
安装依赖包
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配置 MySQL
安装过程中会提示设置 MySQL root 用户的密码。根据提示完成安装。
sudo apt-get install -fsudo dpkg -i mysql-{common,community-client,community-client-core,community-client-plugins,client,community-server,community-server-core,server}_*.deb
select host , user , plugin ,authentication_string from mysql.user;ALTER USER 'root'@'localhost' IDENTIFIED BY 'lihaozhe';
flush privileges;ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'lihaozhe';
flush privileges;update mysql.user set host = '%',plugin='mysql_native_password' where user='root';
flush privileges;quit;sudo systemctl restart mysql
4、安装目录如下
主配置文件:/etc/mysql/my.cnf 或 /etc/mysql/mysql.conf.d/mysqld.cnf
说明:主配置文件中可以修改 datadir、bind-address、skip-name-resolve 等参数。数据文件路径
数据目录:/var/lib/mysql
说明:存储所有数据库的数据文件,需确保 mysql:mysql 拥有该目录的读写权限。日志文件路径
错误日志:/var/log/mysql/error.log
说明:记录 MySQL 启动、运行中的错误信息以及其他日志内容。服务与启动路径
Systemd 服务文件:/lib/systemd/system/mysql.service
说明:通过以下命令管理 MySQL 服务:bash
复制代码
sudo systemctl start mysql # 启动服务
sudo systemctl stop mysql # 停止服务
sudo systemctl restart mysql # 重启服务
sudo systemctl status mysql # 查看服务状态客户端工具路径
MySQL 命令行工具:
/usr/bin/mysql
/usr/bin/mysqldump
说明:使用 mysql 连接数据库,使用 mysqldump 导出数据库。其他相关文件
PID 文件:
/var/run/mysqld/mysqld.pid
Socket 文件:
/var/run/mysqld/mysqld.sock
5、 配置外网访问
MySQL 默认会尝试通过主机名解析客户端的 IP 地址,可能导致网络访问速度变慢。通过禁用主机名解析可以解决这个问题:编辑 `/etc/mysql/mysql.conf.d/mysqld.cnf` 文件,添加以下内容:
[mysqld]
skip-name-resolve
创建外网访问用户
CREATE USER 'root'@'%' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
6、常用命令
重启
sudo systemctl restart mysqlsudo systemctl stop mysqlsudo systemctl start mysqlsudo systemctl enable mysql