MySQL5.7.19安装及主从复制构架配置

这段时间单位安排了一个项目,需求方要求不能用Oracle,必须用开源的MySQL。这可真出了个难题,主要是我不用MySQL好多年。单位dba又是Oracle方向的而且在歇产假,所以只能自己上阵了。

闲话少叙,进入正题。

一、准备工作

先去官网上找mysql的安装包,
https://www.mysql.com/ → DOWNLOADS → Community → MySQL Community Server → MySQL Community Server 5.7.19

我的操作系统是64位CentOS 7.3
在Select Operating System:里面没有这个系统,所以直接选的Red Hat Enterprise Liunx / Oracle Linux
在Select OS Version:里选择了Red Hat Enterprise Liunx 7 / Oracle Linux 7 (x86, 64-bit)

然后下载RPM Bundle(mysql-5.7.19-1.el7.x86_64.rpm-bundle.tar),大小是565.4M

二、安装MySQL5.7.19

下载完成后解压缩:tar -xvf mysql-5.7.19-1.el7.x86_64.rpm-bundle.tar
能够解压出12个包:
mysql-community-client-5.7.19-1.el7.x86_64.rpm
mysql-community-common-5.7.19-1.el7.x86_64.rpm
mysql-community-devel-5.7.19-1.el7.x86_64.rpm
mysql-community-embedded-5.7.19-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.19-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.19-1.el7.x86_64.rpm
mysql-community-libs-5.7.19-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.19-1.el7.x86_64.rpm
mysql-community-minimal-debuginfo-5.7.19-1.el7.x86_64.rpm
mysql-community-server-5.7.19-1.el7.x86_64.rpm
mysql-community-server-minimal-5.7.19-1.el7.x86_64.rpm
mysql-community-test-5.7.19-1.el7.x86_64.rpm

安装新版mysql之前,我们需要将系统自带的mariadb-lib卸载
执行 rpm -qa|grep mariadb 查看系统内安装的MariaDB的数据库
mariadb-libs-5.5.44-2.el7.centos.x86_64
卸载 rpm -e mariadb-libs-5.5.44-2.el7.centos.x86_64 --nodeps

然后按照下面的顺序逐一安装MySQL:
rpm -ivh mysql-community-common-5.7.19-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.19-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.19-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.19-1.el7.x86_64.rpm

在安装server包的时候,系统报错:

error: Failed dependencies:
libaio.so.1()(64bit) is needed by mysql-community-server-5.7.19-1.el7.x86_64
libaio.so.1(LIBAIO_0.1)(64bit) is needed by mysql-community-server-5.7.19-1.el7.x86_64
libaio.so.1(LIBAIO_0.4)(64bit) is needed by mysql-community-server-5.7.19-1.el7.x86_64
我安装了 yum install libaio 包问题解决。

error: Failed dependencies:
/usr/bin/perl is needed by mysql-community-server-5.7.19-1.el7.x86_64
perl(Getopt::Long) is needed by mysql-community-server-5.7.19-1.el7.x86_64
perl(strict) is needed by mysql-community-server-5.7.19-1.el7.x86_64
我安装了 yum install perl 包问题解决。

安装完成后初始化数据库:
执行 mysqld --initialize
执行后会在/var/log/mysqld.log里生成随机密码:A temporary password is generated for root@localhost: 6fQPWsSNw0-=
记住这个初始化密码,首次登陆mysql数据库要用到的。

更改mysql数据库目录的所属用户及其所属组
chown mysql:mysql /var/lib/mysql -R

启动mysql数据库
systemctl start mysqld.service

登陆mysql数据库
mysql -uroot -p'前面在mysqld.log里获取到的初始密码'
登陆成功后修改密码:mysql > set password=password('pwd_123456');
然后修改用户允许访问数据库的IP和过期时间:
mysql > use mysql;
mysql > update user set host='%', password_expired='N' where host='localhost'; // 我在这里配置的是允许任何IP访问,并且账户永不过期。
mysql > SELECT `Host`, `User`, password_expired FROM `user`;
mysql > flush privileges;
mysql > exit;

至此MySQL5.7.19已经安装完成,通过Navicat for MySQL链接正常。

三、搭建MySQL主从复制

先说一下测试环境:
主数据库的ip地址:172.20.1.139
从数据库的ip地址:172.20.1.141
确保主数据库与从数据库一模一样。
使用root用户分别在主数据库和从数据库上创建好库“test_db”,并且在“test_db”库里创建结构完全一样的表“t_user”。
如果创建表的时候报错(插入、修改时也可能会报这个错)
[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause; this is incompatiblewith sql_mode=only_full_group_by
解决办法是在 /etc/my.cnf 里加上下面这行配置:
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/972d169ad5dfbbee9898b32e83f3d13f.html