Mysql同台机器主从配置

时间:2022-09-15 10:57:36

Mysql主从配置

安装主Mysql

安装前的准备

检查系统是否存在mysql用户

[root@test mysql]# less /etc/passwd | grep mysql
mysql:x:501:501::/home/mysql:/sbin/nologin  

若没有则创建

[root@test mysql]# useradd -M -s /sbin/nologin mysql

检查系统是否安装RPM格式的mysql,已安装则删除

[root@test LAMP]# rpm -qa | grep mysql
mysql-libs-5.1.73-5.el6_6.x86_64
mysql-devel-5.1.73-5.el6_6.x86_64
mysql-5.1.73-5.el6_6.x86_64
[root@test LAMP]# yum remove mysql

新建数据库数据存放目录,即datadir,并更改属主、属组为mysql

[root@test LAMP]# chown mysql:mysql /data/mysql/
[root@test LAMP]# ll /data/mysql/ -d
drwxr-xr-x 2 mysql mysql 4096 11月 29 01:21 /data/mysql/

安装配置第一台Mysql

解压Mysql、移动并重命名

[root@test LAMP]# tar zxvf mysql-5.1.72-linux-x86_64-glibc23.tar.gz
[root@test LAMP]# mv mysql-5.1.72-linux-x86_64-glibc23 /usr/local/mysql

进入mysql目录

[root@test LAMP]# cd /usr/local/mysql/

拷备配置文件

cp support-files/my-large.cnf /etc/my.cnf 

初始化数据库,--user=定义数据库属主,--datadir=定义数据库数据存放目录

./scripts/mysql_install_db --user=mysql --datadir=/data/mysql/

拷备启动脚本并修改权限

[root@test mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@test mysql]# chmod 755 /etc/init.d/mysqld

修改启动脚本,指定数据目录datadir和程序目录basedir

[root@test mysql]# vim /etc/init.d/mysqld 

basedir=/usr/local/mysql
datadir=/data/mysql

加入系统启动服务项,设定开机启动

[root@test mysql]# chkconfig --add mysqld
[root@test mysql]# chkconfig mysqld on

启动Mysql,并查看监听端口

[root@test mysql]# service mysqld start
Starting MySQL. SUCCESS! 
[root@test mysql]# netstat -nlp | grep mysql
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      2109/mysqld         
unix  2      [ ACC ]     STREAM     LISTENING     17799  2109/mysqld         /tmp/mysql.sock

安装第二台Mysql-slave

拷备第一台Mysql程序目录并重命为mysql-slave,做为第二台mysql的程序目录

[root@test local]# cp -r mysql mysql-slave

拷备第一台mysql配置文件到mysql-slave下,做为第二台mysql的配置文件

[root@test mysql2]# cp /etc/my.cnf ./my.cnf

编辑mysql-slave的配置文件,更改从mysql的port、socket,避免与第一台mysql发生冲突,添加datadir

[root@test mysql2]# vim my.cnf 
    
    [mysqld]
    port            = 3307
    socket          = /tmp/mysql-slave.sock
    datadir         = /data/mysql-slave

初始化mysql2

[root@test mysql2]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql-slave

使用启动脚本启动Mysql2

拷备启动脚本

[root@test mysql2]# cd /etc/init.d/
[root@test init.d]# cp mysqld mysqld-slave

编辑mysql-slave启动脚本,指定mysql-slave的basedir和datadir,并更改conf=$basedir/my.cnf,指定mysql-slave的配置文件所在位置

[root@test init.d]# vim mysqld-slave
    
    basedir=/usr/local/mysql-slave
    datadir=/data/mysql-slave
    conf=$basedir/my.cnf

使用启动脚本启动mysql-slave

[root@test init.d]# /etc/init.d/mysqld-slave start
Starting MySQL. SUCCESS! 
[root@test init.d]# netstat -nlp | grep mysql
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      3077/mysqld         
tcp        0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      3330/mysqld         
unix  2      [ ACC ]     STREAM     LISTENING     25204  3077/mysqld         /tmp/mysql.sock
unix  2      [ ACC ]     STREAM     LISTENING     30937  3330/mysqld         /tmp/mysql-slave.sock

