Home Database Mysql Tutorial linux下mysql数据库单向同步配置方法分享_MySQL

linux下mysql数据库单向同步配置方法分享_MySQL

Jun 01, 2016 pm 01:22 PM
linux binary database server

bitsCN.com 又叫做主从复制,是通过二进制日志文件完成的,注意:mysql 数据库的版本,两个数据库版本要相同

系统环境:
主服务器master IP: 192.168.0.88
从服务器slave IP: 192.168.0.99

单向同步
注意要点
1、只需要把需要同步的数据库备份传到从数据库上,其它任何东西都不需要
2、在从库上还原数据库时,一定先要建了同名数据库,否则还原会提示错误
3、在制定更新点时,一定要在同步进程还没启动前操作

主服务器上配置
对my.cnf进行修改需要重启动数据库设置才会生效,如果不想对指定数据库同步进行限制,直接跳过这步

[root@kt /]# service mysqld stop 停止Mysql服务
 [root@kt /]# vim /etc/my.cnf 编辑Mysql配置文件添加下面字段,注意:这两个字段默认没有自己在server-id字段下面添加就可以了
binlog-do-db     = kangte   二进制需要同步的kangte数据库
binlog-ignore-db = mysql     二进制不想同步的MySQL数据库
 
[root@kt /]# service mysqld start 启动主服务器
  建立远程同步用户mysql> grant replication slave,replication client,reload,super on *.* to 'kt'@'192.168.0.99' identified by '123456'with grant option;
mysql> flush privileges; 更新数据库让用户生效
[root@zzh /]# mysql -h 192.168.0.88 -u kt -p 在备份服务器上测试一下建的用户是否可以登陆
  
mysql> flush tables with read lock; 设置读锁
mysql> show master status; 查看当前二进制日志名和偏移量值,从库按这个点开始进行数据恢复
查看结果如下:+------------------+----------+--------------+------------------+| File                 Position    Binlog_Do_DB  Binlog_Ignore_DB    |+------------------+----------+--------------+------------------+| mysql-bin.000006       656                                     | +------------------+----------+--------------+------------------+  
[root@zzh /]# mysqldump -u root -p kangte > /kangte.sql 备份指定数据库,也可以直接用物理备份
mysql> unlock tables; 解除读锁
[root@zzh /]# scp /kangte.sql root@192.168.0.99:/ 把备份传到从数据库服务器

备份服务器配置

注意:如果数据库有相同的数据库把之前的库删除 
[root@zzh /]# service mysqld stop 停止Mysql服务
 [root@zzh /]# vim /etc/my.cnf 编辑Mysql配置文件添加下面字段,注意:这些字段默认没有自己在server-id字段下面添加就可以了
server-id               = 2 注意:去掉注释,并把上面的server-id=1屏蔽
master-host =192.168.0.88 指定主服务器IP地址
master-user = kt 制定在主服务器上可以进行同步的用户名
master-password = 123456 密码
master-port = 3306 同步所用的端口
master-connect-retry = 60 断点重新连接时间
replicate-do-db = kangte 二进制需要同步的kangte数据库
replicate-ignore-db = mysql 二进制不想同步的MySQL数据库
  启动从服务器[root@zzh /]# /usr/local/mysql/bin/mysqld_safe --skip-slave-start &
--skip-slave-start 启动从数据库,不立即启动从数据库服务上的复制进程
 
mysql> create database kangte; 注意:一定要先创建kangte库否则还原不了
[root@zzh /]# mysql -u root -p kangte 恢复数据库
 指定更新点(注意:执行下面指定更新点时,一定要在同步进程没有启动时才可以操作)mysql> CHANGE MASTER TO    -> MASTER_LOG_FILE='mysql-bin.000006',        #在主服务器上查的二进制日志名              -> MASTER_LOG_POS=656;                        #在主服务器上查的偏移量值                        
mysql> start slave; 启动同步进程
 检查同步连接性mysql> show slave status/G;
Slave_IO_Running   : Yes 网络连接正常
Slave_SQL_Running: Yes 数据库结构正常

MySQL单向同步实现 命令行操作

实例主机:
dbasky=192.168.1.120
dbaskyback=192.168.1.121

目的:dbaskyback的主机去同步dbasky主机test库上的数据

安装mysql
[root@dbasky]#wget ftp://ftp.cronyx.ru/pub/FreeBSD/ports/distfiles/mysql-5.0.45.tar.gz
[root@dbasky]#cd /usr/local/mysql-5.0.45
[root@dbasky]#groupadd mysql
[root@dbasky]#useradd -g mysql mysql
[root@dbasky]#mkdir /opt/mysql-data
[root@dbasky]#CFLAGS="-O3 " CXX=gcc CXXFLAGS="-O3 -felide-constructors
-fno-exceptions -fno-rtti" ./configure --prefix=/usr/local/mysql --enable-assembler --with-charset=utf8 --with-extra-charsets=gbk,gb2312,latin1 --localstatedir=/opt/mysql-data --with-mysqld-user=mysql --enable-large-files --with-big-tables --without-debug --enable-thread-safe-client --with-fast-mutexes --with-innodb
[root@dbasky]#make
[root@dbasky]#make install
[root@dbasky]#cd /etc
[root@dbasky]#rz my.cnf
[root@dbasky]#chown -R mysql .
[root@dbasky]#chgrp -R mysql .
[root@dbasky]#chown -R mysql /opt/mysql-data
[root@dbasky]#chgrp -R mysql /opt/mysql-data
[root@dbasky]#bin/mysql_install_db --user=mysql
[root@dbasky]#chown -R root .
[root@dbasky]#bin/mysqld_safe --user=mysql &
[root@dbasky]#cd /usr/local/mysql
[root@dbasky]#echo "PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile
[root@dbasky]#echo "export PATH" >> /etc/profile
[root@dbasky]#echo "alias vi="vim"" >> /etc/profile
[root@dbasky]#echo "/usr/local/mysql/lib/mysql" > /etc/ld.so.conf.d/mysql.conf

在dbasky机器上
建立用户
mysql>create database test;
mysql>grant all on *.* to xu@192.168.1.121 identified by 123456;

[root@dbasky]#vi /etc/my.cnf
server-id=1 #为master
log-bin=/var/log/mysql/mysql.log
添加
binlog-do-db=test #要同步的数据库名字

重新启动mysql
用 mysql>show master status 查看

在dbaskyback机器上
dbaskyback#vi /etc/my.cnf
  server-id=2 #为slave
  master-host=192.168.1.120
  master-user=xu
  master-password=123456
  master-port=3306
  master-connect-retry=60 #间隔60秒重新设置
  replicate-do-db=test

用mysql>show slave status 看同步情况,如果有错误也可以看得出来.

bitsCN.com
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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1243
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

MySQL: Structured Data and Relational Databases MySQL: Structured Data and Relational Databases Apr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

git software installation git software installation Apr 17, 2025 am 11:57 AM

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

See all articles