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服务 |
binlog-do-db = kangte | 二进制需要同步的kangte数据库 |
binlog-ignore-db = mysql | 二进制不想同步的MySQL数据库 |
[root@kt /]# service mysqld start | 启动主服务器 |
mysql> flush privileges; | 更新数据库让用户生效 |
[root@zzh /]# mysql -h 192.168.0.88 -u kt -p | 在备份服务器上测试一下建的用户是否可以登陆 |
mysql> flush tables with read lock; | 设置读锁 |
mysql> show master status; | 查看当前二进制日志名和偏移量值,从库按这个点开始进行数据恢复 |
[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服务 |
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数据库 |
--skip-slave-start | 启动从数据库,不立即启动从数据库服务上的复制进程 |
mysql> create database kangte; | 注意:一定要先创建kangte库否则还原不了 |
[root@zzh /]# mysql -u root -p kangte | 恢复数据库 |
mysql> start slave; | 启动同步进程 |
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

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment
