启动:
service mysqld start
查看启动状态:
service mysqld status
查询临时密码:
grep 'temporary password' /var/log/mysqld.log
登录:
mysql -uroot -p
修改密码:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'HanYe@123456';
创建用户:
create user 'hanye'@'%' identified with mysql_native_password by 'HanYe@123456';
授权:
grant all on *.* to 'hanye'@'%';
刷新配置:
flush privileges;
Mysql 命令行连接mycat
mysql -uroot -proot -h192.168.0.40 -P8066 --default-auth=mysql_native_password
主从配置
修改 vim etc/my.cnf 文件
主库 server-id=1 bin-log=hanye_mysql
从库 server-id=2
主库创建从库用户
create user 'repl'@'%' identified by 'HanYe@123456';
create user 'repl'@'%' identified with mysql_native_password by 'HanYe@123456';
ALTER user 'repl'@'%' identified with mysql_native_password by 'HanYe@123456';
授权
grant replication slave on *.* to 'repl'@'%';
刷新权限
FLUSH PRIVILEGES;
锁表
flush tables with read lock;
查看master状态
show master status;
记录File 名称和Position位置 这里分别是 hanye_mysql.000001 857
将位置之前的数据dump
mysqldump --all-databases --master-data >dumpdb.db -uroot -p
将文件dumpdb.db 复制到从库服务器
scp [email protected]:~/dumpdb.db .
将dumpdb.db同步到从库
mysql < dumpdb.db -uroot -p
主库放开锁表语句
unlock tables;
进入从库
配置主从关系
change master to master_host='192.168.0.41',master_user='repl',master_password='HanYe@123456',master_log_file='hanye_mysql.000001',master_log_pos=857;
执行start slave;
主库误删可执行如下
mysql>stop slave;
mysql>SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1
mysql>start slave
重置从库
reset slave;
查看从库状态 (配置失败,查看状态中会有错误描述)
show slave status;
主主模式,反过来配置即可,将原来的主库作为从库,从库作为主库
这里使用的是mysql8.0,具体安装可参照官网
https://dev.mysql.com/doc/refman/8.0/en/installing.html
注意:
主从搭建前提条件,准备两台mysql服务器或者虚拟机这里分别是:
主数据库 :192.168.0.41
从数据库 :192.168.0.40