1. 程式人生 > >mysql多實例-主從復制安裝

mysql多實例-主從復制安裝

put schema pat client distrib clear tar binlog centos

安裝環境:Centos6.5

mysql版本:mysql-5.5.32.tar.gz

一:安裝前準備:

1.安裝一些依賴庫

yum install cmake gcc gcc-c++ ncurses-devel bison perl zilb-devel libio-devel perl-devel -y

2.創建mysql用戶與用戶組

[root@oracle ~]# groupadd mysql
[root@oracle ~]# useradd -g mysql mysql
[root@oracle ~]# id mysql
uid=501(mysql) gid=501(mysql) 組=501
(mysql)

3.創建mysql數據存放目錄,設定安裝兩個實例3306,3307

[root@oracle ~]# mkdir -p /data/{3306,3307}/data
[root@oracle ~]# tree /data
/data
├── 3306
│   └── data
└── 3307
    └── data

4.編譯安裝

tar zxvf mysql-5.5.32.tar.gz 
cd mysql
-5.5.32 cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DDEFAULT-CHARSET=utf8 -DMYSQL_USER=mysql make make install

5.分別復制my.cnf配置文件和mysql啟動腳本到/data/3306和3307下

[root@oracle support-files]# cp my-huge.cnf /data/3306/my.cnf
[root@oracle support-files]# cp my-huge.cnf /data/3307/my.cnf
[root@oracle support-files]# cp mysql.server /data/3306/mysql
[root@oracle support-files]# cp mysql.server /data/3307/mysql

6.修改/data目錄屬主

[root@oracle ~]# chown -R mysql:mysql /data

7.修改3306,3307實例的配置文件

以3306實例為例,3307修改相應位置內容
# The following options will be passed to all MySQL clients [client] #password
= your_password port = 3306 socket = /data/3306/mysql.sock # The MySQL server [mysqld] port = 3306 socket = /data/3306/mysql.sock skip-external-locking key_buffer_size = 384M max_allowed_packet = 1M table_open_cache = 512 sort_buffer_size = 2M read_buffer_size = 2M read_rnd_buffer_size = 8M myisam_sort_buffer_size = 64M thread_cache_size = 8 query_cache_size = 32M # Try number of CPUs*2 for thread_concurrency thread_concurrency = 8 datadir=/data/3306/data
.......略過一段..........

log-bin=mysql-bin #開啟二進制日誌 ,從服務器不需要開啟二進制日誌,3307實例註釋掉這行

server-id = 1 #實例的server-id不能相同,可以設置3307的server-id = 2

my.cnf文件末尾添加 [mysqld_safe]段,定義每個實例的錯誤日誌文件,pid進程位置等

[mysqld_safe]

log-error=/data/3306/3306.err
pid-file=/data/3306/mysqld.pid

8,修改3306,3307實例的啟動腳本文件內容

1.找到basedir=和datadir=兩行,在後面加上mysql的安裝目錄路徑和數據存放路徑
basedir=/usr/local/mysql datadir=/data/3306/data

2.定位到283行:

$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>&1 &

改為:

$bindir/mysqld_safe --datadir="$datadir/my.cnf" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>&1 &

3307實例也修改對應位置

9.給3306,3307的啟動腳本執行權限

[root@oracle 3306]# chmod 755 mysql 
[root@oracle 3307]# chmod 755 mysql 

10.初始化數據庫

[root@oracle ~]# cd /usr/local/mysql/scripts/
[root@oracle scripts]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/3306/data
[root@oracle scripts]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/3307/data
到數據存放目錄查看一下

[root@oracle ~]# cd /data/3306/data/ #同樣查看3307
[root@oracle data]# ls
mysql performance_schema test
[root@oracle data]#

11.啟動mysql

啟動3306實例

[root@oracle ~]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/3306/my.cnf 2>&1 > /dev/null &
[1] 6083

查看一下
[root@oracle ~]# netstat -tunlp | grep mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 6372/mysqld
啟動3307實例

[root@oracle ~]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/3307/my.cnf 2>&1 > /dev/null &
[2] 6393
[root@oracle ~]# netstat -tnlp | grep mysqld
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 6670/mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 6372/mysqld
啟動成功。

再看一下/data目錄下

技術分享

12.給數據庫設置密碼提示錯誤 -bash: mysqladmin: command not found

[root@oracle 3306]# mysqladmin
-bash: mysqladmin: command not found
[root@oracle 3306]# 

執行指令系統默認是到 /usr/sbin 下尋找調用指令的,將/usr/local/mysql/bin/ 下的幾個常用管理命令創建軟連接到/usr/sbin下,以後使用mysql指令就不需要用絕對路徑了

