mysql二進制版本安裝以及主從服務的搭建

簡單的筆記:
 去mysql官網下載5.5的二進制版本,wget -c +url下載tar.gz,我這裏下載的是64位5.5版本的包,先簡單的說下安裝,其實這些在mysql文檔上都有,只不過每個人安裝的時候可能遇到不同的問題,看下.err文件,會有問題提示。爲了方便搭建主從,我在同一臺機器上,安裝兩個mysql。假設這裏的路徑分別爲:/usr/local/mysql01 ,而slave的庫路徑爲/usr/slave,安裝如下,將已經下載的tar.gz包cp到上述目錄下。
以mysql01爲例,
  1.tar zxvf 解壓縮
    
  2.建立軟連接 
      執行ln -s *.tar.gz mysql在當前文件夾建立軟連接(不需要完全按照mysql文檔上的路徑來,甚至運行時文件路徑等,都可以指定,不使用默認)

如圖,淺色的即爲mysql軟連接
3.添加msyql用戶用戶組,指令如下:
   shell> groupadd mysql
   shell> useradd -r -g mysql -s /bin/false mysql
  如果提示已存在 說明裝過mysql,已經添過,可以繼續進行後面的步驟
4.之後cd到我們的mysqll目錄,進行權限的修改
    chown -R mysql .           //修改所屬用戶爲mysql
    chgrp -R mysql .             //修改所屬用戶組爲mysql 
5 調用./bin/scripts/mysql_install_db --user=mysql 進行初始化
6.再次進行權限的修改
   shell> chown -R root .
   shell> chown -R mysql data 
7.拷貝配置文件(如果需要,copy數據庫data文件)
  
   如圖,在support-files文件夾下有很多文件,其中my-small.cnf是用於小型庫,mysql-large.cnf是大型庫,文檔上拷貝的是my-medium.cnf,執行以下命令京配置文件拷貝當安裝的當前目錄
cp ./support-files/my-medium.cnf my.cnf
當需要修改配置文件的時候,可與su 到root權限,或者直接chmod 777 my.cnf修改文件權限,但是之後需要修改爲644 不然mysql不會啓動。
  注:當mysql安裝的時候,data的數據庫文件是在/var/lib/mysql/下,如果一臺機器上安裝多個實例,建議mv 此文件夾到安裝目錄(rm掉以前的data文件夾)
8.啓動
./bin/mysqld_safe --user=mysql &
下面是一些自定義的選項:
[client]
#password       = your_password
port            = 3306
socket          = /tmp/mysql.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port            = 3306
socket          = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
對於port,一臺機器上有多個實例的時候,需要修改爲不同的端口號,還有scok文件,也需要區別。
 
basedir = /usr/local/mysql01/mysql
datadir = /usr/local/mysql01/mysql/data
# Uncomment the following if you are using InnoDB tables
innodb_data_home_dir = /usr/local/mysql01/mysql/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /usr/local/mysql01/mysql/data
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
innodb_buffer_pool_size = 16M
innodb_additional_mem_pool_size = 2M
# Set .._log_file_size to 25 % of buffer pool size
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1  //redo log配置項 0表示事務提交時不寫文件  1表示事務提交時寫文件且fsync到磁盤  2表示寫但不調用fsync落盤 
innodb_lock_wait_timeout = 50

重點配置basedir指定我們的安裝目錄,datadir指定數據庫文件路徑,如果需要,可以指定下sock和pid文件,還有err日誌。在一臺機器上陪多個節點,一定要把sock修改一下,不然每個節點使用的都是/tmp/mysql.sock文件,可以簡單的重命名爲mysql01.sock和mysql02.sock,當連接的時候i,加上-S /tmp/mysql02.sock即可,不然會提示找不到/tmp/mysql.sock錯誤,需要注意的還有端口號,master默認爲3306,slave在一臺機器上一定要修改一下。
之後./bin/mysqld_safe --defaults-file=./my.cnf --user=mysql &
按照相同的步驟安裝slave節點。

集羣的配置:
[mysqld]
log-bin=mysql-bin //開啓bin日誌,slave可關閉
server-id=1 //server-id,master和slave必須不同
之後啓動master,正在master上註冊slave用戶和password,命令如下:
mysql> CREATE USER 'repl'@'%.mydomain.com' IDENTIFIED BY 'slavepass';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.mydomain.com';
其中域名改爲localhost(我在一臺機器上配置的),設置好name和password,同時將slave的server-id改爲2,關閉slave的二進制日誌。這裏先貼一點my.cnf上的註釋

# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id       = 2

# Replication Slave (comment out master section to use this)
#
# To configure this host as a replication slave, you can choose between
# two methods :
#
# 1) Use the CHANGE MASTER TO command (fully described in our manual) -
#    the syntax is:
#
#    CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,
#    MASTER_USER=<user>, MASTER_PASSWORD=<password> ;
#
#    where you replace <host>, <user>, <password> by quoted strings and
#    <port> by the master's port number (3306 by default).
#
#    Example:
#
#    CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
#    MASTER_USER='joe', MASTER_PASSWORD='secret';
#
# OR
#
# 2) Set the variables below. However, in case you choose this method, then
#    start replication for the first time (even unsuccessfully, for example
#    if you mistyped the password in master-password and the slave fails to
#    connect), the slave will create a master.info file, and any later
#    change in this file to the variables' values below will be ignored and
#    overridden by the content of the master.info file, unless you shutdown
#    the slave server, delete master.info and restart the slaver server.
#    For that reason, you may want to leave the lines below untouched
#    (commented) and instead use CHANGE MASTER TO (see above)
#
# required unique id between 2 and 2^32 - 1
# (and different from the master)
# defaults to 2 if master-host is set
# but will not function as a slave if omitted
#server-id       = 2
#
# The replication master for this slave - required
#master-host     =   localhost
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user     =   slave01
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password =   123456
#
# The port the master is listening on.
# optional - defaults to 3306
#master-port     = 3306
#


從這裏我們知道有兩種配置方式,可以在slave的mysql>下直接唱歌 master to ,也可以配置在配置文件中,這裏使用第一種
mysql> CHANGE MASTER TO
    ->     MASTER_HOST='master_host_name',
    ->     MASTER_USER='replication_user_name',
    ->     MASTER_PASSWORD='replication_password',
    ->     MASTER_LOG_FILE='recorded_log_file_name',
    ->     MASTER_LOG_POS=recorded_log_position;
然後 start slave即可。
這裏我在master和slave配置replication之前,是保證了兩個庫的數據一致的,如果不一樣,還要進行一些lock和轉儲快照等的操作,mysql文檔上有說。開了兩個窗口測試了一下,master的更改slave都會體現,so nice。
我只是做了個簡單的步驟記錄。沒貼太多過程圖,其實是配好了,才寫的記錄。












 

發佈了44 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章