安装mysql
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-server
#mysql初始化
mysqld initialize
#启动mysql
service mysqld start
查看数据库日志:less /var/log/mysql/mysqld.log 是否有临时密码
--修改密码:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Admin~123' PASSWORD EXPIRE NEVER;
--新增用户 :
CREATE DATABASE root character set utf8;
use root;
CREATE USER 'root'@'%' IDENTIFIED BY '123456';
GRANT ALL ON *.* TO 'root'@'%';
flush privileges;
--查看数据库:show databases;
--切换数据库:use databaseName;
--查看数据库下面的用户:select user();
查看master状态:
show master status;
主从数据库设置
master数据库为:192.168.1.35
slave数据库为:192.168.1.36
master数据库设置:
[mysqld]
#主数据库端ID号
server-id=1
#开启二进制日志
log-bin=master-bin
log-bin-index=master-bin.index
--192.168.1.35客户端执行
create user 'root'@'192.168.1.36' identified by 'Admin~123';
grant all on *.* to 'root'@'192.168.1.36';
grant replication slave on *.* to 'root'@'192.168.1.36';
flush privileges;
#重启mysql
service mysqld restart
slave数据库配置:
[mysqld]
server-id=2
relay-log-index=slave-bin.index
relay-log=slave-bin
--- 192.168.1.36客户端执行
---配置关联master,3306可以默认不写,master_log_file参考之前记录的信息
change master to master_host='192.168.1.35',master_port=3306,master_user='root',master_log_file='master-bin.000002',master_log_pos=353;
change master to master_password='Admin~123';
--重启从服务器
--开启主从同步
start slave;
--查看从数据库状态
show slave status;
其中Slave_io_state : wait for master to send event
slave_io_running :yes
slave_sql_running : yes
测试:
主数据库:192.168.1.35 新建表
从数据库:192.168.1.36查看表: