mysql主从的搭建

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xxzhaobb/article/details/82830876

初步接触学习Mysql,查找文档,搭建了个主从,具体原理和参数还在琢磨中。

mysql 主从的搭建
下载MySQL安装文件,这里下载的是个压缩包,解压后就可以用。
因为资源有限,在一台Windows下搭建主从。目录为D:\mysql\mysql[1-2]

-- 主库的my.ini参数(通过那个default复制出来)

# 以下内容手动添加
[client]
port=3306
default-character-set=utf8
[mysqld]
#主库配置
server_id=1
log_bin=master-bin
log_bin-index=master-bin.index

#端口
port=3306
character_set_server=utf8
#解压目录
basedir=D:\mysql\mysql1
#解压目录下data目录
datadir=D:\mysql\mysql1\data

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[WinMySQLAdmin]
D:\mysql\mysql1\binbin\mysqld.exe

-- 创建主库mysql服务

mysqld --install master --defaults-file="D:\mysql\mysql1\my.ini"

-- 登录主库

mysql -uroot -p   -- 敲回车 ,没有密码
use mysql;
update user set password=password("mysql") where user="root";
flush privileges;

------- 从库的安装与设置

-- 从库的my.ini配置文件

# 以下内容手动添加
[client]
port=3307
default-character-set=utf8
[mysqld]
#从库配置
server_id=2
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin
#端口
port=3307
character_set_server=utf8
#解压目录
basedir=D:\mysql\mysql2
#解压目录下data目录
datadir=D:\mysql\mysql2\data

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[WinMySQLAdmin]
D:\mysql\mysql2\bin\mysqld.exe

-- 安装从库服务

mysqld --install slave --defaults-file="D:\mysql\mysql2\my.ini"

-- 修改从库的root密码

use mysql;
update  user set password=password("mysql") where user="root";
flush privileges;

-- 以上的配置后,主库从库的配置完毕了。剩下的是关联主库从库

-- 查看master的状态

mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |      503 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql>

-- 查看slave的状态

mysql> show slave status;
Empty set (0.00 sec)

mysql>

-- 通过以上查看,在master的状态下,生成了一个二进制的文件,而slave下是空的。因此需要把主库与从库关联起来。只需要让从库知道主库的地址就可以了 。
-- 在主库创建一个用户,用于与从库同步,并授权,用户名test ,密码mysql
-- 在主库执行

create user test;
update  user set password=password("mysql") where user="test";  -- 
grant replication slave on *.* to '[email protected]' identified by 'mysql';
grant all privileges on *.* to test@'%' identified by 'mysql'; -- 后来用这个了 
flush privileges;

-- 切换到从库,进行设置

change master to master_host='127.0.0.1',master_port=3306,master_user='test',master_password='mysql',master_log_file='master-bin.000001',master_log_pos=0;

-- 再次查看从库的状态

show slave status \G;

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State:
                  Master_Host: 127.0.0.1
                  Master_User: test
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 4
               Relay_Log_File: slave-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: No
              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: 4
              Relay_Log_Space: 120
              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: NULL
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: 0
                  Master_UUID:
             Master_Info_File: D:\mysql\mysql2\data\master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State:
           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
1 row in set (0.00 sec)

ERROR:
No query specified

mysql>

-- 启动主从同步;

mysql> start slave;
Query OK, 0 rows affected (0.11 sec)

mysql>

-- 再次查看从库的状态,终于好了

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 127.0.0.1
                  Master_User: test
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000002
          Read_Master_Log_Pos: 1556
               Relay_Log_File: slave-relay-bin.000004
                Relay_Log_Pos: 284
        Relay_Master_Log_File: master-bin.000002
             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: 1556
              Relay_Log_Space: 2057
              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: 95f2c54c-bfce-11e8-8044-0800272bf016
             Master_Info_File: D:\mysql\mysql2\data\master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           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
1 row in set (0.00 sec)

ERROR:
No query specified

-- 验证

-- 在主库上创建表

mysql> use tes;
Database changed
mysql> create table t1(id varchar(2));
Query OK, 0 rows affected (0.56 sec)

mysql> insert into t1 values('a');
Query OK, 1 row affected (0.11 sec)

mysql> select * from t1;
+------+
| id   |
+------+
| a    |
+------+
1 row in set (0.00 sec)

mysql>

