mysql主从热备份_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从初始化。
解决方法:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

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 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 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

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.

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.

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.

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.
