一、目标

mysql安装和amoeba配置。两台mysql服务器(主从复制),amoeba路由分发并且读写分离。

二、准备

三台虚拟机

amoeba 192.168.48.153 (hostname hadoop1)

mysql1(master) 192.168.48.156 (hostname vm1)

mysql2(slave) 192.168.48.156 (hostname vm2)

三、mysql安装


linux下安装Mysql - 景岳 - 博客园www.cnblogs.com


mysql怎么当前读 mysql读文件_MySQL


3.1、源码安装

mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz

解压包之后cp到/usr/local/mysql下


[root@vm1 mysql]# cp -r mysql-5.6.33-linux-glibc2.5-x86_64  /usr/local/mysql


3.2、创建用户用户组


[root@vm1 mysql]# groupadd mysql
[root@vm1 mysql]# useradd mysql -g mysql


3.3、修改用户权限/usr/local/mysql,执行安装命令


[root@vm1 mysql]# chown -R mysql:mysql ./
[root@vm1 mysql]# ./scripts/mysql_install_db --user=mysql


3.4、修改权限

修改权限为root:root,data文件夹例外


[root@vm1 mysql]# chown -R root:root ./
[root@vm1 mysql]# chown -R mysql:mysql data/


3.5、添加软连接和service


[root@vm1 mysql]# cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysql
[root@vm1 mysql]# ln -s /usr/local/mysql/bin/mysql  /usr/local/bin/mysql


3.6、启动mysql


[root@vm1 mysql]# service mysql start
Starting MySQL.. SUCCESS!


修改默认密码(默认没有密码),先进mysql的bin目录下


[root@vm1 mysql]# cd bin/
[root@vm1 bin]# ./mysqladmin -u root password 1234
./mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!


日志报socket文件有问题,我们看一下/etc/my.cnf文件的socket路径,再做个软链接。就可以了


mysql怎么当前读 mysql读文件_mysql_02


添加软链接:


[root@vm1 bin]# ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock


再次执行命令:


[root@vm1 bin]# ./mysqladmin -u root password 1234
Warning: Using a password on the command line interface can be insecure


有个警告,我们可以暂时忽略。密码修改完成可以登录:


mysql怎么当前读 mysql读文件_mysql_03


至此,mysql安装结束。(还没有开放对外远程连接,下文)

四、主从复制

4.1、根据步骤三,将master和slave两台服务器mysql安装完毕之后,开始配置主从复制。

4.2、master节点

修改/etc/my.cnf文件,在[mysqld]下面添加三行

注意:一点是需要在[mysqld]下面添加,因为/etc/my.cnf下有还有一个[mysqld_safe],写错位置会失效,影响后续的marster节点信息。


log-bin=mysql-bin
server_id=1
binlog-ignore-db=mysql


重启mysql


[root@vm1 ~]# service mysql restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!


连接上mysql,执行命令查询master状态(mysql命令show master status)


[root@vm1 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.6.33-log MySQL Community Server (GPL)
 
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
 
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      120 |              | mysql            |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
 
mysql>


出现master状态即说明my.cnf配置生效了。要记住file名和position两个参数值,slave中需要用到此信息来连接。

接下来需要给slave创建一个帐号让slave可以访问master。因为主从复制是基于二进制文件的,slave始终监听者主程序的二进制日志,根据master日志的变动,则会把变化复制到自己的中继日志中,然后slave的一个SQL线程会把相关的“事件”执行到自己的数据库中,以此实现从数据库和主数据库的一致性,也就实现了主从复制。如下图:


mysql怎么当前读 mysql读文件_mysql_04


执行mysql命令为slave添加账户。


mysql>  grant replication slave on *.* to 'test_slave'@'192.168.48.157' identified by '1234';
Query OK, 0 rows affected (0.01 sec)


至此,master节点配置结束。

4.3、slave节点

slave节点同样需要修改/etc/my.cnf文件,在[mysqld]下添加(指定serverid,要复制的库,要忽略的库)


server-id=2 #此值不能和主数据库的一样,唯一 
replicate-do-db = test #可以指定要复制的库 
replicate-ignore-db = mysql #忽略的库


保存文件并重启slave mysql。

slave执行mysql 命令。开启slave


CHANGE MASTER TO MASTER_HOST='192.168.48.156',MASTER_USER='test_slave',MASTER_PASSWORD='1234',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=120;


mysql> CHANGE MASTER TO MASTER_HOST='192.168.48.156',MASTER_USER='test_slave',MASTER_PASSWORD='1234',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=120;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
 
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)


查看slave状态:


mysql> show slave statusG;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.48.156
                  Master_User: test_slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 120
               Relay_Log_File: vm2-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          Replicate_Ignore_DB: mysql
           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: 120
              Relay_Log_Space: 120
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 1593
                Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
               Last_SQL_Errno: 0
               Last_SQL_Error: 
......


发现有误了,有重复的mysql server UUID。就是master和slave 的mysql uuid是一样的。因为我虚拟机是克隆的,导致了这个问题。

所以去/var/lib/mysql文件夹下面去查看auto.cnf文件中的servier-uuid。


mysql怎么当前读 mysql读文件_mysql_05


确定是一致的,如何解决呢?将次cnf文件重命名即可,重启mysql会自动创建新的。


[root@vm2 mysql]# mv auto.cnf  auto.cnf.bk
[root@vm2 mysql]# service mysql restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@vm2 mysql]# ll
total 110636
-rw-rw---- 1 mysql mysql       56 Mar 29 02:27 auto.cnf
-rw-rw---- 1 mysql mysql       56 Mar 29 01:22 auto.cnf.bk
-rw-rw---- 1 mysql mysql 12582912 Mar 29 02:27 ibdata1
...


OK,再次进入mysql命令行。


mysql> show slave statusG;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.48.156
                  Master_User: test_slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 337
               Relay_Log_File: vm2-relay-bin.000003
                Relay_Log_Pos: 500
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          Replicate_Ignore_DB: mysql
...