-- 在从库上查看

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| tes                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> use tes;
Database changed
mysql> show tables;
+---------------+
| Tables_in_tes |
+---------------+
| t1            |
+---------------+
1 row in set (0.00 sec)

mysql> select * from t1;
+------+
| id   |
+------+
| a    |
+------+
1 row in set (0.00 sec)

-- 碰到的问题: 
-- 第一次查看slave的状态,出现错误。
日志中显示如下:直到问题解决

2018-09-24 16:54:50 588 [Note] Slave SQL thread initialized, starting replication in log 'master-bin.000002' at position 504, relay log '.\slave-relay-bin.000001' position: 4
2018-09-24 16:55:50 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 2, Error_code: 1045
2018-09-24 16:56:50 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 3, Error_code: 1045
2018-09-24 16:57:50 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 4, Error_code: 1045
2018-09-24 16:58:50 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 5, Error_code: 1045
2018-09-24 16:59:50 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 6, Error_code: 1045
2018-09-24 17:00:50 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 7, Error_code: 1045
2018-09-24 17:01:50 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 8, Error_code: 1045
2018-09-24 17:02:50 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 9, Error_code: 1045
2018-09-24 17:03:50 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 10, Error_code: 1045
2018-09-24 17:04:50 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 11, Error_code: 1045
2018-09-24 17:05:33 588 [Note] Error reading relay log event: slave SQL thread was killed
2018-09-24 17:05:33 588 [Note] Slave SQL thread exiting, replication stopped in log 'master-bin.000002' at position 504
2018-09-24 17:05:33 588 [Note] Slave I/O thread killed while connecting to master
2018-09-24 17:05:33 588 [Note] Slave I/O thread exiting, read up to log 'master-bin.000002', position 504
2018-09-24 17:05:35 588 [Warning] Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information.
2018-09-24 17:05:35 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 1, Error_code: 1045
2018-09-24 17:05:35 588 [Warning] Slave SQL: If a crash happens this configuration does not guarantee that the relay log info will be consistent, Error_code: 0
2018-09-24 17:05:35 588 [Note] Slave SQL thread initialized, starting replication in log 'master-bin.000002' at position 504, relay log '.\slave-relay-bin.000001' position: 4
2018-09-24 17:06:35 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 2, Error_code: 1045
2018-09-24 17:07:02 588 [Note] Error reading relay log event: slave SQL thread was killed
2018-09-24 17:07:02 588 [Note] Slave SQL thread exiting, replication stopped in log 'master-bin.000002' at position 504
2018-09-24 17:07:02 588 [Note] Slave I/O thread killed while connecting to master
2018-09-24 17:07:02 588 [Note] Slave I/O thread exiting, read up to log 'master-bin.000002', position 504
2018-09-24 17:09:31 588 [Note] 'CHANGE MASTER TO executed'. Previous state master_host='127.0.0.1', master_port= 3306, master_log_file='master-bin.000002', master_log_pos= 504, master_bind=''. New state master_host='127.0.0.1', master_port= 3306, master_log_file='master-bin.000002', master_log_pos= 798, master_bind=''.
2018-09-24 17:09:33 588 [Warning] Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information.
2018-09-24 17:09:33 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 1, Error_code: 1045
2018-09-24 17:09:34 588 [Warning] Slave SQL: If a crash happens this configuration does not guarantee that the relay log info will be consistent, Error_code: 0
2018-09-24 17:09:34 588 [Note] Slave SQL thread initialized, starting replication in log 'master-bin.000002' at position 798, relay log '.\slave-relay-bin.000001' position: 4
2018-09-24 17:10:33 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 2, Error_code: 1045
2018-09-24 17:11:33 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 3, Error_code: 1045
2018-09-24 17:12:15 588 [Note] Error reading relay log event: slave SQL thread was killed
2018-09-24 17:12:15 588 [Note] Slave SQL thread exiting, replication stopped in log 'master-bin.000002' at position 798
2018-09-24 17:12:15 588 [Note] Slave I/O thread killed while connecting to master
2018-09-24 17:12:15 588 [Note] Slave I/O thread exiting, read up to log 'master-bin.000002', position 798
2018-09-24 17:13:08 588 [Warning] Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information.
2018-09-24 17:13:08 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 1, Error_code: 1045
2018-09-24 17:13:08 588 [Warning] Slave SQL: If a crash happens this configuration does not guarantee that the relay log info will be consistent, Error_code: 0
2018-09-24 17:13:08 588 [Note] Slave SQL thread initialized, starting replication in log 'master-bin.000002' at position 798, relay log '.\slave-relay-bin.000001' position: 4
2018-09-24 17:14:08 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 2, Error_code: 1045
2018-09-24 17:15:08 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 3, Error_code: 1045
2018-09-24 17:16:08 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 4, Error_code: 1045
2018-09-24 17:17:08 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 5, Error_code: 1045
2018-09-24 17:18:08 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 6, Error_code: 1045
2018-09-24 17:19:08 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 7, Error_code: 1045
2018-09-24 17:20:08 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 8, Error_code: 1045
2018-09-24 17:21:08 588 [ERROR] Slave I/O: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 9, Error_code: 1045
2018-09-24 17:22:08 588 [Note] Slave I/O thread: connected to master '[email protected]:3306',replication started in log 'master-bin.000002' at position 798
2018-09-24 17:22:08 588 [ERROR] Slave SQL: Error 'Can't drop database 'db180924'; database doesn't exist' on query. Default database: 'db180924'. Query: 'drop database db180924', Error_code: 1008
2018-09-24 17:22:08 588 [Warning] Slave: Can't drop database 'db180924'; database doesn't exist Error_code: 1008
2018-09-24 17:22:08 588 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'master-bin.000002' position 798.
2018-09-24 17:23:28 588 [Note] Slave I/O thread killed while reading event
2018-09-24 17:23:28 588 [Note] Slave I/O thread exiting, read up to log 'master-bin.000002', position 1556
2018-09-24 17:23:31 588 [Note] 'CHANGE MASTER TO executed'. Previous state master_host='127.0.0.1', master_port= 3306, master_log_file='master-bin.000002', master_log_pos= 1556, master_bind=''. New state master_host='127.0.0.1', master_port= 3306, master_log_file='master-bin.000001', master_log_pos= 4, master_bind=''.
2018-09-24 17:23:35 588 [Warning] Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information.
2018-09-24 17:23:35 588 [Note] Slave I/O thread: connected to master '[email protected]:3306',replication started in log 'master-bin.000001' at position 4
2018-09-24 17:23:35 588 [Warning] Slave SQL: If a crash happens this configuration does not guarantee that the relay log info will be consistent, Error_code: 0
2018-09-24 17:23:35 588 [Note] Slave SQL thread initialized, starting replication in log 'master-bin.000001' at position 4, relay log '.\slave-relay-bin.000001' position: 4
2018-09-24 17:23:37 588 [Note] Error reading relay log event: slave SQL thread was killed
2018-09-24 17:23:37 588 [Note] Slave SQL thread exiting, replication stopped in log 'master-bin.000002' at position 1556
2018-09-24 17:23:37 588 [Note] Slave I/O thread killed while reading event
2018-09-24 17:23:37 588 [Note] Slave I/O thread exiting, read up to log 'master-bin.000002', position 1556
2018-09-24 17:23:44 588 [Warning] Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information.
2018-09-24 17:23:44 588 [Note] Slave I/O thread: connected to master '[email protected]:3306',replication started in log 'master-bin.000002' at position 1556
2018-09-24 17:23:45 588 [Warning] Slave SQL: If a crash happens this configuration does not guarantee that the relay log info will be consistent, Error_code: 0
2018-09-24 17:23:45 588 [Note] Slave SQL thread initialized, starting replication in log 'master-bin.000002' at position 1556, relay log '.\slave-relay-bin.000003' position: 1720

-- 解决方法:
-- 主要还是查看复制的账号,比如权限,查看权限是没有问题的。但是发现不能登录,登录提示以下错误
D:\mysql\mysql1\bin>mysql -utest -p -h127.0.0.1 -P3306
Enter password: *****
ERROR 1045 (28000): Access denied for user 'test'@'localhost' (using password: YES)
参考https://www.cnblogs.com/summertime-wu/p/7466160.html 来解决了。


-- 另外,在搭建的时候,还出现一个问题,原来把mysql的目录设置成了d:\tools\mysql\mysql[1-2],这样导致建立的mysql服务没有办法启动,查看
windows的错误日志,居然发现路径是d:\ ools\mysql\mysql[1-2].修改路径后,OK。估计是把\t当做特殊字符了。

标题

END

猜你喜欢

转载自blog.csdn.net/xxzhaobb/article/details/82830876