linux 6 安装 Mysql 主从复制服务

Mysql主从复制


 

1,环境

主服务器master:192.168.10.23

从服务器slave:192.168.10.18


 

2.安装Mysql

避免安装之后可能出现的各种异常,请清理掉MySQL的历史安装文件,

yum方式安装的MySQL
1.1 终端输入yum list installed mysql*查看历史安装列表;
1.2 使用yum remove [name]命令卸载安装包;

rpm方式安装的MySQL
2.1 终端输入rpm -qa | grep -i mysql命令查看历史安装列表;
2.2 使用rpm -ev [name]命令卸载安装包;

重点
3.1 终端输入 find / -name mysql命令查看各种分散的mysql安装文件或文件夹;
3.2 使用rm -rvf [name]命令删除所有的msyql残余文件;
3.3 输入find / -name my.cnf命令查看残余的mysql配置文件,然后使用rm -rvf [name]删除这些配置文件.

在主从节点服务器上分别安装MySQL,并保证两台服务的时间一致。

# yum -y install mysql mysql-devel mysql-server
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.cn99.com
base                                                             | 3.7 kB     00:00     
extras                                                           | 3.4 kB     00:00     
updates                                                          | 3.4 kB     00:00     
Package mysql-5.1.73-8.el6_8.x86_64 already installed and latest version
Package mysql-devel-5.1.73-8.el6_8.x86_64 already installed and latest version
Package mysql-server-5.1.73-8.el6_8.x86_64 already installed and latest version
Nothing to do
# mysql --version
mysql  Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1
# 

3.配置

配置Master的mysql配置文件,/etc/my.cnf

# cat /etc/my.cnf
[mysqld]

datadir = /data/mysql                                   #数据库地址目录

socket = /var/lib/mysql/mysql.sock

user = mysql                                             #mysql 运行用户

#Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links = 0                            #0表示禁用符号连接

log-bin = mysql-bin                         #启用二进制日志

server-id = 1                                   #服务器ID,可1-之间的任意整数

auto_increment_offset = 1

auto_increment_increment = 2

[mysqld_safe]

log-error = /var/log/mysqld.log

pid-file = /var/run/mysqld/mysqld.pid

replicate-do-db = all                     #同步的数据库
# 

创建刚才设置的数据库目录,并赋予mysql用户权限

#  mkdir /data/mysql/ -p
#  chown -R mysql:mysql /data/mysql/
# ll -d /data/mysql/
drwxr-xr-x. 6 mysql mysql 4096 Sep 18 18:42 /data/mysql/
# 

关闭防火墙,selinx,不然会mysql启动会报错

[Warning] Can't create test file /data/mysql/server2.lower-test

如果mysql已有旧的数据库,再启动会报错:

Table 'mysql.plugin' doesn't exist

利用mv -r /var/lib/mysql /目的地把已有的/var/lib/mysql下的数据库移动到别的地方,或者直接用rm -rf /var/lib/mysql 删除,然后再重新启动 service mysqld restart 就可以了。

重启mysql服务

# /etc/init.d/mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
# ps -ef | grep mysql
root 15393 7145 0 16:42 pts/1 00:00:00 mysql
root 17368 1 0 18:42 pts/0 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/data/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql
mysql 17485 17368 0 18:42 pts/0 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/data/mysql --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root 17543 16800 0 18:45 pts/0 00:00:00 grep mysql
#

 

mysql启动后,进入mysql建立帐户并授权slave,查询出master 的file和position参数。

# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73-log Source distribution
mysql> 
mysql>
mysql> grant replication slave on *.* to 'tongbu'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000008 |      255 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

mysql> 

 

配置从储服务器slave的mysql的配置

# cat /etc/my.cnf
[mysqld]

datadir = /data/mysql                             #数据库地址跟主服务器一致

socket = /var/lib/mysql/mysql.sock

user = mysql

#Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links = 0

log-bin = mysql-bin

server-id = 2                                     #服务器ID,更换一个

auto_increment_offset = 2

auto_increment_increment = 2

[mysqld_safe]

log-error = /var/log/mysqld.log

pid-file = /var/run/mysqld/mysqld.pid

master-host = 192.168.10.23

master-user = tongbu

master-pass = 123456

master-port = 3306

master-connect-retry = 60

replicate-do-db = all

同样创建刚才设置的数据库目录,并赋予mysql用户权限。

关闭防火墙,selinx,启动mysql

[root@nagios usr]# service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
# 

进入mysql,设置同步源,启动同步,并查询状态

# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73-log Source distribution
mysql> change master to master_host='192.168.10.23',master_user='tongbu',master_password='123456',master_log_file='mysql-bin.000008',master_log_pos=255;
Query OK, 0 rows affected (0.03 sec)

mysql> 
mysql> slave start;
Query OK, 0 rows affected (0.00 sec)

mysql>  show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.10.23
                  Master_User: tongbu
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000008
          Read_Master_Log_Pos: 255
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000008
             Slave_IO_Running: Yes                                        #
            Slave_SQL_Running: Yes                                        #YES表示已与主服务器数据库连接成功
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 255
              Relay_Log_Space: 407
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
1 row in set (0.00 sec)

mysql> 

4.测试

在主数据库上新增一个数据库表,查看从数据库有没有同步

mysql> create database test01;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
| test01             |
+--------------------+
4 rows in set (0.00 sec)

mysql> 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
| test01             |
+--------------------+
4 rows in set (0.00 sec)

mysql>

这样简单的Mysql主从服务就搭建好了。

posted @ 2019-09-18 19:09  Justtosee  阅读(211)  评论(0编辑  收藏  举报