[root@oracle 3306]# ln -s /usr/local/mysql/bin/mysqladmin /usr/sbin/
[root@oracle 3306]# ln -s /usr/local/mysql/bin/mysqld_safe /usr/sbin/
[root@oracle 3306]# ln -s /usr/local/mysql/bin/mysql /usr/sbin/

設置密碼

[root@oracle 3306]# mysqladmin -u root password root123 -s /data/3306/mysql.scok
[root@oracle 3306]# mysqladmin -u root password root123 -s /data/3307/mysql.scok

登錄數據庫出現錯誤,登錄被拒絕。

[root@oracle 3306]# mysql -uroot -p -S /data/3306/mysql.sock
Enter password: 
ERROR 1045 (28000): Access denied for user root@localhost (using password: YES)
登錄被拒絕...這個讓我郁悶了一會...
先跳過權限表登錄,再修改密碼

mysqld_safe --defaults-file=/data/3306/my.cnf --skip-grant-table &

再登錄

mysql -uroot -S /data/3306/mysql.sock

[root@oracle 3306]# mysql -uroot -S /data/3306/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.32-log Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> select user,password,host from mysql.user;
+------+----------+-----------+
| user | password | host |
+------+----------+-----------+
| root | | localhost |
| root | | oracle |
| root | | 127.0.0.1 |
| root | | ::1 |
| | | localhost |
| | | oracle |
+------+----------+-----------+
6 rows in set (0.03 sec)

(郁悶:前面設置的密碼這裏查詢怎麽是為空呢。。。。)

先刪除user名稱為空的用戶

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> delete from user where user=‘‘;
Query OK, 2 rows affected (0.03 sec)

修改密碼

mysql> update user set password=PASSWORD(‘root123456‘) where user = ‘root‘;
Query OK, 4 rows affected (0.04 sec)
Rows matched: 4 Changed: 4 Warnings: 0

退出後重啟一下服務,登錄成功(幾乎每次剛安裝好後登錄都會遇到這個問題。。。)

這裏總結一下多實例的啟動,關閉,登錄的指令:
啟動指令:mysqld_safe --defaults-file=/data/3306/my.cnf 2>&1 > /dev/null &
關閉指令:mysqladmin -S /data/3306/mysql.sock shutdown (有密碼要指定-u , -p選項)
登錄指令:mysql -uroot -p -S /data/3306/mysql.sock
指定實例登錄跳過權限表:
跳過權限:mysqld_safe --defaults-file=/data/3306/my.cnf --skip-grant-tables
跳過權限登錄:mysql -uroot -S /data/3306/mysql.sock

//////////////////////////////////////***************************//////////////////////////////////////////////////////////////////////////

同理,3307實例也遇到了登錄問題,使用同樣的方式解決即可。下面開始進行主從復制配置。

主庫端的配置:

1.主庫中添加一個用於主從之間通信的用戶並授權

mysql> grant file on *.* to backup@192.168.%.% identified by root123456;

mysql> grant replication slave on *.* to ‘backup‘@‘192.168.%.%‘ identified by ‘root123456‘;

2.檢查一下主庫二進制是否開啟了

mysql> show variables like log_bin;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin       | ON    |
+---------------+-------+
1 row in set (0.01 sec)
再查看當前二進制記錄信息

mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 410 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

從庫端配置

在從庫執行master change to 配置主從庫連接參數(忘記指令可以在mysql中執行help master change to查看)
mysql> use mysql;

mysql> change master to \

-> master_host=‘192.168.214.133‘\
-> master_user=‘backup‘\
-> master_password=‘root123456‘\
-> master_log_file=‘mysql-bin.000004‘\
-> master_log_pos=410;

啟動從庫slave

mysql> start slave;
Query OK, 0 rows affected (0.03 sec)

查看slave是不是啟動正常了

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.214.133
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 410
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
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: 410
Relay_Log_Space: 410
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: 1
1 row in set (0.00 sec)

接下來,在主庫中創建一個數據庫,測試主從復制功能。

mysql> show tables;
+----------------+
| Tables_in_demo |
+----------------+
| test           |
+----------------+
1 row in set (0.00 sec)

mysql> desc test;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | varchar(20) | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into test values (344,testname);
Query OK, 1 row affected (0.36 sec)

mysql> select * from test;
+------+----------+
| id   | name     |
+------+----------+
| 344  | testname |
+------+----------+
1 row in set (0.44 sec)

mysql> 

再看從庫同步的信息

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| demo               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> select * from demo.test;
+------+----------+
| id   | name     |
+------+----------+
| 344  | testname |
+------+----------+
1 row in set (0.01 sec)

可以看到主從庫信息同步也正常。

mysql多實例-主從復制安裝