Ubuntu
MySQL的主从复制是异步的,分master/slave,在master端存在一个IO线程,而在slave下存在IO及Sql线程。
搭建环境最重要的地方在于将mysql的二进制日志功能开启,我在搭的过程中有个细节没注意,坑了我好久。
环境:
我的两台虚拟机上网方式都是采用桥接模式,不推荐nat.首先对两台虚拟机设置固定ip地址,和你的物理机的ip地址在一个段内,这样你的虚拟机就很类似一台局域网的物理机工作了。
设置固定ip地址:
$ sudo vim /etc/network/interfaces
如下:
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.1.252
netmask 255.255.255.0
gateway 192.168.1.1
$ sudo /etc/init.d/networking restart
我的虚拟机地址各设为:192.168.1.251 192.168.1.252 网关IP:192.168.1.1
设置完后,首先在虚拟机中互ping,必须保证能通。
环境准备好后,可以开始了,我没有采取rpm方式,ubuntu下直接在线安装mysql非常方便的,省去了很多步骤。
$ sudo apt-get install mysql-server
默认安装完自动开启的,所以用 netstat -tap|grep mysql 看看是不是mysql服务存在了
之后查询下mysql运行状态,service mysql status,同样,还有其他命令,service mysql start/stop/restart.
如果出现了以下信息,则代表成功了。
mysql start/running, process 1199
在登陆前先到/etc/mysql/my.cnf下 将bind-address注释掉,因为默认只能本机访问。
登陆mysql
mysql -u root -p
grant replication slave,reload,super on *.* to slave @192.168.1.152 identified by '1234'
然后在另一台Ubuntu下远程测试下
mysql -u slave -h 192.168.1.151 -p
前期工作准备好了,现在就开始配置:
在master 192.168.1.251的机子上对my.cnf修改,切记以下配置信息一定要加到【mysqld】模块中,放在文件其他位置会导致master配置失败。
server-id=1
log_bin=/var/log/mysql/mysql-bin.log
binlog_do_db=student
binlog_ignore_db=mysql
重启下mysql
sudo /etc/init.d/mysql restart
如果在重启过程中failed,建议先看下my.cnf的log-error对应得错误日志文件位置,然后cat看下报错信息
之后进入master的mysql,首先看下二进制功能是否on.
show variables like 'log%'
如果为off代表没开启,则还是返回检查下my.cnf文件,是否放在[mysqld]那块中等。文件路径是否对了
show master status;则能看到对应得file position 信息,这些在从机设置的时候要用到。
最后到salve 192.168.1.252的/etc/mysql/my.cnf下配置:
server-id=2
log_bin=/var/log/mysql/mysql-bin.log
replicate_do_db=student
重启下mysql
sudo /etc/init.d/mysql restart
slave进入mysql:
stop slave
然后设置从master复制日志配置
change master to master_host='192.168.1.151' ,master_user='slave',master_password='1234',
master_log_file='log.000004',master_log_pos=94;
log_file log_pos则是在master下show master status看到的file position信息。
start slave;
show slave status/G
在出现的信息中找到 Slave_IO_Running/Slave_SQL_Running ,都为YES则成功了。
如果出现了NO,则还是查看log-error日志文件,会比较清晰的描述出来的。

MySQL processes data replication through three modes: asynchronous, semi-synchronous and group replication. 1) Asynchronous replication performance is high but data may be lost. 2) Semi-synchronous replication improves data security but increases latency. 3) Group replication supports multi-master replication and failover, suitable for high availability requirements.

The EXPLAIN statement can be used to analyze and improve SQL query performance. 1. Execute the EXPLAIN statement to view the query plan. 2. Analyze the output results, pay attention to access type, index usage and JOIN order. 3. Create or adjust indexes based on the analysis results, optimize JOIN operations, and avoid full table scanning to improve query efficiency.

Using mysqldump for logical backup and MySQLEnterpriseBackup for hot backup are effective ways to back up MySQL databases. 1. Use mysqldump to back up the database: mysqldump-uroot-pmydatabase>mydatabase_backup.sql. 2. Use MySQLEnterpriseBackup for hot backup: mysqlbackup--user=root-password=password--backup-dir=/path/to/backupbackup. When recovering, use the corresponding life

The main reasons for slow MySQL query include missing or improper use of indexes, query complexity, excessive data volume and insufficient hardware resources. Optimization suggestions include: 1. Create appropriate indexes; 2. Optimize query statements; 3. Use table partitioning technology; 4. Appropriately upgrade hardware.

MySQL view is a virtual table based on SQL query results and does not store data. 1) Views simplify complex queries, 2) Enhance data security, and 3) Maintain data consistency. Views are stored queries in databases that can be used like tables, but data is generated dynamically.

MySQLdiffersfromotherSQLdialectsinsyntaxforLIMIT,auto-increment,stringcomparison,subqueries,andperformanceanalysis.1)MySQLusesLIMIT,whileSQLServerusesTOPandOracleusesROWNUM.2)MySQL'sAUTO_INCREMENTcontrastswithPostgreSQL'sSERIALandOracle'ssequenceandt

MySQL partitioning improves performance and simplifies maintenance. 1) Divide large tables into small pieces by specific criteria (such as date ranges), 2) physically divide data into independent files, 3) MySQL can focus on related partitions when querying, 4) Query optimizer can skip unrelated partitions, 5) Choosing the right partition strategy and maintaining it regularly is key.

How to grant and revoke permissions in MySQL? 1. Use the GRANT statement to grant permissions, such as GRANTALLPRIVILEGESONdatabase_name.TO'username'@'host'; 2. Use the REVOKE statement to revoke permissions, such as REVOKEALLPRIVILEGESONdatabase_name.FROM'username'@'host' to ensure timely communication of permission changes.


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

SublimeText3 Chinese version
Chinese version, very easy to use

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.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
