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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
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.

Explain the role of InnoDB redo logs and undo logs. Explain the role of InnoDB redo logs and undo logs. Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

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.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

How does MySQL index cardinality affect query performance? How does MySQL index cardinality affect query performance? Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: From Small Businesses to Large Enterprises MySQL: From Small Businesses to Large Enterprises Apr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

See all articles