两台虚拟机192.168.183.131和192.168.183.132,装完系统之后直接把所有开发包都装上
下载软件包mysql-5.6.10.tar.gz,cmake-2.8.10.2.tar.gz(从5.5开始mysql使用cmake来进行编译了而不是之前的configure)
mysql的编译安装
1.首先安装cmake
- [root@localhost ~]# tar -xvf cmake-2.8.10.2.tar.gz
- [root@localhost ~]# cd cmake-2.8.10.2
- [root@localhost cmake-2.8.10.2]# ./configure
- [root@localhost cmake-2.8.10.2]# make;make install
2.安装mysql
1)创建mysql用户和组
- [root@localhost ~]# groupadd mysql
- [root@localhost ~]# useradd -g mysql mysql
- [root@localhost ~]# mkdir /usr/local/mysql
- [root@localhost ~]# chown -R mysql.mysql /usr/local/mysql
2)解压缩mysql,进行编译安装
- [root@localhost ~]# tar -xvf mysql-5.6.10.tar.gz
- [root@localhost ~]# cd mysql-5.6.10
- [root@localhost ~]# cmake \
- -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
- -DMYSQL_DATADIR=/user/local/mysql/data \
- -DSYSCONFDIR=/etc \
- -DEXTRA_CHARSETS=all \
- -DDEFAULT_CHARSET=utf8 \
- -DDEFAULT_COLLATION=utf8_general_ci \
- -DWITH_INNOBASE_STORAGE_ENGINE=1 \
- -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
- -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
- -DWITH_FEDERATED_STORAGE_ENGINE=1 \
- -DWITH_PARTITION_STORAGE_ENGINE=1 \
- -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
- -DMYSQL_UNIX_ADDR=/var/run/mysqld/mysqld.sock \
- -DMYSQL_TCP_PORT=3306 \
- -DWITH_DEBUG=0 \
- -DENABLED_LOCAL_INFILE=1
- [root@localhost ~]# make;make install
- [root@localhost ~]# cd /usr/local/mysql/
- [root@localhost mysql]# chown -R root.mysql *
- [root@localhost mysql]# chown -R mysql.mysql data/
3)创建主配置文件和启动脚本
- [root@localhost data]# cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
- [root@localhost ~]# vi /etc/my.cnf
- basedir = /usr/local/mysql
- datadir = /usr/local/mysql/data
- port = 3306
- server_id = 131
- socket = /var/run/mysqld/mysqld.sock
- user = mysql
- log-error = /var/log/mysqld.log
- [root@localhost ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
- [root@localhost data]# vi /etc/init.d/mysqld
- basedir=/usr/local/mysql
- datadir=/usr/local/mysql/data
- mysqld_pid_file_path=/var/run/mysqld/mysqld.pid
4)把mysql的相关文件录入系统
配置mysql命令的搜索路径
打开/etc/profile文件找到export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC在这句之前添加export PATH=$PATH:/usr/local/mysql/bin 就会对所有用户有效并且开机自动生效了
告知系统mysql库文件的存放位置
编辑/etc/ld.so.conf.d/mysql.conf 添加/usr/local/mysql/lib
ldconfig -v |grep mysql
ldconfig会按照系统库文件的搜索路径把系统中所有的库文件重新加载一遍
告知系统mysql头文件的位置
ln -sv /usr/local/mysql/include /usr/include/mysql
配置mysql的man手册页
vi /etc/man.config添加 MANPATH=/usr/local/mysql/man
5)初始化数据库并且启动
- [root@localhost scripts]# pwd
- /usr/local/mysql/scripts
- [root@localhost scripts]# mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
- [root@localhost ~]# chkconfig --add mysqld
- [root@localhost ~]# service mysqld start
6)配置第二台mysql,直接复制过去
- [root@localhost ~]# scp -r /etc/my.cnf 192.168.183.132:/etc
- [root@localhost ~]# scp -r /etc/init.d/mysqld 192.168.183.132:/etc/init.d
- [root@localhost ~]# scp -r /usr/local/mysql 192.168.183.132:/usr/local
- [root@localhost ~]# scp -r /etc/ld.so.conf.d/mysql.conf 192.168.183.132:/etc/ld.so.conf.d/
- [root@localhost ~]# groupadd mysql
- [root@localhost ~]# useradd -g mysql mysql
- [root@localhost ~]# chown -R mysql.mysql /usr/local/mysql
- [root@localhost ~]# cd /usr/local/mysql
- [root@localhost ~]# chown -R root.mysql *
- [root@localhost ~]# chown -R mysql.mysql data/
在132上面把/etc/my.cnf配置文件中的server-id改成132,然后重复进行第四部
主从复制的配置
1.把131配置成主库,把132配置成从库
1)在131上面打开二进制日志
修改配置文件/etc/my.cnf,添加log-bin = mylog.000001
重启mysql
2)然后建一个用户,授权可以读取自己的二进制日志
- mysql> grant replication slave on *.* to slave@'192.168.183.132' identified by 'mapabc';
- Query OK, 0 rows affected (0.01 sec)
- mysql> flush privileges;
- Query OK, 0 rows affected (0.00 sec)
3)从库上面登录测试
- [root@localhost ~]# mysql -u slave -p -h 192.168.183.131
- Enter password:
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | test |
- +--------------------+
- 2 rows in set (0.00 sec)
4)主库上面查看当前的POS号
- mysql> show master status;
- +--------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +--------------+----------+--------------+------------------+-------------------+
- | mylog.000001 | 412 | | | |
- +--------------+----------+--------------+------------------+-------------------+
- 1 row in set (0.00 sec)
5)在从库上面配置
- mysql> CHANGE MASTER TO
- -> MASTER_HOST='192.168.183.131',
- -> MASTER_USER='slave',
- -> MASTER_PASSWORD='mapabc',
- -> MASTER_LOG_FILE='mysql.000001',
- -> MASTER_LOG_POS=412;
- Query OK, 0 rows affected, 2 warnings (0.08 sec)
6)查看从库状态并启动从库
- mysql> show slave status\G;
- *************************** 1. row ***************************
- Slave_IO_State:
- Master_Host: 192.168.183.131
- Master_User: slave
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File: mysql.000001
- Read_Master_Log_Pos: 412
- Relay_Log_File: mysqld-relay-bin.000001
- Relay_Log_Pos: 4
- Relay_Master_Log_File: mysql.000001
- Slave_IO_Running: No
- Slave_SQL_Running: No
- mysql> start slave;
- mysql> show slave status\G;
- *************************** 1. row ***************************
- Slave_IO_State: Waiting for master to send event
- Master_Host: 192.168.183.131
- Master_User: slave
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File: mylog.000001
- Read_Master_Log_Pos: 412
- Relay_Log_File: relay-bin.000002
- Relay_Log_Pos: 279
- Relay_Master_Log_File: mylog.000001
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
7)测试
主库131创建数据库
- mysql> create database db5;
- Query OK, 1 row affected (0.00 sec)
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | db5 |
- | mysql |
- | performance_schema |
- | test |
- +--------------------+
- 5 rows in set (0.00 sec)
从库上面查看
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | db5 |
- | mysql |
- | performance_schema |
- | test |
- +--------------------+
- 5 rows in set (0.01 sec)
已经同步过来了。
把132配置成主库,把131配置成132的从库
1)对于主从复制来说,主库可以接受读写,从库只能读不能写,一旦从库有写入操作,造成主从库数据不一致,复制立马就断开了,我们就在131上面建立一个授权帐号让它同步到132上面
- mysql> grant replication slave on *.* to slave@'192.168.183.131' identified by 'mapabc';
- Query OK, 0 rows affected (0.00 sec)
2)打开132的二进制日志,把132配置成主库,然后重启132的mysql
3)在131上面配置
- mysql> CHANGE MASTER TO
- -> MASTER_HOST='192.168.183.132',
- -> MASTER_USER='slave',
- -> MASTER_PASSWORD='mapabc',
- -> MASTER_LOG_FILE='mylog.000001',
- -> MASTER_LOG_POS=120;
- Query OK, 0 rows affected, 2 warnings (0.12 sec)
- mysql> start slave;
- Query OK, 0 rows affected (0.01 sec)
4)测试
在132上面创建数据库
- mysql> create database db4;
- Query OK, 1 row affected (0.01 sec)
在131上面查看,看到已经同步过来了
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | db4 |
- | db5 |
- | mysql |
- | performance_schema |
- | test |
- +--------------------+
- 6 rows in set (0.00 sec)
至此,主主同步配置完成。