千家信息网

Linux二进制包安装Mysql8主从复制

发表于:2025-01-29 作者:千家信息网编辑
千家信息网最后更新 2025年01月29日,1、下载安装包wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.18-el7-x86_64.tar.gz2、删除系统自带rpm -qa
千家信息网最后更新 2025年01月29日Linux二进制包安装Mysql8主从复制

1、下载安装包

wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.18-el7-x86_64.tar.gz

2、删除系统自带

rpm -qa |grep  mysqlrpm -qa |grep mariadbrpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64

3、修改配置文件

cat /etc/my.cnf[mysql]default-character-set=utf8[mysqld]skip-name-resolveport = 3306basedir=/usr/local/mysqldatadir=/usr/local/mysql/datamax_connections=200character-set-server=utf8default-storage-engine=INNODBlower_case_table_names=1max_allowed_packet=16Mlog-bin=master-bin           #master是master-bin,slave的是slave-binserver-id=1           #master为1,slave2gtid_mode=ONenforce_gtid_consistency=ONbinlog_checksum=NONE

4、初始化数据库

groupadd mysqluseradd -r -g mysql -s /bin/false mysqlcd /usr/localtar xvf mysql-8.0.18-el7-x86_64.tar.gzln -s mysql-8.0.18-el7-x86_64 mysqlcd mysqlmkdir mysql-fileschown mysql:mysql mysql-fileschmod 750 mysql-filesbin/mysqld --initialize --user=mysqlbin/mysql_ssl_rsa_setupbin/mysqld_safe --user=mysql &cp support-files/mysql.server /etc/init.d/mysql.server

记住数据库的随机密码

#关闭命令

bin/mysqladmin -u root -p shutdown

#启动命令

bin/mysqld_safe --user=mysql &


5、设置环境变量:

cat  /etc/profile.d/msyqlMYSQL_HOME=/usr/local/mysql/export PATH=$PATH:$MYSQL_HOME/bin

6、修改随机密码:

mysql -u root -p #输入初始化随机生成的密码

ALTER USER 'root'@'localhost' IDENTIFIED BY 'hello123';
show master status; #查看master状态

7、为主从复制创建用户

CREATE USER 'econage'@'%' IDENTIFIED BY 'hello123';

GRANT REPLICATION SLAVE ON *.* TO econage@'%';


#mysql8 之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是caching_sha2_password,把mysql用户登录密码加密规则还原成mysql_native_password. 不然会报错如下

#error connecting to master 'econage@192.168.0.56:3306' - retry-time: 60 retries: 1 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.


ALTER USER 'econage'@'%' IDENTIFIED WITH mysql_native_password BY 'hello123'; #添加这一条命令即可

FLUSH PRIVILEGES;

8、查看连接是否正确:

mysql -ueconage -phello123 -h292.168.0.56 -e 'status;'

9、在slave进行连接master

CHANGE MASTER TO
MASTER_HOST='192.168.0.56', #master主机ip
MASTER_USER='econage', #上面创建的用户
MASTER_PASSWORD='hello123', #用户密码
MASTER_LOG_FILE='master-bin.000002', #master的file文件名
MASTER_LOG_POS=474; # master的Position号

查看

show slave status\G #可能会状态不对,执行下面命令
stop slave;
start slave;

显示两个yes则正确。

10、验证

#分别在master和slave查看

show databases;

#在master去创建一个库

create database hello

#会发现slave上也有一个hello的库

0