Linux源码安装MySQL

切换阿里镜像源

1.备份系统源

1
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
1
curl -o /etc/yum.repos.d/CentOS-Base.repo [http://mirrors.aliyun.com/repo/Centos-7.repo](http://mirrors.aliyun.com/repo/Centos-7.repo)

2.更新缓存

1
2
3
yum clean
yum makecache
yum update

yum安装

通过上面切换源,yum带有MySQL5.7,直接使用如下命令即可

1
yum install -y mysql

启动服务

1
systemctl start mysqld

开机启动

1
systemctl enable mysqld

查看默认密码

mysql5.7之后安装时,默认分配了一个临时密码

1
grep 'temporary password' /var/log/mysqld.log

修改默认密码

1
mysql -u root -p '默认密码'
1
alter user 'root'@'localhost' IDENTIFIED BY 'youPassword';

rpm源安装

添加MySQL5.7镜像源

1
sudo rpm -ivh https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

验证添加结果

1
sudo yum repolist all | grep mysql | grep enabled
1
2
3
mysql-connectors-community/x86_64  MySQL Connectors Community    enabled:     51
mysql-tools-community/x86_64 MySQL Tools Community enabled: 63
mysql57-community/x86_64 MySQL 5.7 Community Server enabled: 267

开始安装

1
sudo yum -y install mysql-community-server

管理MySQL

1.启动MySQL

1
sudo systemctl start mysqld

2.开机启动

1
sudo systemctl enable mysqld

3.查看运行状态

1
sudo systemctl status mysqld

4.重启MySQL

1
sudo systemctl restart mysqld

MySQL配置

1.默认配置文件

1
sudo vim /etc/my.cnf

2.默认root密码修改

1
2
cat /var/log/mysqld.log | grep -i 'temporary password'
mysql_secure_installation // 根据该命令提示设置root密码,该命令会禁止匿名用户登录