MySQL - Linux 安装

54 阅读2分钟

安装

MySQL官网

MySQL官网: www.mysql.com/downloads/

Pasted image 20230831094736.png

Pasted image 20230831095127.png

Pasted image 20240627210425.png

Pasted image 20240627210520.png

Pasted image 20240627210547.png

Pasted image 20240627210604.png

Pasted image 20240627210637.png

上传导Linux系统

Pasted image 20240627211234.png

MySQL压缩文件移动 usr/local/etc

mv mysql-8.3.0-linux-glibc2.28-x86_64.tar.xz /usr/local/etc

解压MySQL 文件夹

tar -xvf mysql-8.3.0-linux-glibc2.28-x86_64.tar.xz

修改mysql文件名

mv mysql-8.3.0-linux-glibc2.28-x86_64 mysql-8.3.0

添加组、用户、设置权限

添加用户组mysql

sudo groupadd mysql

在用户组下添加mysql

sudo useradd -r -g mysql mysql

检查用户组、用户是否存在

groups mysql

配置MySQL配置文件

vi /usr/local/etc/mysql-8.3.0/support-files/mysql.serve
basedir=/usr/local/etc/mysql-8.3.0/
datadir=/usr/local/etc/mysql-8.3.0/data

Pasted image 20240627214837.png

编写mysql配置文件

[mysqld]
basedir=/usr/local/etc/mysql-8.3.0
datadir=/usr/local/etc/mysql-8.3.0/data
port=3306
socket=/tmp/mysql.sock
character_set_server=utf8
lower_case_table_names=1
log-error=/usr/local/etc/mysql-8.3.0/data/mysql.log
pid-file=/usr/local/etc/mysql-8.3.0/data/mysql.pid
[mysql]
default-character-set = utf8

配置环境变量

这个要用管理员模式

# vi etc/profile
sudo vi /etc/profile
# setting MySQL environment
MYSQL_HOME=/usr/local/etc/mysql-8.3.0
PATH=$PATH:$MYSQL_HOME/bin
export MYSQL_HOME PATH

Pasted image 20240627215138.png

重新加载

source etc/profile

启动MySQL

初始化MySQL

mysqld --initialize --console

Pasted image 20240627215337.png

初始密码为:fjusXTH6QZ%&

登录MySQL

mysql -u root -p 
root@lavm-4uhrzatn01:~# mysql -u root -p 
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.37-0ubuntu0.22.04.3 (Ubuntu) Copyright (c) 2000, 2024, 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>

修改密码

# 修改MySQL密码
set password for root@localhost='123456'
# 退出
exit
# 重新登录
mysql -u root -p 

Pasted image 20240629102145.png

常用的MySQL操作

  • 查看MySQL运行日志sudo journalctl -u mysql.service
  • 重新启动MySQL服务sudo service mysql restart
  • 停止MySQL服务sudo service mysql stop
  • 启动MySQL服务sudo service mysql start
  • 查询MySQL状态sudo service mysql status
  • 在系统启动时自动启动:sudo systemctl enable mysql

修改访问问题

修改配置文件

# 编辑MySQL配置文件 
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf 
# 修改bind-address配置 
# bind-address = 127.0.0.1 
bind-address = 0.0.0.0
# 重启MySQL服务 
sudo systemctl restart mysql
update user set host='%' where user='root'
flush privileges

Pasted image 20240629104500.png

修改防火墙端口,放开3306 端口

sudo firewall-cmd --permanent --add-port=3306/tcp
sudo firewall-cmd --reload
flush privileges

Pasted image 20240629104643.png

引用

csdn引用