安裝mysql 配置主從(詳解)

由於最近工作需要,配置個主從

1、查看系統有沒有mysql數據庫

rpm -aq | grep -i mysql   
 rpm -e MySQL-server-5.6.27-1.el6.x86_64   #刪除
yum remove mysql mysql-server mysql-libs compat-mysql51  #yum刪除mysql  
whereis mysql  #查看mysql的位置
 rm -rf /usr/lib64/mysql   #刪除
2、安裝mysql

 wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm  #下載rpm包
# rpm -ivh mysql-community-release-el7-5.noarch.rpm  
# yum install mysql-community-server  #yum安裝
3、安裝mysql成功之後重啓mysql服務

systemctl start mysqld.service
4、編輯mysql密碼

mysql -u root
use mysql; 
update user set password=password('123') where user='root'
flush privileges;
5、安裝成功之後,有幾個重要的目錄
#數據庫目錄
/var/lib/mysql/
#配置文件
/usr/share/mysql(mysql.server命令及配置文件)
#相關命令
/usr/bin(mysqladmin mysqldump等命令)
#啓動腳本
/etc/rc.d/init.d/(啓動腳本文件mysql的目錄)

6、哎呦剛纔密碼忘了怎麼辦

(1)登錄到數據庫所在服務器,手工kill吊mysql進程:

kill `cat /mysql-data-directory/hostname.pid` 

其中,/mysql-data-directory/hostname.pid指的是 MySQL 數據目錄下的.pid 文件,它記錄了

MySQL 服務的進程號。

(2)使用--skip-grant-tables選項重啓 MySQL 服務:

[root@localhost mysql]#  ./bin/mysqld_safe --skip-grant-tables --user=zzx & [1] 20881 
[root@localhost mysql]# Starting mysqld daemon with databases from /home/zzx/mysql/data 

其中--skip-grant-tables 選項前面曾經介紹過,意思是啓動 MySQL 服務的時候跳過權限表認證。

啓動後,連接到 MySQLroot將不需要口令。

(3)用空密碼的 root 用戶連接到 MySQL,並且更改 root 口令:

[root@localhost mysql]# mysql -uroot 
Welcome to the MySQL monitor.  Commands end with ; or \g. 
Your MySQL connection id is 53 
Server version: 5.0.41-community-log MySQL Community Edition (GPL)  
Type 'help;' or '\h' for help. Type '\c' to clear the buffer. 
 mysql> 
mysql> set password = password('123'); 
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement 
mysql> update user set password=password('123') where user='root' and host='localhost'; 
Query OK, 1 row affected (0.00 sec) 
Rows matched: 1  Changed: 1  Warnings: 0 

此時,由於使用了--skip-grant-tables選項啓動,使用“setpassword”命令更改密碼失敗,直接更新 user 表的 password 字段後更改密碼成功。

(4)刷新權限表,使得權限認證重新生效:

mysql> flush privileges; 
Query OK, 0 rows affected (0.00 sec) 
(5)重新用 root 登錄時,必須輸入新口令

注意:在 MySQL中,密碼丟失後無法找回,只能通過上述方式修改密碼。



7、接來下,就開始安裝主從

主機A:192.168.1.1(master)

主機B:192.168.1.2(slave)
首先,確定兩臺機子之間可以ping通,

Master的配置

編輯mysql配置文件 :

vi /etc/my.cnf

log-bin=mysql-bin
server-id=2  #用於標識唯一的數據庫 確保唯一
binlog-ignore-db=information_schema  #表示同步的時候ignore的數據庫
binlog-ignore-db=cluster
binlog-ignore-db=mysql
binlog-do-db=ufind_db  #指定需要同步的數據庫
重啓mysql,進去mysql,賦予從庫權限賬號,允許用戶在出庫上讀取日誌,賦予slave機器有file、replication slave權限才行

在master數據褲上

>GRANT FILE ON *.* TO 'root'@'192.168.1.2' IDENTIFIED BY 'mysql password';
 >GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.1.2' IDENTIFIED BY 'mysql password';
