wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
# 解压
tar -xvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
# 移动
mv mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/
# 重命名
mv /usr/local/mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/mysql
mkdir /usr/local/mysql/data
# mysql用户组
groupadd mysql
# mysql用户
useradd mysql -g mysql
chown -R mysql.mysql /usr/local/mysql
/usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize
#如果有警告之类的忽略就行,只要没有error或者faile就代表初始化成功了
# 如果出现以下错误, 则执行下面的命令来安装numactl,然后再重新执行上面的mysqld命令
/usr/local/mysql/bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
yum -y install numactl
[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
user=mysql
port=3306
character-set-server=utf8
# 取消密码验证
skip-grant-tables
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# skip-grant-tables
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
# 将mysql加入服务
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
# 开机自启
chkconfig mysql on
# 启动mysql服务
service mysql start
# 登录(由于/etc/my.cnf中设置了取消密码验证,所以此处密码任意)
/usr/local/mysql/bin/mysql -u root -p
# 操作mysql数据库
>use mysql;
# 修改密码
>update user set authentication_string=password('你的密码') where user='root';
>flush privileges;
>exit;
service mysql start
/usr/local/mysql/bin/mysql -u root -p
>ALTER USER 'root'@'localhost' IDENTIFIED BY '修改后的密码';
>exit;
/usr/local/mysql/bin/mysql -u root -p
>use mysql;
>update user set host='%' where user = 'root';
>flush privileges;
>eixt;
ln -s /usr/local/mysql/bin/mysql /usr/bin
CREATE DATABASE `testdb` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
# 永久禁用防火墙(需要重启虚拟机)
systemctl disable firewalld.service
reboot
# 在虚拟机上查看一下IP地址
ifconfig
mysql -h 192.168.249.135 -u root -p
> show databases;
必须是创建完成的克隆
克隆完成之后需要修改第二台(192.168.249.136)和第三台(192.168.249.137)的mysql的uuid值,随便修改一下即可只要保证三台机器的uuid不同即可
vim /etc/my.cnf
[mysqld]
# servier-id 每台mysql都要不同
server-id=1
log-bin=mysql-bin
# 授权一个用户名为rep,密码为root
# *.*代表所有库的所有表(可以指定库.表)
# ip代表只能该ip段下有权限访问, 即配置从服务器的前两个ip段即可,我的两个slave都是172.16段
mysql> grant replication slave on *.* to 'rep'@'%' identified by 'root';
service mysql restart
# File 和 Position 两个字段比较重要
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 588| | | |
+------------------+----------+--------------+------------------+-------------------+
[mysqld]
server-id=2
log-bin=relay-bin
service mysql restart
# 如果之前已经配置过主从复制,需要重新复制需要先停止slave,然后再change master
mysql> stop slave;
#master_log_file='mysql-bin.000001':这是master库状态栏里面的File
#master_log_pos=588:这是master库状态栏里面的Position
mysql> change master to
master_host='192.168.249.135',
master_port=3306,
master_user='rep',
master_password='root',
master_log_file='mysql-bin.000001',
master_log_pos=588;
mysql> start slave;
mysql> show slave status\G;
修改/etc/my.cnf,其它步骤和slave1操作完全一样
[mysqld]
server-id=3
log-bin=relay-bin
# 在master上创建一个数据库
mysql> create database testdb;
# 在slave1和slave2上看testdb是否存在
mysql> show databases;