MySQL High Availability DRBD + Heartbeat + MySQL
1. Architecture Mysql:
Master <= slave
10.24.6.4: 3306 <= 10.24.6.6: 3306
VIP:
10.24.6.20
The VIP and mysql must be in the same network segment; otherwise, the ping fails.
2. References
3. DRBD shared memory settings
Shared Hard Disk/dev/sdb1
3.1. Create a New Hard Disk/dev/sdb1, but do not mount any directories.
3.2. Modify the hosts file and Host Name
On 10.24.6.4
Echo "10.24.6.4 drbd01">/etc/hosts
Echo "drbd01">/etc/hostname
On 10.24.6.6
Echo "10.24.6.6 drbd02">/etc/hosts
Echo "drbd02">/etc/hostname
3. Install drbd
On 10.24.6.4/10.24.6.6
Sudo apt-get install drbd8-utils
3. 4. Configure the configuration file of drbd. conf (/etc/drbd. conf)
Vim/etc/drbd. conf:
Include "/etc/drbd. d/global_common.conf ";
Include "/etc/drbd. d/*. res ";
In this way, you need to configure global_common.conf and create a resource file ending with res, Which is r0.res.
3.5. Configure global_common.conf (both master and slave are required)
Vim/etc/drbd. d/global_common.conf
Global {
Usage-count no; // help LINBIT calculate drbd usage}
Common {
Protocol C; // C is the safest and best-performing method to confirm the completion of write operations
Syncer {
Rate 100 M; // synchronization rate
}
}
. Configure ro. res (both master and slave nodes are required)
Sudo vim/etc/drbd. d/r0.res
Resource r0 {// r0 resource Name, which will be used in many places in the future
On drbd01 {
Device/dev/drbd0; // path of the Logical device
Disk/dev/sdb1; // physical device
Address 10.24.6.4: 7788; // master node
Meta-disk internal;
}
On drbd02 {
Device/dev/drbd0;
Disk/dev/sdb1;
Address 10.24.6.6: 7788; // slave Node
Meta-disk internal;
}
}
Annotations are not allowed.
3.7. initialize the metadata of drbd (operate on the master node)
10.24.6.4:
Sudo drbdadm create-md r0
3.8. handle errors
Run the following command: dd if =/dev/zero bs = 1 M count = 1 of =/dev/sdb1; sync
If you run drbdadm create-md r0 again and see success, the operation is successful.
3.9. Start the drbd Service (both master and slave nodes are executed)
/Etc/init. d/drbd start
Cat/proc/drbd
Ro: Secondary/Secondary indicates no primary disk has been created
Ds: Inconsistent/Inconsistent indicates that the data in the logical device (I .e./dev/drbd0) of the two nodes (master/slave) is not synchronized.
The hard disks of local and peer nodes may be in one of the following States:
Diskless: no local Block devices are allocated to DRBD. This indicates no available devices, or the drbdadm command is used for manual separation or the underlying I/O error causes automatic separation.
Attaching: the instantaneous status when no data is read.
Failed failure: the next status of the local block device that reports an I/O error. The next status is Diskless.
Negotiating: the instantaneous status before Attach reads data in the connected DRBD settings
Inconsistent: The data is Inconsistent, and a new resource is created immediately after the status appears on the two nodes (before the initial full synchronization. In addition, this status occurs on a node during synchronization (synchronization target ).
Outdated: The data resources are consistent, but they are Outdated.
DUnknown: this status occurs when the peer node's network connection is unavailable
Consistent: the data of an unconnected node is Consistent. When a connection is established, it determines whether the data is UpToDate or Outdated.
UpToDate: consistent latest data status, which is normal
3.10. Port Detection
10.24.6.4:
10.24.6.6:
3.11. initialize the network hard disk (executed by the master node)
Drbdadm -- overwrite-data-of-peer primary r0
View synchronization progress
Cat/proc/drbd
Ds: UpToDate/Inconsistent indicates that two nodes are synchronizing data.
3.12. Enable and disable resources (not required)
###### Manually enable resources
Drbdadm up <resource>
###### Manually disable Resources
Drbdadm down <resource>
Note:
Resource: indicates the resource Name. Of course, you can also use all to indicate [disabled | enabled] all resources.
3.13. Upgrade and downgrade resources (not required)
###### Upgrade Resources
Drbdadm primary <resource>
###### Downgrade a resource
Drbdadm secondary <resource>
Note: For DRBD in single-master mode, the two nodes are connected at the same time. Any node can be changed to the master node within a specific period of time, but only one of the two nodes can be the master node, if you already have a master, you need to downgrade it before you can upgrade it. This restriction is not imposed in dual-master mode.
3.14. How to view the resource connection status
[Root @ nod1 ~] # Drbdadm cstate r0 # r0 is the Resource Name
Connected
Status meaning:
The connection status of the resource. A resource may have one of the following connection statuses:
StandAlone independent: network configuration is unavailable; resources are not connected or managed to be disconnected (using the drbdadm disconnect command), or authentication failure or split-brain occurs.
Disconnecting disconnected: the disconnection is only a temporary state, and the next state is StandAlone.
Unconnected suspended: it is a temporary status before the connection attempt. The next status may be WFconnection and WFReportParams.
Timeout: the connection to the peer node times out and is also in the temporary status. The next status is Unconected.
BrokerPipe: the connection to the peer node is lost, which is also a temporary state. The next state is Unconected.
NetworkFailure: the temporary status after the connection is pushed to the peer node. The next status is Unconected.
ProtocolError: temporary status after the connection is pushed to the peer node. The next status is Unconected.
TearDown disassembly: temporary state. The peer node is closed, and the next state is Unconected.
WFConnection: waiting to establish a network connection with the peer node
WFReportParams: a TCP connection has been established. This node is waiting for the first network package from the peer node.
Connected connection: DRBD has established a connection. The data image is now available and the node is in normal state.
StartingSyncS: full synchronization. A synchronization has just started initiated by an administrator. In the future, the possible status will be SyncSource or PausedSyncS.
StartingSyncT: full synchronization. An administrator has just started synchronization and the next state is WFSyncUUID.
WFBitMapS: Some synchronization has just started. The next step may be in the SyncSource or PausedSyncS status.
WFBitMapT: Some synchronization has just started. The next step may be in the WFSyncUUID status.
WFSyncUUID: synchronization is about to begin. The next step may be SyncTarget or PausedSyncT.
SyncSource: synchronization on this node is in progress
SyncTarget: synchronization with the current node as the synchronization target is in progress
PausedSyncS: The local node is a continuous synchronization source, but the synchronization has been paused. It may be because another synchronization is in progress or the synchronization is paused using the command (drbdadm pause-sync ).
PausedSyncT: The local node is the target for continuous synchronization, but the synchronization has been paused. This can be because another synchronization is in progress or the synchronization is paused using the command (drbdadm pause-sync ).
VerifyS: Online device verification with local node as verification source is being executed
VerifyT: Online device verification with local nodes as the verification target is being executed
3.15. Resource synchronization completed
10.24.6.4:
10.24.6.6:
3.16. Data synchronization test
After formatting the disk, you can write data to it. The data is synchronized to the slave node.
Mkfs. ext4/dev/drbd0
Mount/dev/drbd0/mnt/data
(Note: only the primary disk node can be attached to the disk)
Cd/mnt/data
Mkdir testfile // create a test file named testfile
Umount/dev/drbd0 // detach a disk
Drbdadm secondary r0 // master downgrade, that is, the disk of the master node is the master disk at the beginning. Now it is downgraded to the slave disk, writing data to the master disk, and reading data from the slave disk. On drbd02 (slave node)
Drbdadm primary r0 // The master node is upgraded from the disk of the node to the master disk so that data on the disk can be read from the machine of the node.
Mount/dev/drbd0/mnt/data // after the master is upgraded, the permission cd/mnt/data ls-l will be available.
We will see the folder named testfile created in the master node. (For a moment:
To back up master-slave Mysql Data, we should be inspired here, I only need to save Mysql data to/mnt/data (we will put/dev/drbd0 IN THE PATH) so that the primary Mysql can write data to this region, drbd will automatically synchronize the data to the slave node.
Mysql on the server also reads data from here. In this way, two data copies are automatically backed up. )
3.17. Problem
Master downgrade failed:
Because the degraded directory is in use
4. MYSQL settings 4.1. Change the storage location of the Mysql database to a shared directory (executed on both the master and slave nodes)
By default, the storage location of the database is/var/lib/mysql (in/etc/mysql/my. the datadir location in cnf is found). Now you need to change it to exist in/share_mysql.
Copy data under/var/lib/mysql to/share_mysql
Cp-r/var/lib/mysql/share_mysql
Vim/etc/mysql/my. cnf
Change the value of datadir to/share_mysql/mysq.
4.2. Grant database users access permissions
4.3. System-level modification Permissions
140602 22:36:39 mysqld_safe Starting mysqld daemon with databases from/home/mysql
140602 22:36:39 [Warning] Can't create test file/home/mysql/localhost. lower-test
140602 22:36:39 [Warning] Can't create test file/home/mysql/localhost. lower-test
/Usr/libexec/mysqld: Can't change dir to '/home/mysql/' (Errcode: 13)
140602 22:36:39 [ERROR] Aborting
140602 22:36:39 [Note]/usr/libexec/mysqld: Shutdown complete
140602 22:36:39 mysqld_safe mysqld from pid file/var/run/mysqld. pid ended
You have modified the value of datadir in my. cnf.
You have already chown and chmod the owner and permissions of the new data directory or its parent path several times.
You have tried service mysql start countless times, or/etc/init. d/mysql start, and mysql_install_db!
Congratulations! You saw this article. I found a solution after being stuck in the system for a few hours.
There are two reasons for this. Either of these causes will cause you to be notified of this warning by the system. If you are not a professional linux system security engineer or you are just a PHP programmer and have not studied system security in depth, you will not find the answer easily.
First, selinux, remember to install RedHat on the Character interface when I was studying (a very old operating system ...) When there is such an option, we usually listen to the advice of the predecessors, change the default value to do not install it. However, if selinux is enabled on the machine that you want to operate on, it does not allow your mysql to operate on mysql_install_db at the new target location, and a warning is displayed in the title. A simple solution is to temporarily disable selinux using commands so that your operations can continue.
Setenforce 0
However, it is best to use a permanent method so that this product can be retained after restart.
Modify selinux = disabled in the/etc/SELINUX/config file, and restart or wait for the next restart.
Second, apparmor is the same as selinux. It also limits the directory permissions that mysql can use.
These two lines are available in the/etc/apparmor. d/usr. sbin. mysqld file, which specifies the data file path permission used by mysql.
/Var/lib/mysql/r,
/Var/lib/mysql/** rwk,
You must have seen that/var/lib/mysql/is the default path of the data file installed in mysql. apparmor controls the directory permissions that mysqld can use.
I want to move the data file to/data/mysql. In order to enable mysqld TO USE THE/data/mysql directory, add the following two files as shown above.
/Data/mysql/r,
/Data/mysql/** rwk,
Restart apparmor,/etc/init. d/apparmor restart
Then you can do what you want!
4.4. Database Synchronization Test
Start mysql on 10.24.6.4 to create a database named mongo_mysql.
The first step is to ensure that 10.24.6.4 is primary and 10.24.6.6 is secondary.
Master-slave switchover mounting
Service mysql stop # disable the mysql service
Drbdadm secondary r0 # downgrade 10.24.6.4 to backup
Umount/dev/drbd0 # detach a virtual hard disk
On drbd02
Drbdadm primary r0 # Upgrade 10.24.6.6
Mount/dev/drbd0 # mount a virtual hard disk
Service mysql start # start a database
Go to mysql to check whether a database named test has been synchronized from drbd01.
OK. Now the test is complete.
(Long-winded: A smart reader may ask, the efficiency of such operations is too low every time. Right, heartbeat will automatically help us do these things and it will listen to the service, once the service is stopped, the slave service will take over the service)
For more details, please continue to read the highlights on the next page: