参考:https://blog.csdn.net/Amor_Leo/article/details/85161624
https://www.cnblogs.com/ygqygq2/p/6045279.html
安装mysql
1.清除Centos7默认数据库
rpm -qa |grep mariadb |xargs yum remove -y
2.将mysql压缩包放到 /home 目录下,并解压;或者下载mysql压缩包并解压
3.在 /usr/local 目录下创建 mysql 文件夹
mkdir mysql
4.将解压的mysql文件夹下面的所有文件cp到 /usr/local/mysql 文件夹下面
cd /home/mysql-5.7.24-linux-glibc2.12-x86_64
cp -rf * /usr/local/mysql
5.在mysql文件夹下面创建data文件夹
6.创建mysql用户组
groupadd mysql
7.创建mysql用户
useradd -r -g mysql mysql
8.修改 /usr/local/mysql 目录的拥有者为mysql用户
cd /usr/local/mysql
chown -R mysql:mysql ./
9.安装mysql
./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize
记住初始密码
10.修改 /etc/my.cnf
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
lower_case_table_names = 1#设置忽略大小写(简单来说就是sql语句是否严格)
bind-address = 0.0.0.0# 开启ip绑定
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/usr/local/mysql/data/mysqld.pid
[client]
socket=/usr/local/mysql/mysql.sock
default-character-set=utf8
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
11.添加mysql开机启动
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
12.修改mysqld文件
vi /etc/init.d/mysqld
添加
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
13.放行端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
14.启动mysql
service mysqld start
15.设置mysql开机启动
chkconfig --add mysqld
16.配置mysql环境变量
vi /etc/profile
在最后添加 export PATH=$PATH:/usr/local/mysql/bin
17.配置刷新
source /etc/profile
18.重启mysql
service mysqld restart
19.登陆mysql
mysql -u root -p
输入原始密码
20.修改密码
alter user ‘root’@‘localhost’ identified by ‘root’;
21.刷新权限
flush privileges;
22.一系列操作设置远程连接数据库
show databases;
use mysql;
show tables;
select Host,User from user;
update user set user.Host=’%’ where user.User=‘root’;
flush privileges;
ctrl + d
23.navicat远程连接测试
搭建主主
1.第一台主机 /etc/my.cnf
server-id=1
log-bin=master-bin
auto_increment_offset = 1
auto_increment_increment = 2
log-slave-updates=true #将复制事件写入binlog,一台服务器既做主库又做从库此选项必须要开启
2.登陆mysql
mysql -u root -p
运行语句
create user ‘rep1’@‘192.168.%.%’ identified by ‘123456’;
grant replication slave on . to ‘rep1’@‘192.168.%.%’;
flush privileges;
show master status;
3.第二台主机 /etc/my.cnf
server-id=2
log-bin=master-bin
auto_increment_offset = 2
auto_increment_increment = 2
log-slave-updates=true #将复制事件写入binlog,一台服务器既做主库又做从库此选项必须要开启
4.登陆mysql
运行
change master to master_host=‘192.168…第一台主机ip’,master_port=3306,master_user=‘rep1’,master_password=‘123456’,master_log_file=‘master-bin.000001’,master_log_pos=763;
start slave;
show slave status \G;
5.反向主从再来一次