一.linux 上安装 mysql
1.查看mysql是否安装
rpm -qa|grep mysql
2.卸载 mysql
yum remove mysql mysql-server mysql-libs mysql-common
rm -rf /var/lib/mysql
rm /etc/my.cnf
3.安装mysql
下载mysql的官方 yum respository
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
安装 yum respository
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum安装mysql
yum -y install mysql-community-server
4. mysql 设置
首先启动mysql
systemctl start mysqld.service
查看mysql状态
systemctl status mysqld.service
在日志文件中找出密码:
grep "password" /var/log/mysqld.log
进入数据库:
mysql -uroot -p # 回车后会提示输入密码
修改密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
MySQL完整的初始密码规则可以通过如下命令查看:
mysql> SHOW VARIABLES LIKE ‘validate_password%';
修改密码规则
set global validate_password_policy=0;
set global validate_password_length=1;
ALTER USER 'root'@'localhost' IDENTIFIED BY ‘root’;
卸载 yum respository
yum -y remove mysql57-community-release-el7-10.noarch
二.mysql 主从复制
1.环境配置
克隆并配置两台主机,将来一个作Master,一个作Slave.并关闭防火墙
systemclt disable firewalld
2.修改master主机
修改Master主机的my.cnf
[mysqld]
#port=3306
character-set-server=utf8
log-bin=/var/mysql/log/mysql-bin.log
server-id=1 #唯一,建议 ip 地址最后一位
创建/var/mysql/log目录
mkdir -p /var/mysql/log
修改/var/mysql目录的所有者
chown -R mysql:mysql ./mysql
重启Master的mysqld服务
systemctl restart mysqld
3.修改 slave 主机
修改Slave主机的my.cnf
[mysqld]
#port=3306
character-set-server=utf8
server-id=2 #ip 地址最后一位
修改Slave主机中MySQL的UUID
UUID,通用唯一识别码
cat /var/lib/mysql/auto.cnf
mv /var/lib/mysql/auto.cnf /var/lib/mysql/auto.cnf.bak
重启Slave的mysqld服务
systemctl restart mysqld
4.master 授权 slave 复制权限
登录Master的mysql
mysql -uroot -proot
授权
grant replication slave on *.* to 'root'@'192.168.181.138' identified by 'root';
重启Master的mysqld服务
systemctl restart mysqld
再次登录Master的mysql
mysql -uroot -proot
查看master状态
show master status;
5.Slave指定Master
登录Slave的mysql
mysql -uroot -proot
指定master
change master to master_host='192.168.181.137', master_user='root', master_password='111', master_log_file='mysql-bin.000002', master_log_pos=154;
重启Slave的mysqld服务
systemctl restart mysqld
登录Slave的mysql
mysql -uroot -p111
开启Slave
start slave;
查看Slave状态
show slave status;