mysql 安装 主从配置

软件下载

下载地址: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 postfix
 rpm -ev postfix-2.10.1-7.el7.x86_64
 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/bash

#http://repo.mysql.com/yum/mysql-5.6-community/el/7/x86_64/ 注意linux red6  red7
yum install -y wget
mkdir software
if [ ! -s ./software/mysql-community-common-5.7.20-1.el7.x86_64.rpm ];then
 wget -c "http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-common-5.7.20-1.el7.x86_64.rpm" -P software
fi

if [ ! -s ./software/mysql-community-libs-5.7.20-1.el7.x86_64.rpm ];then
 wget -c "http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-libs-5.7.20-1.el7.x86_64.rpm"  -P software
fi

if [ ! -s ./software/mysql-community-devel-5.7.20-1.el7.x86_64.rpm ];then
 wget -c "http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-devel-5.7.20-1.el7.x86_64.rpm"  -P software
fi

if [ ! -s ./software/mysql-community-client-5.7.20-1.el7.x86_64.rpm ];then
 wget -c "http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-client-5.7.20-1.el7.x86_64.rpm" -P software
fi

if [ ! -s ./software/mysql-community-server-5.7.20-1.el7.x86_64.rpm ];then
 wget -c "http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-server-5.7.20-1.el7.x86_64.rpm" -P software
fi

if [ ! -s ./software/mysql-community-libs-compat-5.7.20-1.el7.x86_64.rpm ];then
 wget -c "http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-libs-compat-5.7.20-1.el7.x86_64.rpm" -P software
fi

rpm -ev postfix --nodeps
rpm -ev mariadb-libs --nodeps

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

systemctl start mysqld.service
db_pass=$(sed -n '/temporary password/p' /var/log/mysqld.log | awk -F ": " '{print $2}')
echo mysql temp password: $db_pass

mysql --connect-expired-password  -uroot -p${db_pass}<<EOF
set global validate_password_policy=LOW;
set global validate_password_length=6;
set password = password('123456');
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
flush privileges;
show databases;
EOF
从节点配置

从节点安装步骤同上,首先登录 主节点查看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
说明主从已同步。可以在主节点创建 测试库 测试表 进行测试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值