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



Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values and pointers to data rows, and is suitable for non-primary key column queries.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.
