mysql主从配置读写分离笔记

第二次回头看了,第一次学完后感觉都会了,回头再看才发现什么都想不起来了。还得查资料再学习,虽然很简单。还是做个笔记吧!笔记有点糙 就是自己看的


因为主要是测试主从和读写分离 所以直接 yum install -y mariadb mariadb-server  用的是centos 7.4 用光盘搭建的yum源。

启动mysql systemctl start mariadb systemctl enable mariadb 开机自动启动 

1、运行mysql_secure_installation 设置root密码,然后一直点yyyyyy.......

2、登陆mysql -uroot -p123456

3、创建个用户 grant all on *.* to '51cto'@'%' identified by '51cto';  创建为51cto密码为51cti且可以在任意电脑上登陆且对数据库拥有所有权限。

4、使用命令show grants for 51cto@%  可以查看用户权限  但是测试了下 好像是不正确 可能是没使用好。


MariaDB [(none)]> flush privileges;

Query OK, 0 rows affected (0.00 sec)

使用此命令刷新,这样就设置完账户了,开始配置主服务,其实很简单就是打开日志功能 并且设置个server-id=1即可  如下图;

image.png

重启mysql

image.png

然后使用 flush tables with read lock;命令来对所有数据库的表执行只读锁定,然后使用show master status;查看状态,并且记下File 和Position。

除了解锁 unlock tables;后就只剩下配置从服务器了

从mysql配置 也是一样 打开vim、etc/my.cnf/

image.png

很简单  配置server-id=2 和主mysql不能相同  然后设置read-only=on 意思是只能读取数据不能写入数据 读写分离(不是能明白);本文中注释了;

然后重启mysql即可

下面使用change master to 命令设置

change master to master_host='主服务器的地址',(逗号)master_user='51cto(设置的用户名)',master_passowrd='51cto(密码)',master_log_file='zabbix_bin.000001',master_log_pos=410443;最后两个是从数据库上面的截图。

启动slave start slave  show slave status|\G 查看状态。

MariaDB [(none)]> show slave status\G

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 10.91.10.2

                  Master_User: zabbix2

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: zabbix_bin.000001

          Read_Master_Log_Pos: 410443

               Relay_Log_File: mariadb-relay-bin.000002

                Relay_Log_Pos: 322253

        Relay_Master_Log_File: zabbix_bin.000001

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB: 

          Replicate_Ignore_DB: 

           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: 410443

              Relay_Log_Space: 322549

              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

1 row in set (0.00 sec)

最后就是这个这样的效果,然后在主服务器上解锁 unlock tables;

到此完成。 接下来打算使用keepalived 切换主备mysql 不用使用人工切换了。

猜你喜欢

转载自blog.51cto.com/12529218/2126471