啟動數據庫 傳統啟動方式 /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