企业一般使用MySQL二进制包安装MySQL8.0

另外还有yum/apt、源码、Shell、容器化安装(Docker一键安装)

安装步骤

1.安装依赖环境

(1)Centos7安装依赖

yum install -y libaio libaio-devel ncurses ncurses-devel openssl openssl-devel

(2)Ubuntu22.04 安装依赖

apt update

apt install -y libaio1 libaio-dev libtinfo5 libncurses-dev libncurses5 libncurses5-dev libncursesw5 libncursesw5-dev libssl-dev

2.下载二进制包

(1)官网下载

wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz

(2)局域网内下载

wget

http://192.168.56.200/Software/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz

3.设置安装路径与权限

(1)解压

tar -xvf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz

(2)将mysql文件夹剪切并改名

mv mysql-8.0.20-linux-glibc2.12-x86_64 /usr/local/mysql

(3)创建mysql用户

useradd mysql -M -s /sbin/nologin

(4)创建数据库数据目录

mkdir /usr/local/mysql/data

(5)更改mysql目录权限

chown mysql:mysql -R /usr/local/mysql

4.修改配置与初始化

(1)修改MySQL配置文件

cat << EOF > /etc/my.cnf

[mysqld]

port = 3306

basedir = /usr/local/mysql

datadir = /usr/local/mysql/data

log-error = /usr/local/mysql/data/error.log

pid-file = /usr/local/mysql/data/mysql.pid

socket = /tmp/mysql.sock

character-set-server=utf8mb4

collation-server=utf8mb4_unicode_ci

default-storage-engine=InnoDB

default-authentication-plugin=mysql_native_password

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

EOF

(2)执行初始化

/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

(3)执行完后查看初始密码:

cat /usr/local/mysql/data/error.log

显示结果如下:

2025-08-01T01:59:17.918526Z 0 [Warning] [MY-010915] [Server] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.

2025-08-01T01:59:17.918708Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.20) initializing of server in progress as process 1361

2025-08-01T01:59:17.955395Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.

2025-08-01T01:59:18.606916Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.

2025-08-01T01:59:19.405704Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 5wp7=Y3<TZp;(4)初始化成功后,启动MySQL

/usr/local/mysql/support-files/mysql.server start

显示结果如下:

Starting MySQL.. SUCCESS!

(5)启动MySQl本质执行的是mysqld命令

ps -aux|grep mysqld

netstat -lnutp|grep 3306

显示结果如下:

tcp6    0   0 :::33060 :::* LISTEN   1672/mysqld

tcp6    0   0 :::3306 :::* LISTEN   1672/mysqld     

5.设置环境变量

(1)修改系统环境变量

想要在任意目录执行mysql的命令需要修改/etc/profile

PATH: 可执行命令的目录路径

vim /etc/profile

(2)最后一行添加指定PATH关联MySQL的bin目录

export PATH=$PATH:/usr/local/mysql/bin

(3)配置完成后让系统重新加载/etc/profie文件

source /etc/profile

6.修改密码

(1)使用你的初始密码登录MySQL

mysql -uroot -p

Enter password:5wp7=Y3(初始密码)

注意:在输入密码时,密码是不会显示了,你正确输入即可。

(2)修改密码

alter user 'root'@'localhost' identified by 'root123';

显示为Query OK, 0 rows affected (0.05 sec)则成功

(3)退出

exit:

7.设置开机启动

(1)将启动脚本复制到/etc/init.d/目录,这是启动脚本的默认目录。

cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

(2)配置mysqld开机启动后,可以使用systemctl管理mysqld

systemctl enable mysqld

systemctl restart mysqld

systemctl status mysqld

8.设置远程登录(先关闭防火墙)

(1)本地登录MySQL

mysql -uroot -proot123

用户 密码

(2)创建远程登录账号

mysql> create user 'root'@'%' identified with mysql_native_password by 'root123';

显示结果如下:

Query OK, 0 rows affected (0.01 sec)

(3)设置开放权限

mysql> grant all on *.* to 'root'@'%';

显示结果如下:

Query OK, 0 rows affected (0.00 sec)

(4)刷新使权限生效

mysql> flush privileges;

显示结果如下:

Query OK, 0 rows affected (0.00 sec)

遇到问题:

Can’t connect to MySQL server on ‘192.168.8.20’ (61)

解决方法:

关闭防火墙

systemctl stop firewalld.service

firewall-cmd --state

(客户端-命令行)
①mysql:官方的客服端

centos7上安装mariadb

yum install mariadb

在Ubuntu上下载mysql

apt install mysql-client

mysql -uroot -proot123 -h192.168.8.20

②mycli:Python实现的mysql客户端,拥有关键字提示功能。

在Ubuntu上安装mycli

apt install mycli

CentOS7上安装mycli

yum install python3-pip

pip3 install mycli==1.10

pip3 install pymysql==0.8

mycli -uroot -proot123 -h“192.168.8.20”

(客户端-图形化)

phpmyadmin

Navicat

SQLyou

Workbench

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