### ubantu系统,直接使用apt命令安装
sudo apt install -y mysql-server mysql-client
### 在centos系统 中
### 如果直接使用yum install mysql,会安装mariadb
[root@VM-4-12-centos ~]# yum install mysql
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 mariadb.x86_64.1.5.5.68-1.el7 将被 安装
### 如果自带mariadb,先删除yum remove mariadb
### 到mysql官网查找yum仓库 https://dev.mysql.com/downloads/repo/yum/ 自行查找所需版本
el6 是Red Hat 6.x、CentOS 6.x 的下载
el7 是Red Hat 7.x、CentOS 7.x 的下载
el8 是Red Hat 8.x、CentOS 8.x 的下载
### 这个是centos7的
https://repo.mysql.com//mysql80-community-release-el7-7.noarch.rpm
### 直接wget https://repo.mysql.com//mysql80-community-release-el7-7.noarch.rpm
### 然后yum install mysql80-community-release-el7-7.noarch.rpm -y
### 查看本地仓库
[root@VM-4-12-centos ~]# ls /etc/yum.repos.d/
CentOS-Base.repo mysql-community-debuginfo.repo mysql-community-source.repo
CentOS-Epel.repo mysql-community.repo
### 如果使用yum install mysql-community-server 装的是mysql8.0的
### 开发中常用的是mysql5.7,于是修改/etc/yum.repos.d/mysql-community.repo
### 将5.7中enabled=0修改为enabled=1
### 将8.0中enabled=1修改为enabled=0
### 如果没有5.7,请将下面的代码复制
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
### 再次执行yum install mysql-community-server就可以安装5.7的mysql了
### mysql --version
[root@VM-4-12-centos ~]# mysql --version
mysql Ver 14.14 Distrib 5.7.41, for Linux (x86_64) using EditLine wrapper
### 启动mysql
[root@VM-4-12-centos ~]# systemctl start mysqld
### 其他linux版本中,root密码为空,可以直接登录,但sentos7有一个临时密码
###寻找临时密码
[root@VM-4-12-centos ~]# cat /var/log/mysqld.log | grep "password is"
2023-04-13T07:01:16.287447Z 1 [Note] A temporary password is generated for root@localhost: 4+B0ewolhU*U
### 设置root用户密码,会受密码策略影响,要复杂些
alter user root@localhost identified with mysql_native_password by "密码"
### 密码如果忘记 centos中找到my.cnf文件,其他版本linux类似
加上 skip-grant-tables
### 以下设置root用户密码,不会受密码策略影响
update mysql.user set authentication_string=password("你的密码") where user="root"
### 如果想要在linux输入mysql时有提示,可以下载mycli
pip3 install mycli