启动数据库 传统启动方式 /usr/local/mysql/bin/mysqld_safe --user=mysql & 制作成服务启动 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql 查看启动是否成功 netstat -tnl|grep 3306 ps -ef|grep mysql 相关命令 service mysql start 停止mysql服务 service mysql stop 重启mysql服务 service mysql restart 添加到开机启动项 chkconfig --add mysql
b、创建用户,并赋予权限: 登陆数据库时报错 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysqld.sock' (2)
ln -s /tmp/mysql.sock /tmp/mysqld.sock
登陆 mysql -uroot create user repl_user;
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY PASSWORD '******';
设置密码时会遇到报错:
ERROR 1372 (HY000): Password hash should be a 41-digit hexadecimal number
解决办法:用select password('你想输入的密码');查询出你的密码对应的字符串
select password('123456');
查出的是*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9
GRANT REPLICATION SLAVE ON *.* TO 'repl_user'@'%' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9';
验证主从是否搭建成功在从库执行 show slave status /G 在主库创建一个表 use test create table aa (name char(10)); insert into aa values('Tom'); 在从库查询 use test select * from aa; 查到刚刚插入的数据就O了
修改root密码 cd /usr/local/ mysql /bin
./mysqladmin -u root password
mysql> use mysql; mysql> desc user; mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root"; //授权远程连接 mysql> update user set Password = password('123456') where User='root'; //设置root用户密码 mysql> select Host,User,Password from user where User='root'; mysql> flush privileges; mysql> exit 二、MYSQL5.6.40主主搭建 前面的安装配置都一样,只需要配置/etc/my.cnf
mysql> grant replication slave on *.* to 'repl'@'192.168.159.148' identified by '123456'; mysql> flush privileges;
5、 主库A(192.168.159.148 ) 配置同步信息 mysql> show master status; +------------------+----------+--------------+------------------+-------------------------------------------------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------------------------------------------------------------------------+ | mysql-bin.000003 | 2552 | | | 7b0fba4f-5cd4-11e8-bada-000c29ba59e8:1-20, a1788b59-5d6a-11e8-bead-000c295d547a:3-9 | +------------------+----------+--------------+------------------+-------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
mysql> change master to master_host='192.168.159.149',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=1727;
mysql > start slave;
mysql> show slave status /G; 注意看其中的这两个状态是YES就是正常
Slave_IO_Running: Yes Slave_SQL_Running: Yes
6、 主库A(192.168.159.149 ) 配置同步信息 mysql> show master status; +------------------+----------+--------------+------------------+-------------------------------------------------------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------------------------------------------------------------------------------+ | mysql-bin.000004 | 1727 | | | 7b0fba4f-5cd4-11e8-bada-000c29ba59e8:8-10:16-20, a1788b59-5d6a-11e8-bead-000c295d547a:1-9 | +------------------+----------+--------------+------------------+-------------------------------------------------------------------------------------------+ 1 row in set (0.05 sec)
mysql> change master to master_host='192.168.159.148',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=2552;
mysql > start slave;
mysql > show slave status /G; 注意看其中的这两个状态是YES就是正常 Slave_IO_Running: Yes Slave_SQL_Running: Yes