1、准备工作
groupadd mysql
useradd -r -g mysql mysql
mkdir /usr/local/mysql/ #创建mysql安装目录
mkdir /data/mysql #创建数据存放目录
chown mysql:mysql -R /data/mysql
yum install -y gcc gcc-c++ libtool autoconf automake imake libxml2-devel expat-devel ncurses-devel cmake bison
wget http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.43.tar.gz
2、解压源码包,编译安装
tar -zxvf mysql-5.5.43.tar.gz
cd mysql-5.5.43 #进入解压后mysql目录
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/data/mysql/ -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306
make && make install #编译通过过,安装
3、初始化MySQL
cp support-files/my-medium.cnf /etc/my.cnf #复制配置文件
cp support-files/mysql.server /etc/init.d/mysqld #复制启动脚本
chmod 755 /etc/init.d/mysqld
cd /usr/local/mysql #进入安装目录
chown -R mysql.mysql . # 授权
./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql #初始化数据库
/etc/init.d/mysqld start #启动数据库
4、MySQL配置
chkconfig --add mysqld #添加系统服务
chkconfig mysqld on #添加开机启动
export PATH=$PATH:/usr/local/mysql/bin #添加环境变量
echo 'PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
service mysqld start/stop
mysql主从配置
192.168.2.96 salve
192.168.2.97 master
1、首先配置主服务器[root@localhost ~]# service mysqld stop
[root@localhost ~]# vi /etc/my.cnf ##在[mysqld]段内添加如下内容(其实默认就是需要检查一下)
log-bin=mysql-bin###########启用二进制日志#############
server-id=1###########服务器唯一ID###########[root@localhost ~]# service mysqld start
Starting MySQL.. [确定]
[root@localhost ~]# mysqladmin -uroot -p password "123456"
#默认root用户密码为空,修改密码为123456 这里修改密码也可以直接登录进去 在重新授权
Enter password:
[root@localhost ~]# mysql -uroot -p ##登陆mysql终端,授权给从服务器,mysql> grant replication slave on *.* to 'root'@'192.168.2.96' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges; ###刷新
Query OK, 0 rows affected (0.00 sec)
mysql> show master status; ##查询主数据库状态
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 471 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
###这里需要记录 File 以及 Position 的值,在操作从服务器时会用到,以当时终端的为准###
2、配置从服务器[root@localhost ~]# service mysqld stop ##停止mysql数据库
Shutting down MySQL. [确定]
[root@localhost ~]# vi /etc/my.cnf ##在[mysqld]段内添加如下内容
[mysqld]
log-bin=mysql-bin ###########启用二进制日志#############
server-id=2 ###########服务器唯一ID###########
[root@localhost ~]# service mysqld start ##启动数据库
Starting MySQL.. [确定]
[root@localhost ~]# mysqladmin -uroot -p password "123456" #默认root用户密码为空,修改密码为123456
Enter password:
[root@localhost ~]# mysql -uroot -p ###登陆mysql终端,
###执行同步SQL语句###
mysql> change master to
-> master_host='192.168.2.97',
-> master_user='root',
-> master_password='123456',
-> master_log_file='mysql-bin.000004',
-> master_log_pos=471;
注意:这里完成后最后刷新权限 否则下面查看主从同步状态时显示:Slave_IO_Running: Connecting
mysql> flush privileges; ###刷新权限
Query OK, 0 rows affected (0.00 sec)
###启动Slave同步进程####
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
####主从同步检查####
mysql> show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
代表mysql主从搭建成功,测试主从服务器是否能够同步,就自己简单的测试一下即可 这里就不做演示了。
mysql主主配置
接着上面的继续简单操作几步即可完成主主配置(其操作步骤和搭建主从一样)
在从服务器上操作如下 (这次从服务器其实是当做主)mysql> grant replication slave on *.* to 'root'@'192.168.2.97' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 700 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
192.168.2.97服务器如下操作:mysql> change master to master_host='192.168.2.96', master_user='root', master_password='123456', master_log_file='mysql-bin.000004', master_log_pos=700;
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> start slave;
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.2.96
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 700
Relay_Log_File: linux-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: 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: 700
Relay_Log_Space: 409
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:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 2
1 row in set (0.00 sec)
OK!到这里mysql主主搭建完成
由于博主比较懒 mysql环境搭建就写在一个博文里面了 测试的话就自己简单测试下即可。有错误的地方请指出来 一块学习进步。