Centos7 安装Mysql8 主从数据库

前提条件

准备了两台虚拟机 mysql-master 192.168.30.199, mysql-slave 192.168.30.198

 

1:官网下载并安装Mysql8

  

1:安装mysql

    1.1:官网下载mysql的安装包,并解压到/usr/local/mysql目录下。
1.1.1:解压xz包,xz -d mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz(解压成tar包)
1.1.2:解压tar包,tar -xvf mysql-8.0.27-linux-glibc2.12-x86_64.tar -C /usr/local
1.1.3:将数据移到mysql目录 mv ./mysql-8.0.27-linux-glibc2.12-x86_64/* ./mysql
1.1.4: 在mysql目录下创建data目录,mkdir data
1.2:创建mysql用户,并修改/usr/local/mysql目录的权限
1.2.1:创建mysql用户,useradd mysql
1.2.2:修改mysql用户的密码,passwd mysql
1.2.3:修改/usr/local/mysql目录的所属用户 chown -R mysql:mysql mysql
1.3:编辑配置文件/etc/my.cnf
[root@localhost local]# cat /etc/my.cnf
[mysqld]
port=3306 #3306端口
basedir=/usr/local/mysql # mysql的安装目录
datadir=/usr/local/mysql/data # mysql数据库的数据的存放目录
max_connections=10000 # 允许最大连接数
max_connect_errors=10 # 允许连接失败的次数,防止有人从该主机试图攻击数据库系统
character-set-server=utf8 # 服务端使用的字符集
default-storage-engine=INNODB # 默认存储引擎

[mysql]
default-character-set=utf8 # 设置mysql客户端默认字符集
1.4:切换到MySQL用户,进入/usr/local/mysql/bin目录启动服务
./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --lower_case_table_names=1
1.4.1: ./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize
1.4.2: 保存初始密码
登录mysql,修改root用户密码:
./bin/mysql -uroot -p
alter user 'root'@'localhost' identified by 'huang1314';
CREATE USER 'root'@'%' IDENTIFIED with mysql_native_password BY 'huang1314';
grant all privileges on *.* to 'root'@'%' with grant option;
flush privileges;
1.5:将mysql添加到服务 开机自启,进入/usr/local/mysql/support-files进行设置
1.5.1:cp -a mysql.server /etc/init.d/mysqld
1.5.2:chmod +x /etc/init.d/mysqld
1.5.3:chkconfig --add mysqld
1.6:配置环境变量
1.6.1:echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
1.6.2:source /etc/profile
1.7:启动mysql
1.7.1:启动命令 servie mysqld start|status|stop|restart
1.7.2:修改root密码。ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Hyx@20220527';
1.7.4:show databases;
1.7.5:use mysql;
1.7.3:update user set host='%' where user = 'root'; ---设置root用户可在任意主机登录,即可远程登录

2:根据上述步骤,分别先安装好mysql。现在开始配置master配置。
  2.1:主库安装在192.168.30.199机器,这时需要修改主库的/etc/my.cnf配置文件。具体配置如下,指定service-id,启动bin-log日志
[mysql]
# 默认字符集
default-character-set=utf8mb4

[client]
port=3306
socket=/tmp/mysql.sock

[mysqld]
skip-name-resolve
port=3306
basedir=/usr/local/mysql  # mysql的安装目录
datadir=/usr/local/mysql/data # mysql数据库的数据的存放目录
max_connections=10000   # 允许最大连接数
max_connect_errors=10   # 允许连接失败的次数,防止有人从该主机试图攻击数据库系统
default-storage-engine=INNODB   # 默认存储引擎
lower_case_table_names=1

#开启日志文件
# 设置server_id,注意要唯一
server-id=101
binlog-format=ROW # 选择 ROW 模式
log-bin=mysql-mater-bin
# relay_log配置中继日志
relay_log=edu-mysql-relay-bin
# 永久设置时区时间
default-time_zone = '+8:00'
#指定要同步的数据库,多个用逗号隔开(可以不用配置)
binlog_do_db=study_db


sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

#socket=/var/lib/mysql/mysql.sock
socket=/tmp/mysql.sock
collation_server=utf8mb4_general_ci
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
character-set-server=utf8mb4 # 服务端使用的字符集
skip-name-resolve
log-error=/usr/local/mysql/data/error.log
pid-file=/usr/local/mysql/data/mysql.pid
default-character-set=utf8   # 设置mysql客户端默认字符集
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
View Code

  2.2:重启数据库 service mysqld restart,查询并验证service_id是否存在。

-- 查询server_id是否存在
show variables like '%server_id%';
-- 查询同步的文件和行数,如下查询的数据在从服务器上执行命令会用到。
show master status;

 

       2.3:   连接数据库,创建用来同步数据的用户syncadm,并授权。

-- 创建用户
create user 'syncadm'@'%' IDENTIFIED by 'syncadm';
-- 授权
grant REPLICATION CLIENT ON *.* TO syncadm;
grant REPLICATION SLAVE ON *.* TO syncadm;
grant SUPER ON *.* TO syncadm;
grant reload on *.* to syncadm;
-- 刷新权限
FLUSH PRIVILEGES;

    2.4:执行show master status;查看当前二进制日志名称和偏移量。记录file和position的值。

3:修改从库slave的/etc/my.cnf文件,

  3.1:   

  # 设置server_id,注意要唯一
  server-id=102

       # 如果有可能作为主库,这里也可以开启binlog
  log-bin=mysql-slave-bin
  # relay_log配置中继日志(同步日志)
  relay_log=edu-mysql-relay-bin

  3.2:使用命令配置从机

-- 停止同步
STOP SLAVE;
-- 重置同步
reset slave;
-- 配置从机
CHANGE MASTER TO master_host = '192.168.30.199',
   master_user = 'syncadm',
   master_password = 'syncadm',
   master_port = 3306,
   master_log_file = 'mysql-mater-bin.000009',
   master_log_pos = 30778,
   master_connect_retry = 30,
   get_master_public_key = 1;
-- 开启同步
start slave;
-- 查看同步状态
show slave status;

4: 验证,在主库建立表,增删改查数据,看是否同步成功。

 




posted @ 2022-08-02 17:26  低调小雄  阅读(368)  评论(0编辑  收藏  举报