Pages

Thursday, December 28, 2023

Database Replication Master-Slave

Database replication is a strategy for maintaining multiple copies of data across different servers to ensure data availability and redundancy. In a master-slave setup, the master server handles all write operations while one or more slave servers handle read operations and act as a backup. This guide will take you through 10 simple steps to set up a master-slave replication in MySQL.


A. Master Server Configuration

Step 1: Configure MySQL Settings

Edit MySQL Configuration: Open /etc/my.cnf in a text editor and enter the following under the [mysqld] section:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server-id = 1
log-bin=db-bin
relay-log=db-relay-bin

Step 2: Grant Replication Privileges
2. Grant Slave Access:
Access MySQL (mysql -u root -p) and execute:
GRANT REPLICATION SLAVE ON *.* TO 'replication'@'SLAVE_IP' IDENTIFIED BY 'password'; FLUSH PRIVILEGES;

Replace SLAVE_IP with your slave server's IP address and password with a secure password.

Step 3: Lock Tables and Note Master Status
3. Lock Tables:
FLUSH TABLES WITH READ LOCK;

4. Check Master Status:
SHOW MASTER STATUS;

Note the File and Position values.

5. Unlock Tables:
UNLOCK TABLES;

B. Slave Server Configuration

Step 4: Configure MySQL Settings
Edit MySQL Configuration:Open /etc/my.cnf in a text editor and enter the following under the [mysqld] section
server-id=2
master-host = 10.10.3.21
master-user = replication
master-password = password
master-port = 3306 
10.10.3.21 is the MASTER server IP. These information will be stored to /var/lib/mysql/master.info

Step 5: Setup Replication on Slave
2. Access MySQL:
mysql -u root -p

Stop Slave Threads:
STOP SLAVE;

Reset Previous Master Info:
RESET SLAVE;

Change Master:
CHANGE MASTER TO MASTER_HOST='MASTER_IP', MASTER_USER='replication', MASTER_PASSWORD='password', MASTER_LOG_FILE='Master_Binlog_File', MASTER_LOG_POS=Master_Log_Position;

Replace MASTER_IP with your master server's IP address, Master_Binlog_File with the file noted earlier, and Master_Log_Position with the position noted earlier.

Start Slave Threads:
START SLAVE;

Step 6: Verify Slave Status Check Replication Status:
SHOW SLAVE STATUS \G;

Ensure Slave_IO_Running and Slave_SQL_Running are both set to Yes, and Seconds_Behind_Master is a non-negative number.


Conclusion
You've now successfully configured a basic master-slave replication setup. This configuration allows the slave to take over in case the master goes down, and it can also help with load balancing by directing read queries to the slave. Regularly check the replication status and backup your data to ensure everything operates smoothly. Remember, replication is just one part of a comprehensive disaster recovery plan.

Sending Email Through Telnet with Authentication

Sending an email through Telnet can be an insightful way to understand the communication between an email client and a server. This guide will take you through the process of sending an email via Telnet, including the authentication steps necessary for servers that require it.

Step 1: Connect to the Mail Server

  1. Open Command Prompt or Terminal.
  2. Connect to the email server using Telnet:
    telnet mail.sparkinnovators.com 25
    If the connection is successful, you should see a response from the SMTP server.

Step 2: Handshake with the Server

  1. Greet the mail server:
    ehlo me
    The server should respond with its capabilities. One of them should be AUTH LOGIN if it requires authentication.

Step 3: Authenticate with the Server

  1. Begin the authentication process:

    auth login

    The server will respond with 334 VXNlcm5hbWU6, prompting for your username (email address) in Base64 encoding.


  2. Send your Base64 encoded email address:

    • Convert your email address to Base64 (using a converter like this one).
    • Respond with the encoded string.
  3. Send your Base64 encoded password:

    • Convert your password to Base64 (using the same converter).
    • Respond with the encoded string.

    If successful, you should receive an "Authentication succeeded" message.

Step 4: Compose and Send the Email

  1. Set the sender email address:

    mail from:<email address you used above>
  2. Set the recipient email address:

    rcpt to:<mbressman@gmail.com>
  3. Start composing the message:

    Data

    After this command, you're typing the content of your email.

  4. Type the Subject and Body:

    Subject: this is a test
  5. test123 
  6. test456

To end the message, put a single period (.) on a line by itself and press Enter.

Step 5: Close the Connection

  1. Terminate the session:
    quit
    The server should confirm the termination and close the connection.

Important Notes:

  • Security Warning: Using Telnet and sending credentials in Base64 encoding is not secure. This method should only be used for testing in a secure and controlled environment.
  • Spam Filters: Many servers have strict rules to prevent spam. Emails sent this way may end up in the recipient's spam folder or be rejected entirely.
  • Legal and Ethical Considerations: Ensure you have permission to use the email server and that you're complying with all relevant laws and regulations.

This guide demonstrates how to send an email through Telnet, including the authentication step. Remember, for regular email sending, it's best to use a proper email client or script that can handle encryption and authentication to ensure secure and reliable delivery.

Install Imagick on Cpanel Server

Imagick is a PHP extension that utilizes the ImageMagick software suite for image processing. It's widely used for creating, editing, composing, and converting images. This guide will take you through the steps of installing ImageMagick and the Imagick PHP extension on a cPanel server.

Step 1: Installing ImageMagick

1. Access Your Server:
Log in to your server as the root user via SSH:

ssh root@server

2. Navigate to the Source Directory:

cd /usr/local/src/

3. Download ImageMagick:

wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz

4. Extract the Archive:

tar zxvf ImageMagick.tar.gz

5. Configure and Install:

Navigate to the ImageMagick directory (the name might vary based on the version):

cd ImageMagick-*

Now, compile and install ImageMagick:

./configure make make install

6. Install Perl Module:

If you need PerlMagick, the Perl interface for ImageMagick, continue with these steps:

cd PerlMagick perl Makefile.PL make make install

7. Confirm ImageMagick Installation:

Verify that ImageMagick is installed:

which convert

Expected output: /usr/local/bin/convert

Step 2: Installing Imagick PHP Extension

1. Download Imagick for PHP:

wget http://pecl.php.net/get/imagick-2.3.0.tgz

2. Extract the Archive:

tar -zxvf imagick-2.3.0.tgz

3. Navigate to the Imagick Directory:

cd imagick-2.3.0

4. Prepare Imagick for PHP:

phpize ./configure make make install

5. Update PHP Configuration:

Edit the php.ini file used by your PHP installation. The location of this file can vary, but typically it's found in /usr/local/lib/php.ini:

vi /usr/local/lib/php.ini

6. Add Imagick Extension:

In the php.ini file, add the following line to enable the Imagick extension:

extension="imagick.so"

Save and close the file.

Step 3: Restart the Web Server

After updating the PHP configuration, restart Apache to apply changes:

/scripts/restartsrv_httpd

Conclusion

You've successfully installed ImageMagick and the Imagick PHP extension on your cPanel server. This setup allows you to leverage powerful image processing capabilities directly from PHP scripts. Test the installation by running a PHP script that utilizes Imagick functions to ensure everything is working correctly. Remember to regularly update both ImageMagick and the Imagick extension to maintain security and functionality.