单节点配置
1.下载yum源
下载地址:https://dev.mysql.com/downloads/repo/yum/
2.安装yum源
rpm -Uvh platform-and-version-specific-package-name.rpm
rpm -Uvh mysql80-community-release-el7-2.noarch.rpm
3.修改yum源中要安装的版本
vim /etc/yum.repos.d/mysql-community.repo
enable=1为默认按照的版本,=0非默认安装的版本
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
4.查看yum中mysql版本信息
yum repolist all | grep mysql ##查看yum中的版本信息
5.安装mysql
yum install mysql-community-server
到这里时有些会报一下错
错误显示:
软件包:mysql-community-server-5.7.20-1.el6.x86_64 (mysql57-community)
需要:libsasl2.so.2()(64bit)
您可以尝试添加 --skip-broken 选项来解决该问题
您可以尝试执行:rpm -Va --nofiles --nodigest
解决方法:
修改vim /etc/yum.repos.d/mysql-community.repo 源文件
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/ 修改
enabled=1
gpgcheck=0 修改
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 修改
yum install mysql-community-server #再次安装mysql
6.修改mysql的密码策略
★★★注意★★★:mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements错误。
vim /etc/my.cnf
增加如下
validate_password=off
validate_password_policy=0
7.启动mysql
systemctl start mysqld 启动mysql
systemctl restart mysqld 重启mysql
systemctl stop mysqld 停止mysql
8.登陆mysql并修改mysql的密码
mysql本地初次登陆密码是自动生成的,需要进行第一次登陆修改。自动生成的密码在/var/log/mysqld.log文件中,
grep 'temporary password' /var/log/mysqld.log 获取初始密码
mysql -uroot -p 登陆
修改默认密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '1234';
mysql 相关命令
创建用户
create user userName identified by 'password';
给用户赋权
grant replication slave on *.* to 'userName'@'%' identified by 'password';
mysql相关的文件
/etc/my.cnf 配置文件
/var/log/mysql.log 日志文件
/var/lib/mysql/ 数据文件和二进制文件
mysql主从配置
主节点配置
创建同步用户并赋权
create user userName identified by 'password';
create user repl identified by 'repl';
grant replication slave on *.* to 'repl'@'%' identified by 'repl'; 给同步用户赋权
my.cnf配置
log-bin=mysql-bin 开启二进制日志文件
server-id=111 配置serverid 唯一 一般使用ip最后一段
重启mysql服务
登陆mysql 查询master信息
show master status;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
从节点配置
my.cnf配置
server-id=112
relay-log=slave-relay-bin
relay-log-index=slave-relay-bin.index
read_only=1
重启mysql服务
systemctl restart mysqld
登陆mysql服务配置同步
change master to master_host='192.168.62.111',master_port=3306,master_user='repl',master_password='repl',master_log_file='mysql-bin.000001',master_log_pos=154;
master_user 主库用户
master_password 同步用户的密码
master_log_file 主库二进制日志文件
master_log_pos 开始同步的位置
start slave;开始同步数据
show slave status \G 查看从库的状态