search
HomeDatabaseMysql Tutorialmysql主从热备份_MySQL

环境:

主机:192.168.53.81

从机:192.168.53.82


一、查看主库mysql版本号,从库的mysql版本号要和主库一致。

[root@53-81 ~]# /usr/local/mysql/bin/mysql -V

/usr/local/mysql/bin/mysql Ver 14.14 Distrib 5.1.60, for unknown-linux-gnu(x86_64) using readline 5.1

创建测试表

# mysql -p123456

#use test;

mysql> create table t2 (id int(4));

mysql> insert into t2 values (1);

 

二、从库安装相同版本的mysql数据库。

1,下载相同版本的mysql源码包。

2,查看主机mysql编译参数。grep configure /usr/local/mysql/bin/mysqlbug

3,开始安装。可以写成批处理文件。

tar zxvf mysql-5.1.60.tar.gz

cd mysql-5.1.60

./configure '--prefix=/usr/local/mysql' '--with-extra-charsets=complex''--enable-assembler' '--with-mysqld-ldflags=-all-static' '--with-charset=utf8''--enable-thread-safe-client' '--with-big-tables' '--with-readline''--with-ssl' '--with-embedded-server' '--enable-local-infile'

make&& make install

cd ../

groupaddmysql

useradd-s /sbin/nologin -M -g mysql mysql

cp/usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf

sed -i's/skip-locking/skip-external-locking/g' /etc/my.cnf

 

/usr/local/mysql/bin/mysql_install_db--user=mysql

chown -Rmysql /usr/local/mysql/var

chgrp -Rmysql /usr/local/mysql/.

cp/usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql

chmod 755/etc/init.d/mysql

 

cat >/etc/ld.so.conf.d/mysql.conf

/usr/local/mysql/lib/mysql

/usr/local/lib

EOF

ldconfig

 

ln -s/usr/local/mysql/lib/mysql /usr/lib/mysql

ln -s/usr/local/mysql/include/mysql /usr/include/mysql

/etc/init.d/mysqlstart

 

ln -s/usr/local/mysql/bin/mysql /usr/bin/mysql

ln -s/usr/local/mysql/bin/mysqldump /usr/bin/mysqldump

ln -s/usr/local/mysql/bin/myisamchk /usr/bin/myisamchk

/usr/local/mysql/bin/mysqladmin-u root password 123456

三、主库创建/etc/my.cnf,修改里边的键值增加

server-id=1

log-bin=binlog_name

重启数据库,增加日志功能

 

四、主库增加用户,用于从库读取主库日志。

mysql> grant all on *.* toslave@'192.168.53.82' identified by 'slavekjh-123';

 

五、从库的操作

1,从库连接主库进行测试。

# mysql -u slave -pslavekjh-123 -h192.168.53.81

如果连接成功说明主库配置成功

 

2,停从库,

/etc/init.d/mysql stop

修改从库/etc/my.cnf,增加选项:

server-id=2

skip-slave-start

replicate-do-db=test    #需要同步的数据库

replicate-do-db=test2    #需要同步的数据库

replicate-do-db=email     #需要同步的数据库

replicate-do-db=ttreport   #需要同步的数据库

 

3,启动从库,进行主从库数据同步

/etc/init.d/mysql start

 

六,同步数据。

1,主库锁定数据库

mysql> flush tables with read lock;

测试是否锁定

mysql> use test;

mysql> insert into t2 values (1);

ERROR 1223 (HY000): Can't execute the querybecause you have a conflicting read lock

 

查看主库状态

mysql> show master status;

+------------------+----------+--------------+------------------+

| File            | Position |Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000033 | 74195486 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

 

2,从库开始同步

## mysql -p123456

mysql> change master tomaster_host='192.168.53.81',master_user='slave',master_password='slavekjh-123',master_port=3306,master_log_file='mysql-bin.000033',master_log_pos=74195486;

 

开始同步主库

mysql>load data from master;

 

开始同步

slave start ;

 

查看同步状态

show slave status/G;

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

 

删除/etc/my.cnf   

skip-slave-start                 

 

3,主库操作

解锁:

unlock tables;

插入数据看是否同步。

mysql> use test;

mysql> insert into t2 values (123456);

 

4,从库查看是否同步成功

mysql> use test;

Database changed

mysql> select * from t2;

 

完成===============================================================

测试

一,从库停止一段时间后,再启动是否能自动同步?是的。

二,主库停止一段时间后,主库启动,从库能否自动同步?是的。

问题1:同步主库时出现Net error reading from master 报错

原因:load table from master只支持myisam表,如果试图载入一个非MyISAM表,会导致以下错误。如果抛出这个错误,确保所有在主表是MyISAM。如果是这样,请重试运行的SQL语句。在InnoDB表或其他引擎的数据库的情况下,使用mysqldump从初始化。

解决方法:

 

Statement
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
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

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

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.