1、本次测试使用mysql8.0.29。辅助工具使用navcat16。navcat12以下的版本在连接mysql时会存在密码加密方式与mysql不一致而导致的登录错误,所以推荐使用navcat15以上的版本。

     主机和从机安装mysql8.0.29
     如果安装后用navcat连接报错:1251
     解决办法:1.打开cmd命令窗口,输入命令 mysql -uroot -p 打开MySQL数据库,然后输入密码进行登录。
                     2.执行 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
                     3.执行 FLUSH PRIVILEGES;
                     4.使用数据库工具修改root的host为%,以便跨服务器访问。
                     5.执行 FLUSH PRIVILEGES;

2、修改主机的mysql配置文件。如何在主机查找mysql.ini文件(windows下) ?可以通过mysql的客户端来查找文件的存放地址。在查询分析器执行 :show variables like '%data%'

      例如我本机的my.ini文件位置:d:\ProgramData\MySQL\MySQL Server 8.0

3、修改主机my.ini
    server-id=1
    log-bin=mysql-bin
    #目标数据库
    binlog_do_db=rp-cloud   

4、然后重启主机的mysql80服务。最好重启一下主机。

5、使用mysql自带的数据库工具在主库创建用户:

grant system_user on *.* to 'root';//给root账户需要先授权
FLUSH PRIVILEGES;

create user 'salve'@'192.168.20.46' identified by '123456';
GRANT ALL PRIVILEGES ON *.* TO 'salve'@'192.168.20.46'  ;//给全部权限
FLUSH PRIVILEGES;

//修改连接的密码方式:
update mysql.user set plugin='mysql_native_password' where user='salve';
FLUSH PRIVILEGES;

然后重启主机的mysql80服务


6、在从机上创建从库:rp-cloud  
在从机用navcat16连接一下主库,用 'salve'账户,看能不能连接。能连接后面的操作才能成功。

7、然后编辑从数据库的 my.ini 文件,在 [mysqld] 节点中增加如下内容:
server-id=2 #指定唯一的ID,2至32,必须的,并且不能跟主数据库一样
replicate-do-db=rp-cloud #指定要同步的数据库,必须的
replicate-ignore-db=mysql #指定不要同步的数据库。


我的从库是win7下安装的,修改my.ini之后服务无法启动。
将my.ini文件另存一下,编码格式改回ANSI编码格式,覆盖掉原来的老my.ini文件。
然后重启主机的mysql80服务
用navcat16连接一下主服务器,通了


8、查询显示主服务器的状态信息,并且找到File 和 Position 的值记录下来;

show master status;

+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      324 | rpa-cloud    |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
     
9、重启从数据库,设置登录主数据库的账号和密码等信息,然后在从机的mysql控制台启动slave
mysql>change master to master_host='192.168.20.23',master_port=3306,master_user='salve',master_password='123456', master_log_file='mysql-bin.000001',master_log_pos=324;

master_host指向主机地址,
master_port指向主机端口
master_user是主库里面添加的只允许从库访问的用户
master_password是密码


10、然后看看slave的运行状态:show slave status \G;

*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.20.23
                  Master_User: salve
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000008
          Read_Master_Log_Pos: 157
               Relay_Log_File: li-PC-relay-bin.000012
                Relay_Log_Pos: 326
        Relay_Master_Log_File: mysql-bin.000008
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: rpa-cloud
          Replicate_Ignore_DB: mysql
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 157
              Relay_Log_Space: 885
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
                  Master_UUID: 0a3a7222-fcf4-11ec-8bdd-70b5e860d6d3
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica has read all relay log; waiting for more
updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
       Master_public_key_path:
        Get_master_public_key: 0
            Network_Namespace:
1 row in set, 1 warning (0.00 sec)

如上,如果
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
说明配置成功。否则配置就是失败的。配置成功后可以在主库添加一个测试表格,看看从库是否发生了同样的变化。


11、修改若依中数据库的配置为读写分离,如下面截取的spring配置,在yaml里面修改spring配置。如果是微服务版的若依,则需要在nacos中修改yaml配置。注意缩进和格式。

# spring配置
spring:
  redis:
    host: localhost
    port: 6379
    password: 123456
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    # url: jdbc:mysql://localhost:3306/rp-cloud?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
    # username: root
    # password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      # 主库数据源
      master:
        url: jdbc:mysql://localhost:3306/rp-cloud?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
        username: root
        password: 123456
      # 从库数据源
      slave:
        # 从数据源开关/默认关闭
        enabled: true
        url: jdbc:mysql://192.168.20.46:3306/rp-cloud?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
        username: root
        password: 123456
      # 初始连接数
      initialSize: 5
      # 最小连接池数量
      minIdle: 10
      # 最大连接池数量,太小容易卡死
      maxActive: 2000
      # 配置获取连接等待超时的时间
      maxWait: 60000
      # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      timeBetweenEvictionRunsMillis: 60000
      # 配置一个连接在池中最小生存的时间,单位是毫秒
      minEvictableIdleTimeMillis: 300000
      # 配置一个连接在池中最大生存的时间,单位是毫秒
      maxEvictableIdleTimeMillis: 900000
      # 配置检测连接是否有效
      validationQuery: SELECT 1 FROM DUAL
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      webStatFilter:
        enabled: true
      statViewServlet:
        enabled: true
        # 设置白名单,不填则允许所有访问
        allow:
        url-pattern: /druid/*
        # 控制台管理用户名和密码
        login-username: ruoyi
        login-password: 123456
      filter:
        stat:
          enabled: true
          # 慢SQL记录
          log-slow-sql: true
          slow-sql-millis: 1000
          merge-sql: true
        wall:
          config:
            multi-statement-allow: true
  servlet:
    multipart:
      enabled: true
      location: D:/rpatempdir
      max-file-size: 1000MB
      max-request-size: 1000MB
      file-size-threshold: 0
      resolve-lazily: false
   

    

Logo

快速构建 Web 应用程序

更多推荐