Home Database Mysql Tutorial Linux环境下单机上实现MySQL5主从数据库同步复制

Linux环境下单机上实现MySQL5主从数据库同步复制

Jun 07, 2016 pm 04:59 PM

1.安装MySQL(http://www.linuxidc.com/Linux/2009-03/18988.htm)2.启动多个MySQL服务器 要实现在单机上启动多个MySQL服务器

1.安装MySQL()
2.启动多个MySQL服务器
    要实现在单机上启动多个MySQL服务器,,有两种方法,一种是直接使用mysqld_safe来运行多个服务器
当然这样分别编辑配置文件,而且关闭服务器的时候也要读取配置文件,所以比较麻烦,另一种方法是使用
MySQL提供到工具mysqld_multi脚本来管理多个服务器,下面使用的方法是mysqld_safe来实现。
3.前置条件
    假设MySQL安装到目录为/usr/local/mysql/,设为MYSQL_DIR通常它是一个链接文件。
    数据文件目录为$MYSQL_DIR/data。
现在要添加另一个服务器的数据目录,因为为了模拟分布式服务器到同步,不可能让多个服务器共享一个数据目录。
$cd $MYSQL_DIR
$sudo cp -r -p data var2
    上面的指令将data保留原来到权限复制一份到var2,var2也就是另一个服务器的数据目录,这样,在原始
状态下,两个数据库服务器的数据是一致的。
4.假设有MySQL的合法用户root:root。
5.启动安装好以后的那个服务器
    $cd $MYSQL_DIR/bin
    $sudo ./mysqld_safe --user=mysql --binlog-do-db=test &
    上面的命令表示启动服务器并且使用二进制日志记录数据库test的更新动作。
6.测试是否启动成功
    $mysql -u root -p -S/tmp/mysql.sock
    输入密码后,如果能够成功登录的话表示成功,这里最容易出现2002错误,表示socket文件错误,你可以
使用命令
    $ps aux|grep mysql
来查看当前服务器使用的socket文件,然后在登录的时候使用相应的socket文件。
7.编辑配置文件
    $sudo vi /etc/my.cnf
最初的时候,这个配置文件是针对前面启动的服务器的,现在我们把它修改一下,然后就可以启动另一个服务器
找到[mysqld]段落,然后修改如下:
[mysqld]
server-id=2    #原来是1
socket=/tmp/mysql.sock2    #原来是/tmp/mysql.sock
port=3307                #原来是3306
#下面3行是添加的
pid-file=$MYSQL_DIR/var2/localhost.pid2
datadir=$MYSQL_DIR/var2
log=$MYSQL_DIR/var2/db2.log
    注意使用最前面的MySQL安装目录来代替上面的$MYSQL_DIR。
8.启动第二个服务器
    $cd $MYSQL_DIR/bin
    $sudo ./mysqld_safe --user=mysql  &
9.测试第二个服务器
    $mysql -u root -p -P 3307 -S /tmp/mysql.sock2
    输入密码后,应该能够正确连接到mysql服务器。
现在,两个服务器能够正常的运行在同一台机器上了,剩下的就是配置主从服务器,然后让主服务器更新,从服务器
连接主服务器并且保持同步。
10.同步服务器
    注意到我们启动第一个服务器的时候使用了一个参数--binlog-do-db=test表示,我们希望把数据库test的更新
操作都记录到二进制日志文件中。
    1)登录到主服务器
        $mysql -u root -p -P 3306 -S /tmp/mysql.sock
    2)查看主服务器的状态   
        mysql>show processlist\G
            上面这条命令执行后应该看到至少两个线程,第一个就是登录的线程,第二个就是发送二进制日志
            的线程。
        mysql>flush tables with read lock;
        mysql>show master status;
        mysql>unlock tables;
    记住show master status\G命令输出的结果,这里的File是二进制日志文件,Position是偏移量,Binlog_Do_DB
表示对哪些数据库记录更新操作,Binlog_Ignore_DB表示忽略哪些数据库更新。待会儿配置从服务器时要使用File和
Position。
    3)登录到从服务器
        $mysql -u root -p -P 3307 -S /tmp/mysql.sock2
    4)配置从服务器
        首先要确保停止从服务器同步线程
        mysql>stop slave;
        然后设置主服务器参数
        mysql>change master to
            ->master_host='127.0.0.1',
            ->master_user='root',
            ->master_password='root',
            ->master_log_file='mysql-bin.000016',
            ->master_log_pos=102;
        最后启动从服务器同步线程
        mysql>start slave;
        检查从服务器同步线程是否启动成功
        mysql>show slave status\G
            如果从上面的输出中看到了IO线程和LOG线程都是YES的话,那么表示启动成功,最后一行输出表示从服务器
            比主服务器滞后多少。
        查看线程
        mysql>show processlist;
            当从服务器同步成功启动以后,上面这条命令应该输出至少3个线程,第一个是登录线程,第二个是IO线程,第
            三个是日志线程。
11.测试同步
    在主服务器中的数据库test中任意执行一些更新操作,然后在从服务器中查看,应该马上就能够看到更新结果。

linux

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Reduce the use of MySQL memory in Docker Reduce the use of MySQL memory in Docker Mar 04, 2025 pm 03:52 PM

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

How to solve the problem of mysql cannot open shared library How to solve the problem of mysql cannot open shared library Mar 04, 2025 pm 04:01 PM

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Run MySQl in Linux (with/without podman container with phpmyadmin) Run MySQl in Linux (with/without podman container with phpmyadmin) Mar 04, 2025 pm 03:54 PM

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

What is SQLite? Comprehensive overview What is SQLite? Comprehensive overview Mar 04, 2025 pm 03:55 PM

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

Running multiple MySQL versions on MacOS: A step-by-step guide Running multiple MySQL versions on MacOS: A step-by-step guide Mar 04, 2025 pm 03:49 PM

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

See all articles