1.下载mysql 安装包
2.安装mysql
2.1 解压
tar -zxvf mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz
2.2 复制到 /usr/local/mysql 目录
cp -r mysql-5.6.41-linux-glibc2.12-x86_64 /usr/local/mysql
2.3 添加用户组
groupadd mysql
2.4 添加用户
useradd -g mysql mysql
2.5 创建目录
cd /usr/local/mysql/
mkdir ./data/mysql
chown -R mysql:mysql ./
apt-get install libaio1 libaio-dev
./scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/mysql
2.6 复制mysql.server
cp support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
cp support-files/my-default.cnf /etc/my.cnf
2.7 修改启动脚本
vi /etc/init.d/mysqld
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data/mysql
2.8 启动服务
service mysqld start
2.9 测试连接
./mysql/bin/mysql -uroot
修改root 密码
set password for root@localhost = password('123');
执行下边的命令,让远程可以用root连接:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123' WITH GRANT OPTION;
在master和slave中都创建一个名字叫HA的数据库
2.10 将mysql 加入到环境变量
export PATH=$PATH:/usr/local/mysql/bin
source /etc/profile
克隆刚才配置的虚拟机,修改auto.cnf 的uuid , 使master和slave的id 不同
vi /usr/local/mysql/data/mysql/auto.cnf
3.修改master 的my.cnf
vi /etc/my.cnf
server_id =1
log_bin=mysql-bin
binlog-do-db=HA
binlog-ignore-db=mysql
4.重启master 的mysql(master 服务器)
service mysqld restart
5.验证(master 服务器)
cd /usr/local/mysql/bin/
./mysql -uroot -p123
mysql> show variables like 'server_id';
mysql> show master status;
6.添加一个同步用户slaveUser并赋值权限(用于从服务器slave使用)(master 服务器)
mysql>create user slaveUser ;
mysql>grant replication slave on *.* to 'slaveUser'@'192.168.117.145' identified by '123';
7.修改slave 的 mycnf
vi /etc/my.cnf
server_id = 2
log-bin=mysql-bin
replicate-do-db=HA #要同步的mstest数据库,要同步多个数据库,就多加几个replicate-db-db=数据库名
replicate-ignore-db=mysql #要忽略的数据库
8.重启slave 的mysql
service mysqld restart
9.校验slave
mysql> show variables like 'server_id';
10. 修改slave参数
mysql> stop slave;
mysql> change master to master_host='192.168.117.143',master_user='slaveUser',master_password='123',master_log_file='mysql-bin.000001' ,master_log_pos=732;
其中master_log_file 和master_log_pos 是在master服务器中执行show master status; 得到的
mysql> start slave;
11. 重启slave mysql server: service mysqld restart
12.验证(slave)
mysql> show slave status G
在master的HA数据库中创建几条记录,检查slave中的数据
MySQL登录时出现 Access denied for user 'root'@'xxx.xxx.xxx.xxx' (using password: YES):
grant all privileges on *.* to root@'%' identified by '123';
[ERROR] Slave SQL: Slave failed to initialize relay log info structure from the repository, Error_code: 1872
reset slave
mysql>stop slave;
mysql>reset slave;
mysql>change master to master_host='192.168.117.143',master_user='slaveUser',master_password='123',master_log_file='mysql-bin.000001' ,master_log_pos=732;
mysql>start slave;
show slave status G