使用启动命令起动Mysql-slave

使用脚本停止mysql2

[root@test init.d]# /etc/init.d/mysqld-slave stop
Shutting down MySQL..... SUCCESS! 
[root@test init.d]# netstat -nlp | grep mysql
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      3077/mysqld         
unix  2      [ ACC ]     STREAM     LISTENING     25204  3077/mysqld         /tmp/mysql-slave.sock

使用启动命令启动Mysql-slave

[root@test init.d]# /usr/local/mysql-slave/bin/mysqld_safe --defaults-file=/usr/local/mysql-slave/my.cnf --user=mysql &
[1] 3373
[root@test init.d]# 151129 02:53:58 mysqld_safe Logging to '/data/mysql-slave/test.err'.
151129 02:53:58 mysqld_safe Starting mysqld daemon with databases from /data/mysql-slave
[root@test init.d]# !ne
netstat -nlp | grep mysql
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      3077/mysqld         
tcp        0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      3494/mysqld         
unix  2      [ ACC ]     STREAM     LISTENING     25204  3077/mysqld         /tmp/mysql.sock
unix  2      [ ACC ]     STREAM     LISTENING     31582  3494/mysqld         /tmp/mysql-slave.sock

配置Mysql主从的准备工作

主mysql,端口:3306,socket:/tmp/mysql.sock
从mysql,端口:3307,socket:/tmp/mysql-slave.sock

在主上创建测试库:db1

[root@stady ~]# mysql -u root -S /tmp/mysql.sock -e "create database db1";

导出主的mysql库数据,导入到db1库

[root@stady ~]# mysqldump -u root -S /tmp/mysql.sock mysql > 123.sql
-- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly.
[root@stady ~]# mysql -u root -S /tmp/mysql.sock db1 < 123.sql

查看db1库

[root@stady ~]# mysql -u root -S /tmp/mysql.sock 

mysql> use db1
Database changed
mysql> show tables;

+---------------------------+-
| Tables_in_db1             |
+---------------------------+-
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
23 rows in set (0.00 sec)

mysql> exit

配置主mysql(master)

编辑主mysql的配置文件

[root@stady ~]# vim /etc/my.cnf

更改或添加

server-id       = 1         #设置主mysql的server-id,可为任意但必须唯一
log-bin=mysql-master        #开启主mysql的二进制binlog日志
#binlog-do-db=db1,db2,...dbn    #可选项,用来指定需要同步的库
#binlog-ignore-db=db1,db2...dbn #可选项,用来指定同步需要忽略的库

重启主mysql

[root@stady ~]# service mysqld restart
Shutting down MySQL. SUCCESS! 
Starting MySQL. SUCCESS! 

设置主Mysql的root密码

[root@stady ~]# mysqladmin -u root -S /tmp/mysql.sock password 'qwe123'

密码登陆主mysql

[root@stady ~]# mysql -u root -S /tmp/mysql.sock -p
Enter password: 

授权一个用来同步数据的用户repl
mysql> grant replication slave on .* to 'repl'@'127.0.0.1' identified by '123123';*
Query OK, 0 rows affected (0.00 sec)

锁死表的读
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

查看主mysql的状态
mysql> show master status;

+---------------------+----------+--------------+------------------+
| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------------+----------+--------------+------------------+
| mysql-master.000003 |      394 |              |                  |
+---------------------+----------+--------------+-------------------
File:
position:

配置从mysql(slave)

编辑从mysql的配置文件

[root@stady ~]# vim /usr/local/mysql-slave/my.cnf 

修改或添加

server-id       = 2 #设置从mysql的server-id,不能与主Mysql一样
replicate-do-db=db1,db2,...dbn  #可选项,用来指定需要同步的库
replicate-ignore-db=db1,db2...dbn #可选项,用来指定同步需要忽略的库
#若主mysql已定义,则从mysql就不用再次定义

