Home Database Mysql Tutorial MySQL的复制(主从,主主,基于ssl)

MySQL的复制(主从,主主,基于ssl)

Jun 07, 2016 pm 05:40 PM
mysql

1.准备的主机node1:172.16.133.11node2:172.16.133.12均已安装好MySQLMySQL安装见:http://5142926.blog.51cto.com/5132926/9356522.mysql主从复制node1:主服务器

1.准备的主机
node1:172.16.133.11
node2:172.16.133.12
均已安装好MySQL
MySQL安装见:
2.mysql主从复制
node1:主服务器
node2:从服务器
(1)node1:
    启用二进制日志(默认就是启用的)
 创建具有复制权限的用户
 设置server-id
node2:
    启用中继日志(默认是禁用的,如果不需要的话,可以手动关闭二进制日志)
 设置server-id
 启动从服务,并指定主服务器参数
node1:node1中mysql服务器binary log默认就是开启的,server-id也不用修改,默认即可
创建具有复制权限的用户
 

  • node2:修改mysql主配置文件my.cnf中的server-id为21,注释掉log-bin=mysql-bin
    并在其后添加relay-log=mysql-relay
    完成后,进入mysql,查询下全局变量show global variables like '%log%;
     

  • mysql>show slave status/G查看从服务器工作状态,可以看到Slave_IO_Running: No和Slave_SQL_Running: No还是no,启用这两项
     

    然后就算配置完成了,可以在node1中建立一个测试数据库testdb,和一个测试表t1


    进入node2的mysql查看


    (2).如果不想让从服务器线程在mysql服务启动时自动启动,则可以在从服务器中设置skip-slave-start=1
    为防止主服务器突然崩溃,可以在主服务器上设置
    sync_binlog=1
    innodb_flush_logs_at_trx_commit=1
    (3).数据库复制过滤
    主服务器
    [mysqld]
    binlog-do-db=magedu
    在主服务器过滤:任何不涉及到数据库相关的写操作都不会被记录到二进制日志当中,所以最好不要设置,一般在从服务器中设置即可
    从服务器:
    replicate_do_db
    rpplicate_ignore_db

    replicate_do_table
    replicate_ignore_table

    replicate_wild_do_table
    replicate_wild_ignore_table
    在从服务器上只复制testdb一个数据库:
    [mysqld]
    replicate_do_db=testdb
    replicate_do_db=mysql
    (4).如果主服务器以运行很长时间,才接入一台新的从服务器,如果采取复制,会比较慢,可以采用备份的方式
    node1:先对mysql服务器施加读锁
    mysql>flush tables with read lock;
    而后对mysql所在数据目录的逻辑卷,进行备份
    lvcreate -L 50M -s -p r -n mydata-snap /dev/myvg/mydata
    mysql>show master status;查看现在所在位置


    mysql>unlocak tables(备份完成后要立即解锁)
    mount /dev/myvg/mydata-snap /mnt
    cd /mnt
    ll
    find . | cpio -o -H newc --quiet | gzip > /root/alldatabase.gz
    cd
    umount /mnt
    scp alldatabase.gz node2:/root
    mysql>use testdb
    mysql>create table tb2


    node2:
    gzip -d /root/alldatabase.gz
    cp alldatabase /data/mydata
    cd /data/mydata
    cpio -id rm alldatabase
    然后就可以直接service mysqld start
    然后进入mysql,重新设置主从
    mysql>change master to master_host='172.16.133.11',master_user='repluser',master_password='redhat',master_log_file='mysql-bin.000003',master_log_pos=542;
    mysql>start slave;
    mysql>show slave status\G
    mysql>use testdb;
    mysql>show tables
    备份+复制完成
    (5).半同步主从复制
    node1:
    mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so'; 
    mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1; 
    mysql> SET GLOBAL rpl_semi_sync_master_timeout = 1000;
    node2:
    mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so'; 
    mysql> SET GLOBAL rpl_semi_sync_slave_enabled = 1; 
    mysql> STOP SLAVE IO_THREAD; START SLAVE IO_THREAD;
    也可通过设置全局变量的方式来设置,如下:
    set global rpl_semi_sync_master_enabled=1
    取消加载插件
    mysql> UNINSTALL PLUGIN rpl_semi_sync_master;
    查看从服务器上的semi_sync是否开启:
    mysql> SHOW GLOBAL STATUS LIKE 'rpl_semi%';
    查看主服务器上的semi_sync是否开启,虚拟主机,注意clients 变为1 ,证明主从半同步复制连接成功:
    (6).基于ssl传输的mysql主从复制
    ①.修改配置文件
    node1:
    server_id=10        
    log_bin=mysql-bin
    sync_binlog=1事务提交后立即写入磁盘二进制文件,不再先缓存再写
    node2:
    read_only=1
    ②准备证书,私钥
    Ⅰ.建立字签证服务器
    node1:
    vim /etc/pki/tls/openssl.cnf
       dir=/etc/pki/CA
    (umask 077;openssl genrsa 2048 > private/cakey.pem)
    openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3655


    mkdir certs crl newcerts
    touch index.txt
    echo 01 > serial
    Ⅱ.为node1上的mysql准备私钥及颁发证书
    mkdir /usr/local/mysql/ssl
    cd ssl/
    (umask 077;openssl genrsa 1024 > mysql.key)
    openssl req -new -key mysql.key -out mysql.csr
    openssl ca -in mysql.csr -out mysql.crt
    cp /etc/pki/CA/cacert.pem /usr/local/mysql/ssl/
    Ⅲ.为node2上的mysql准备私钥及颁发证书
    mkdir /usr/local/mysql/ssl
    cd ssl/
    (umask 077;openssl genrsa 1024 > mysql.key)
    openssl req -new -key mysql.key -out mysql.csr
    scp ./mysql.csr node1:/root
    Ⅳ.为node2签发证书
    openssl ca -in mysql.csr -out mysql.crt
    scp ./mysql.crt node2:/usr/local/mysql/ssl
    cd /etc/pki/CA
    scp ./cacert.pem node2:/usr/local/mysql/ssl
    完成后,确定node1和node2中的/usr/local/mysql/ssl目录下,有这4个文件


    ③.打开mysql的ssl功能
    node1:
    mysql>show variables like '%ssl%';


    其中have_openssl,have_ssl显示为disabled,表示未开启ssl
    编辑主配置文件/etc/my.cnf在[mysqld]中添加
    ssl
    即可,重启mysql服务
    mysql>show variables like '%ssl%';

  • Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

    MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

    MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

    MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

    Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

    MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

    How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

    Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

    How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

    The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

    MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

    The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

    Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

    Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

    How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

    The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

    See all articles