mysql 安装 主从配置

非 Y 不嫁゛ 提交于 2019-12-21 04:23:42

软件下载

下载地址:mysql5.7

下载如下yum包:

mysql-community-client-5.7.20-1.el7.x86_64.rpm
mysql-community-common-5.7.20-1.el7.x86_64.rpm
mysql-community-devel-5.7.20-1.el7.x86_64.rpm
mysql-community-libs-5.7.20-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.20-1.el7.x86_64.rpm
mysql-community-server-5.7.20-1.el7.x86_64.rpm

安装

安装顺序:

 rpm -ivh mysql-community-common-5.7.20-1.el7.x86_64.rpm
 rpm -ivh mysql-community-libs-5.7.20-1.el7.x86_64.rpm
 rpm -ivh mysql-community-client-5.7.20-1.el7.x86_64.rpm
 rpm -ivh mysql-community-server-5.7.20-1.el7.x86_64.rpm
 rpm -ivh mysql-community-libs-compat-5.7.20-1.el7.x86_64.rpm

注意,我的linux是centos 7.2 需要卸载默认的安装包mariadb

 rpm -qa | grep mariadb #查看安装信息
 rpm -e mariadb-libs* --nodeps #根据查找到的软件包信息卸载

安装完成后

配置

key_buffer_size = 32M
max_allowed_packet = 32M
thread_stack = 256K
thread_cache_size = 64
query_cache_limit = 8M
query_cache_size = 64M
query_cache_type = 1
max_connections = 550

log_bin=/var/lib/mysql/mysql_binary_log
server_id=6   #我的IP:192.168.2.6   取IP最后 末尾数
binlog_format = mixed
read_buffer_size = 2M
read_rnd_buffer_size = 16M
sort_buffer_size = 8M
join_buffer_size = 8M

innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 64M
innodb_buffer_pool_size = 4G
innodb_thread_concurrency = 8
innodb_flush_method = O_DIRECT
innodb_log_file_size = 512M

启动mysql,修改密码

 # 启动服务
 systemctl start mysqld.service 
 # 在日志中会生成初始密码
 vim /var/log/mysqld.log  
 # 登录
 mysql -uroot -p   
 
 # 设置密码
 set global validate_password_policy=LOW;
 set global validate_password_length=8;
 set password = password('passwd') #设置密码
 #授权用户root使用密码passwd从任意主机连接到mysql服务器
 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'passwd' WITH GRANT OPTION;
 #授权用于同步 用户mysync使用密码passwd从192.168.* 段主机连接到mysql服务器
 GRANT REPLICATION SLAVE ON *.* TO 'mysync'@'192.168.%' IDENTIFIED BY 'passwd';
 flush privileges;

从节点配置

从节点安装步骤同上,首先登录 主节点查看bin-log 位置信息:

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

然后在从节点登录mysql:

mysql> change master to
    -> master_host='192.168.2.6',
    -> master_user='mysync',
    -> master_password='123456',
    -> master_log_file='mysql_binary_log.000001',
    -> master_log_pos=598;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

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

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.220.8.6
                  Master_User: mysync
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_binary_log.000001
          Read_Master_Log_Pos: 598
               Relay_Log_File: hdh05-relay-bin.000002
                Relay_Log_Pos: 327
        Relay_Master_Log_File: mysql_binary_log.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: 598
              Relay_Log_Space: 534
              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: 178f2e46-222a-11ea-8258-0cc47a0bd934
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave 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: 
1 row in set (0.00 sec)

当看到
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
说明主从已同步。可以在主节点创建 测试库 测试表 进行测试。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!