在从mysql创建db1库,拷备主mysql的db1库,导入到从mysql的db1库
确保主从mysql上的数据完全一致

[root@stady ~]# mysql -u root -S /tmp/mysql-slave.sock -e "create database db1";
[root@stady ~]# mysqldump -u root -S /tmp/mysql.sock -pqwe123 db1 > db1.sql
[root@stady ~]# mysql -u root -S /tmp/mysql-slave.sock db1 < db1.sql 

查看从mysql的db1库

[root@stady ~]# mysql -u root -S /tmp/mysql-slave.sock 

mysql> use db1
Database changed
mysql> show tables;

停上主从复制

mysql> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)

指定change master

mysql> change master to master_host='127.0.0.1', #指定主mysql的ip
master_port=3306,   #指定主mysql的端口
master_user='repl', #指定同步用户
master_password='123123',   #同步用户密码
master_log_file='mysql-master.000003', #指定二进制的binlog文件
master_log_pos=394; #指定binlog的偏移量

Query OK, 0 rows affected (0.04 sec)

开启主从复制

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

退出从mysql

    mysql> quit
    Bye

进入主mysql,解锁表

[root@stady ~]# mysql -u root -S /tmp/mysql.sock -p qwe123 -e "unlock tables";

进入从mysql,查看slave状态

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 127.0.0.1
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-master.000005
          Read_Master_Log_Pos: 106
               Relay_Log_File: stady-relay-bin.000007
                Relay_Log_Pos: 254
        Relay_Master_Log_File: mysql-master.000005
             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: 106
              Relay_Log_Space: 557
              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: 
1 row in set (0.00 sec)

只有当

Slave_IO_Running: Yes #
Slave_SQL_Running: Yes #

则说明主从mysql运行正常

测试主从

登陆主mysql,删除db1库中的host表

[root@stady ~]# mysql -u root -S /tmp/mysql.sock -p 
    mysql> use db1
    Database changed
    mysql> show tables;
    +---------------------------+
    | Tables_in_db1             |
    +---------------------------+
    | columns_priv              |
    | event                     |
    | func                      |
    | general_log               |
    | help_category             |
    | help_keyword              |
    | help_relation             |
    | help_topic                |
    | host                      |
    | ndb_binlog_index          |
    | plugin                    |
    | proc                      |
    | procs_priv                |
    | servers                   |
    | slow_log                  |
    | tables_priv               |
    | time_zone                 |
    | time_zone_leap_second     |
    | time_zone_name            |
    | time_zone_transition      |
    | time_zone_transition_type |
    | user                      |
    +---------------------------+
    22 rows in set (0.01 sec)
    
    mysql> drop table host;
    Query OK, 0 rows affected (0.00 sec)

登入从mysql,查看db1库中是否存在host表

[root@stady ~]# mysql -u root -S /tmp/mysql-slave.sock 
    mysql> use db1
    Database changed
    mysql> show tables;
    +---------------------------+
    | Tables_in_db1             |
    +---------------------------+
    | columns_priv              |
    | event                     |
    | func                      |
    | general_log               |
    | help_category             |
    | help_keyword              |
    | help_relation             |
    | help_topic                |
    | ndb_binlog_index          |
    | plugin                    |
    | proc                      |
    | procs_priv                |
    | servers                   |
    | slow_log                  |
    | tables_priv               |
    | time_zone                 |
    | time_zone_leap_second     |
    | time_zone_name            |
    | time_zone_transition      |
    | time_zone_transition_type |
    | user                      |
    +---------------------------+
    21 rows in set (0.00 sec)

在从myslq的db1库中没有host表,说明主从复制配置成功

注意:

如果需重启主mysql,务必要先把从mysql的slave停掉,即在从mysql上执行:

    mysql> slave stop;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> quit
    Bye

然后再重启主、从mysql

[root@stady ~]# service mysqld stop
Shutting down MySQL....... SUCCESS! 
[root@stady ~]# /etc/init.d/mysqld-slave stop
Shutting down MySQL..... SUCCESS! 
[root@stady ~]#