centos安装mysql的三种方式

centos安装mysql的三种方式:
rpm安装、源码 二进制TAR包 又称二进制glibc(无需编译)、源码 TAR包 (要编译)

下面只介绍rpm安装和二进制glibc安装


方法一:配置yum仓库安装(和方法二原理一样,都是rpm安装)

1、下载安装yum仓库
```
yum -y install https://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm
```
2、安装
```
yum -y install mysql-community-server
```
3、启动
```
systemctl start mysqld
```
4、查看状态
```
systemctl status mysqld
```
5、查看初始密码
```
grep 'temporary password' /var/log/mysqld.log
```
6、登录测试(用初始化密码)
```
mysql -uroot -p
```
7、修改root登录密码
```
ALTER USER 'root'@'localhost' IDENTIFIED BY '密码';
flush privileges;
```
8、允许root远程访问
```
update mysql.user set host = '%' where user ='root';
```

方法二:下载rpm包安装

1、下载安装包
官网
[https://downloads.mysql.com/archives/community/](https://downloads.mysql.com/archives/community/)
```
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.20-1.el7.x86_64.rpm-bundle.tar
```
国内镜像站(中国科学技术大学镜像站)
[http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-8.0/](http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-8.0/)
```
wget http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-8.0/mysql-8.0.20-1.el7.x86_64.rpm-bundle.tar
```
2、解压
```
tar -xvf mysql-8.0.20-1.el7.x86_64.rpm-bundle.tar
```
3、安装(各个rpm是有依赖的,所以要按顺序安装)
```
(1)rpm -ivh mysql-community-common-8.0.20-1.el7.x86_64.rpm
(2)rpm -ivh mysql-community-libs-8.0.20-1.el7.x86_64.rpm
如果报错:
error: Failed dependencies:
mariadb-libs is obsoleted by mysql-community-libs-8.0.20-1.el7.x86_64
执行:yum -y remove mysql-libs再继续安装
(3)rpm -ivh mysql-community-libs-8.0.20-1.el7.x86_64.rpm
(4)rpm -ivh mysql-community-server-8.0.20-1.el7.x86_64.rpm
如果报错:
error: Failed dependencies:
libaio.so.1()(64bit) is needed by mysql-community-server-8.0.20-1.el7.x86_64
libaio.so.1(LIBAIO_0.1)(64bit) is needed by mysql-community-server-8.0.20-1.el7.x86_64
libaio.so.1(LIBAIO_0.4)(64bit) is needed by mysql-community-server-8.0.20-1.el7.x86_64
执行:yum -y install libaio再继续安装
```
4、启动
```
systemctl start mysqld
```
5、查看状态
```
systemctl status mysqld
```
6、查看初始密码
```
grep 'temporary password' /var/log/mysqld.log
```
7、登录测试(用初始化密码)
```
mysql -uroot -p
```
8、修改root登录密码
```
ALTER USER 'root'@'localhost' IDENTIFIED BY '密码';
flush privileges;
```
9、允许root远程访问
```
update mysql.user set host = '%' where user ='root';
```

方法三:源码 二进制TAR包 又称二进制glibc 安装

1、下载二进制glibc包
官网
[https://dev.mysql.com/downloads/mysql/](https://dev.mysql.com/downloads/mysql/)(选择对应版本mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz)
```
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz
```
国内镜像站(中国科学技术大学镜像站)
```
wget http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-8.0/mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz
```
2、解压
```
tar -xvf mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz
```
3、将解压目录移动到自定义目录,并改名
```
mv mysql-8.0.21-linux-glibc2.12-x86_64 /usr/local/mysql
```
4、创建用户组和用户
```
groupadd mysql
useradd -r -g mysql mysql
```
5、创建配置文件
```
vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql # 基本路径
datadir=/usr/local/mysql/data #数据路径 (先在/usr/local/mysql建data目录)
socket=/usr/local/mysql/data/mysql.sock # socket文件
symbolic-links=0
log-error=/usr/local/mysql/data/mysqld.log #错误日志
pid-file=/usr/local/mysql/data/mysqld.pid #pid文件
[mysql]
socket=/usr/local/mysql/data/mysql.sock
```
6、配置环境变量
```
vim /etc/profile
export MYSQL_HOME=/usr/local/mysql
export MYSQL_PATH=$MYSQL_HOME/bin
export PATH=$PATH:$MYSQL_PATH

source /etc/profile
```
7、初始化
```
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
报错:
error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
原因:缺少libaio库文件
解决:执行yum install libaio* -y 再重新初始化
```
8、修改属主属组
```
chown -R mysql:mysql /usr/local/mysql
```
9、配置服务管理
```
cd /usr/local/mysql/support-files/
mv mysql.server /etc/init.d/mysqld
```
10、启动,设置密码
因为没有初始密码,用跳过授权表的方式来启动mysql
```
mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables &
```
免密登录
```
mysql -uroot -p 不需要输入密码直接回车
flush privileges;
ALTER USER 'root'@'localhost' IDENTIFIED BY '密码';
```
11、重启验证密码
```
service mysqld restart
service mysqld status
```
12、允许root远程访问
```
mysql -uroot -p

update mysql.user set host = '%' where user ='root';
```

posted @ 2020-09-08 15:24  小明大强  阅读(2623)  评论(0编辑  收藏  举报