>FLUSH PRIVILEGES
重啓mysql 登錄mysql 顯示主庫信息:

mysql> show master status;
+------------------+----------+--------------+----------------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                 | Executed_Gtid_Set |
+------------------+----------+--------------+----------------------------------+-------------------+
| mysql-bin.000004 |   120    | ufind_db     | information_schema,cluster,mysql |                  |
+------------------+----------+--------------+----------------------------------+-------------------+
1 row in set (0.00 sec)
這裏的file 和possition在從庫上用到的

slave的配置

1.從庫的配置,先修改配置文件:

log-bin=mysql-bin
server-id=3
binlog-ignore-db=information_schema
binlog-ignore-db=cluster
binlog-ignore-db=mysql
replicate-do-db=ufind_db
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60
2、重啓mysql,進入mysql執行以下操作
mysql> stop slave;  #關閉Slave
mysql> change master to master_host='192.168.1.1'
,master_user='root',
master_password='123456',
master_log_file='mysql-bin.000004', master_log_pos=28125;

mysql> start slave;  #開啓Slave
在這裏指定Master的信息,master_log_file是在配置Master的時候的File選項,master_log_pos是在配置Master的Position

選項,這裏要進行對應

然後通過show slave status;查看配置信息

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.167.1.1
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 28125
               Relay_Log_File: VM_128_194_centos-relay-bin.000004
                Relay_Log_Pos: 26111
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: ufind_db
          Replicate_Ignore_DB: mysql
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 28125
              Relay_Log_Space: 26296
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 2
                  Master_UUID: 8ac3066a-9680-11e5-a2ec-5254007529fd
             Master_Info_File: /data/mysqldb/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> 

可以看到,已經配置成功。

在顯示的這些信息中,我們主要關心“Slave_IO_Running”和“Slave_SQL_Running”這兩個進程狀態是否是“yes”,

這兩個進程的含義分別如下。 l  Slave_IO_Running:此進程負責從服務器(Slave)從主服務器(Master)上讀取 BINLOG

日誌,並寫入從服務器上的中繼日誌中。

l  Slave_SQL_Running:此進程負責讀取並且執行中繼日誌中的 BINLOG日誌。

只要其中有一個進程的狀態是no,則表示複製進程停止,錯誤原因可以從“Last_Errno”字段的值中看到。

除了查看上面的信息,用戶還可以通過這個命令瞭解從服務器的配置情況以及當前和主服務器的同步情況,包括指向那個主服

務器,主服務器的端口,複製使用的用戶,當前日誌恢復到的位置等,這些信息都是記錄在從服務器這一端的,主服務器上並

沒有相應的信息。


添加需要同步的從庫Slave

由於種種原因,測試的時候使用test庫,這裏我按照上述的方式,修改Master的my.cnf的配置文件,新增同步的數據庫test,重

啓MySQL,執行Master的:show master status如下:

這裏寫圖片描述

相應的,要修改Slave從庫的信息在my.cnf 增加 replicate-do-db=test,重啓Mysql,根據上述的show master status,在Slave

從庫中執行下邊的內容:

>stop slave
>change master to master_host='192.168.1.1',master_user='root',master_password='123456',master_log_file='mysql-bin.000005', master_log_pos=120;
>start slave
繼續使用 show slave status;

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.1
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 1422
               Relay_Log_File: VM_128_194_centos-relay-bin.000004
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000005
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: ufind_db,test
          Replicate_Ignore_DB: mysql
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1422
              Relay_Log_Space: 468
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 2
                  Master_UUID: 8ac3066a-9680-11e5-a2ec-5254007529fd
             Master_Info_File: /data/mysqldb/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> 

是否啓用bin-log日誌:

show variables like 'log_bin';

查看所有日誌日誌:

show binary logs;

查看某個日誌文件裏面的操作:

show binlog events in 'mysql-bin.000001'


至此,配置就完成了




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章