一、Oracle官网下载对应Mysql的版本:
点击下载Mysql8.0
如图:
![图片[1] - Liunx 系统安装 Mysql 8.0+ - 宋马](https://pic.songma.com/blogimg/20250508/94914573b22c4d4eb05a66563b7d3ab8.png)
二、详细步骤
上传到服务器目录中 : cd /usr/local,解压tar.xz:
tar -xvf mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz
解压后,命名为 mysql:
mv mysql-8.0.26-linux-glibc2.12-x86_64 /usr/local/mysql
切换至 /usr/local/mysql 目录下,创建一个数据文件目录 data(用于所有的数据库存储数据目录)
cd /usr/local/mysql
mkdir data
创建mysql用户组和mysql用户
groupadd mysql
useradd -g mysql mysql
给用户赋予mysql目录权限
chown -R mysql.mysql /usr/local/mysql/
或者
sudo chown -R mysql.mysql /usr/local/mysql/
数据库初始化
切换至/usr/local/mysql 目录下,执行一下初始化命令
./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize

生成初始化的密码,需要记录下来,后续登录需要!!!
新增my.cnf文件
vim /etc/my.cnf
或
vi /etc/my.cnf
新增配置以下内容:
[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
socket = /usr/local/mysql/mysql.sock
character-set-server=utf8
port = 3306
#严格性和行为规则
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE
# 1:创建的数据库名和表名转换为小写存储,查询时不区分大小写(Linux 默认为0 区分大小写(按创建时的名称严格匹配))
lower_case_table_names=1
[client]
socket = /usr/local/mysql/mysql.sock
default-character-set=utf8
#[mysqld]
#datadir=/var/lib/mysql
#socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
#[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
#!includedir /etc/my.cnf.d
注意:当前 my.cnf中没有配置 lower_case_table_names=1 ;
如果需要配置,需要清空数据库数据后才可以添加(建议先备份好数据),要把 /usr/local/mysql/data 目录清除后再进行
步骤6的操作:数据库初始化
设置my.cnf权限:
# 664 是权限的数字表示,对应以下权限:
# 6(所有者 owner):读(r) + 写(w) ;
# 6(所属组 group):读(r) + 写(w) ;
# 4(其他用户 others):只读(r)
sudo chmod 664 /etc/my.cnf
将mysqld服务添加到系统中
切换至:cd /usr/local/mysql
cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
配置环境变量
vi /etc/profile
export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib
export PATH
设置环境变量立即生效:
source /etc/profile
启动mysql服务,查看是否启动成功。
#启动命令:
service mysql start
#状态命令:
service mysql status
[root ~]# service mysql start
Redirecting to /bin/systemctl start mysql.service
[root ~]# service mysql status
Redirecting to /bin/systemctl status mysql.service
● mysqld.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
Active: active (running) since Sun 2022-01-16 17:17:55 CST; 8s ago
Docs: man:systemd-sysv-generator(8)
Process: 27231 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/mysqld.service
├─27242 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/VM-0-4-centos.pid
└─27408 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=VM-0-4-cent...
Jan 16 17:17:54 VM-0-4-centos systemd[1]: Starting LSB: start and stop MySQL...
Jan 16 17:17:54 VM-0-4-centos mysqld[27231]: Starting MySQL.Logging to '/usr/local/mysql/data/VM-0-4-centos.err'.
Jan 16 17:17:55 VM-0-4-centos mysqld[27231]: SUCCESS!
Jan 16 17:17:55 VM-0-4-centos systemd[1]: Started LSB: start and stop MySQL.
登陆mysql 并且修改密码为 123456789
登录:mysql -uroot -p
设置密码:ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘123456789’;
[root ~]# mysql -uroot -p
Enter password: 输入初始密码
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 13
Server version: 8.0.26
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> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456789';
Query OK, 0 rows affected (0.01 sec)
mysql>
设置mysql远程登录后可使用客户端进行连接
1)切换数据库 use mysql;
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
2)修改mysql库中host值,所有ip都能访问到root用户(生产环境建议不要使用,除非公司同意)
update user set host=‘%’ where user=‘root’ limit 1
mysql> update user set host='%' where user='root' limit 1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
-- 以上操作可能存在安全隐患,生产环境建议使用指定ip连接。 允许 168.168.8.200 使用 root 用户连接
CREATE USER 'root'@'168.168.8.200' IDENTIFIED BY '123456789';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'168.168.8.200' WITH GRANT OPTION;
FLUSH PRIVILEGES;
3)刷新mysql权限使其生效
flush privileges;
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
防火墙单独开放 3306端口
#开放服务端口:
firewall-cmd --zone=public --add-port=3306/tcp --permanent
#查询端口号3306是否开启:
firewall-cmd --query-port=3306/tcp
#重启防火墙:
firewall-cmd --reload
#查看firewall防火墙状态
systemctl status firewalld
#查看firewall防火墙开放端口
firewall-cmd --list-ports
#关闭防火墙命令
systemctl stop firewalld.service
#禁止firewall开机启动
systemctl disable firewalld.service














暂无评论内容