centos 7安装mysql

  Seves

1. 下载yum包(两种方式)

  • wget下载
wget 'https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm'

2. 安装软件源

rpm -Uvh mysql57-community-release-el7-11.noarch.rpm

3. 选择安装版本安装

  • 列出版本
$yum repolist all | grep mysql
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community   disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - disabled
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community   disabled
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - disabled
!mysql-connectors-community/x86_64 MySQL Connectors Community    enabled:     63
mysql-connectors-community-source  MySQL Connectors Community -  disabled
!mysql-tools-community/x86_64      MySQL Tools Community         enabled:     69
mysql-tools-community-source       MySQL Tools Community - Sourc disabled
mysql-tools-preview/x86_64         MySQL Tools Preview           disabled
mysql-tools-preview-source         MySQL Tools Preview - Source  disabled
mysql55-community/x86_64           MySQL 5.5 Community Server    disabled
mysql55-community-source           MySQL 5.5 Community Server -  disabled
mysql56-community/x86_64           MySQL 5.6 Community Server    disabled
mysql56-community-source           MySQL 5.6 Community Server -  disabled
!mysql57-community/x86_64          MySQL 5.7 Community Server    enabled:    287
mysql57-community-source           MySQL 5.7 Community Server -  disabled
mysql80-community/x86_64           MySQL 8.0 Community Server    disabled
mysql80-community-source           MySQL 8.0 Community Server -  disabled

如果想安装最新版本的,直接使用 yum 命令即可

yum install mysql-community-server

如果想要安装 5.6 版本的,有2个方法。命令行支持 yum-config-manager 命令的话,可以使用如下命令:

$ sudo dnf config-manager --disable mysql57-community
$ sudo dnf config-manager --enable mysql56-community
$ yum repolist | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community                  36
mysql-tools-community/x86_64      MySQL Tools Community                       47
mysql56-community/x86_64          MySQL 5.6 Community Server                 327

或者直接修改 /etc/yum.repos.d/mysql-community.repo 这个文件

# Enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
enabled=1 #表示当前版本是安装
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=0 #默认这个是 1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

通过设置 enabled 来决定安装哪个版本。
设置好之后使用 yum 安装即可。

4. 启动mysql

systemctl start mysqld.service

5. 查看mysql运行状态

systemctl status mysqld.service

6. 安装之后是有root密码的,但是是随机的, 需要修改

  • 获取mysql临时随机密码
grep 'temporary password' /var/log/mysqld.log


这里的密码是YdsGaxOq>2n!

  • 登录并修改密码
mysql -uroot -pYdsGaxOq>2n!
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';

update user set authentication_string=password('123abc') where user='root';
flush privileges;

7. 授权其他机器登陆

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;

FLUSH  PRIVILEGES;

作者 @没有故事的老大爷
在所有不被想起的快乐里,我最喜欢你

419