mysql安裝配置、主從複製配置詳解

做者:NiceCui


mysql安裝、配置

1. yum 下載mysql

僅限 centos7如下 版本html

#yum install mysql
#yum install mysql-server
#yum install mysql-devel

啓動服務mysql

[root@localhost hadoop]# service mysqld restart

centos 7 mysql-server失敗ios

[root@yl-web yl]# yum install mysql-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.sina.cn
 * extras: mirrors.sina.cn
 * updates: mirrors.sina.cn
No package mysql-server available.
Error: Nothing to do

查資料發現是CentOS 7 版本將MySQL數據庫軟件從默認的程序列表中移除,用mariadb代替了。web

MariaDB數據庫管理系統是MySQL的一個分支,主要由開源社區在維護,採用GPL受權許可。開發這個分支的緣由之一是:甲骨文公司收購了MySQL後,有將MySQL閉源的潛在風險,所以社區採用分支的方式來避開這個風險。MariaDB的目的是徹底兼容MySQL,包括API和命令行,使之能輕鬆成爲MySQL的代替品。sql

解決:安裝mariadbshell

[root@yl-web yl]# yum install mariadb-server mariadb 

mariadb數據庫的相關命令是:

systemctl start mariadb  #啓動MariaDB

systemctl stop mariadb  #中止MariaDB

systemctl restart mariadb  #重啓MariaDB

systemctl enable mariadb  #設置開機啓動

因此先啓動數據庫

[root@yl-web yl]# systemctl start mariadb

鏈接mysql數據庫

[root@localhost hadoop]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 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>

2. 配置mysql

設置密碼centos

mysql> set password for 'root'@'localhost' =password('password');
Query OK, 0 rows affected (0.00 sec)

mysql> 

//不須要重啓數據庫便可生效。

設置編碼安全

mysql配置文件爲/etc/my.cnf

最後加上編碼配置

[mysql]
default-character-set =utf8

//這裏的字符編碼必須和/usr/share/mysql/charsets/Index.xml中一致。

遠程鏈接設置服務器

把在全部數據庫的全部表的全部權限賦值給位於全部IP地址的root用戶。

mysql> grant all privileges on *.* to root@'%'identified by 'password';
若是是新用戶而不是root,則要先新建用戶

mysql>create user 'username'@'%' identified by 'password';  
此時就能夠進行遠程鏈接了。

配置mysql 端口號

[root@test etc]# vi my.cnf  
[mysqld]  
port=3306  // 加上設置的端口號
datadir=/var/lib/mysql  
socket=/var/lib/mysql/mysql.sock  
user=mysql  
# Disabling symbolic-links is recommended to prevent assorted security risks  
symbolic-links=0  
  
[mysqld_safe]  
log-error=/var/log/mysqld.log  
pid-file=/var/run/mysqld/mysqld.pid  
  
"my.cnf" 11L, 261C written  
[root@test etc]#  
  
4. 從新啓動mysql  

[root@localhost /]# service mysqld restart

查看端口:

mysql> show global variables like 'port';

+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.00 sec)

mysql>

遠程鏈接可能還會存在防火牆阻斷遠程鏈接失敗的狀況

加入對應mysql端口的 容許
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

結果:
[root@localhost /]# vi  /etc/sysconfig/iptables  // 打開防火牆配置

# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
COMMIT

重啓防火牆

[root@localhost /]# service  iptables restart

若是鏈接失敗 或者 telnet ip port 失敗 則關閉防火牆

service  iptables stop

// 服務器重啓將會失效

主從複製配置

一、主從服務器分別做如下操做:

  • 1.一、版本一致
  • 1.二、初始化表,並在後臺啓動mysql
  • 1.三、修改root的密碼

二、修改主服務器master

#vi /etc/my.cnf
    [mysqld]
    log-bin=mysql-bin   //[必須]啓用二進制日誌
    server-id=222      //[必須]服務器惟一ID,默認是1,通常取IP最後一段

三、修改從服務器slave:

#vi /etc/my.cnf
    [mysqld]
    log-bin=mysql-bin   //[不是必須]啓用二進制日誌
    server-id=226      //[必須]服務器惟一ID,默認是1,通常取IP最後一段

四、重啓兩臺服務器的mysql

/etc/init.d/mysql restart

五、在主服務器上創建賬戶並受權slave:

#/usr/local/mysql/bin/mysql -uroot -pmttang  
  mysql>GRANT REPLICATION SLAVE ON *.* to 'mysync'@'%' identified by 'q123456';
 //通常不用root賬號,“%”表示全部客戶端均可能連,只要賬號,密碼正確,此處可用具體客戶端IP代替,如192.168.1  45.226,增強安全。

六、登陸主服務器的mysql,查詢master的狀態

mysql>show master status;
   +------------------+----------+--------------+------------------+
   | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
   +------------------+----------+--------------+------------------+
   | mysql-bin.000004 |      308 |              |                  |
   +------------------+----------+--------------+------------------+
   1 row in set (0.00 sec)
   注:執行完此步驟後不要再操做主服務器MYSQL,防止主服務器狀態值變化

七、從服務器設置普通用戶只讀模式

mysql>create user 'test'@'%' identified by '123456';  // 建立普通用戶,能夠遠程鏈接

mysql> grant select  on *.* to test@'%'identified by '123456'; //受權全部庫,只能查詢操做

mysql> grant all privileges on *.* to test@'%'identified by '123456';  //這是授予全部權限
  • 關於mysql建立用戶以及權限,下面有詳細說明;這裏只是作了主從複製避免從庫添加數據作準備;
  • 從庫遠程登陸就用剛纔設置的普通的只要查詢權限的帳號去登陸,避免致使主從出錯

八、配置從服務器Slave:

mysql>change master to master_host='192.168.145.222',master_user='mysync',master_password='q123456',
    master_log_file='mysql-bin.000004',master_log_pos=308;   //注意不要斷開,308數字先後無單引號。

    Mysql>start slave;    //啓動從服務器複製功能
    
    change master to master_host='192.168.80.131',master_user='mysync',master_password='q123456',master_log_file='mysql-bin.000001',master_log_pos=251;

九、檢查從服務器複製功能狀態:

mysql> show slave status\G
*************************** 1. row ***************************

    Slave_IO_State: Waiting for master to send event
    Master_Host: 192.168.2.222  //主服務器地址
    Master_User: mysync   //受權賬戶名,儘可能避免使用root
    Master_Port: 3306    //數據庫端口,部分版本沒有此行
    Connect_Retry: 60
    Master_Log_File: mysql-bin.000004
    Read_Master_Log_Pos: 600     //#同步讀取二進制日誌的位置,大於等於Exec_Master_Log_Pos
    Relay_Log_File: ddte-relay-bin.000003
    Relay_Log_Pos: 251
    Relay_Master_Log_File: mysql-bin.000004
    Slave_IO_Running: Yes    //此狀態必須YES
    Slave_SQL_Running: Yes     //此狀態必須YES
    ......

注:Slave_IO及Slave_SQL進程必須正常運行,即YES狀態,不然都是錯誤的狀態(如:其中一個NO均屬錯誤)。

以上操做過程,主從服務器配置完成。

十、MySQL添加用戶、刪除用戶與受權詳細說明;

  • 1.新建用戶
mysql> insert into mysql.user(Host,User,Password) values("localhost","test",password("1234"));

這樣就建立了一個名爲:test 密碼爲:1234 的用戶。

注意:此處的"localhost",是指該用戶只能在本地登陸,不能在另一臺機器上遠程登陸。
若是想遠程登陸的話,
將"localhost"改成"%",表示在任何一臺電腦上均可以登陸。也能夠指定某臺機器能夠遠程登陸。
  • 2.爲用戶受權
受權格式:grant 權限 on 數據庫.* to 用戶名@登陸主機 identified by "密碼"; 

2.1 登陸MYSQL(有ROOT權限),這裏以ROOT身份登陸:

@>mysql -u root -p

@>密碼
2.2 首先爲用戶建立一個數據庫(testDB):

mysql>create database testDB;
2.3 受權test用戶擁有testDB數據庫的全部權限(某個數據庫的全部權限):

mysql>grant all privileges on testDB.* to test@localhost identified by '1234';

mysql>flush privileges;//刷新系統權限表

格式:grant 權限 on 數據庫.* to 用戶名@登陸主機 identified by "密碼"; 
2.4 若是想指定部分權限給一用戶,能夠這樣來寫:

mysql>grant select,update on testDB.* to test@localhost identified by '1234';

mysql>flush privileges; //刷新系統權限表
2.5 受權test用戶擁有全部數據庫的某些權限:   

mysql>grant select,delete,update,create,drop on *.* to test@"%" identified by "1234";

//test用戶對全部數據庫都有select,delete,update,create,drop 權限。

//@"%" 表示對全部非本地主機受權,不包括localhost。
(localhost地址設爲127.0.0.1,若是設爲真實的本地地址,不知道是否能夠,沒有驗證。)

//對localhost受權:
加上一句grant all privileges on testDB.* to test@localhost identified by '1234';便可。
  • 三、刪除用戶
@>mysql -u root -p

@>密碼

mysql>Delete FROM user Where User='test' and Host='localhost';

mysql>flush privileges;

mysql>drop database testDB; //刪除用戶的數據庫

刪除帳戶及權限:>drop user 用戶名@'%';

        >drop user 用戶名@ localhost;
  • 其餘mysql操做
5. 列出全部數據庫

  mysql>show database;


6. 切換數據庫

  mysql>use '數據庫名';


7. 列出全部表

  mysql>show tables;

 
8. 顯示數據表結構

  mysql>describe 表名;

 
9. 刪除數據庫和數據表

  mysql>drop database 數據庫名;

  mysql>drop table 數據表名;

十一、主從服務器測試

主服務器Mysql,創建數據庫,並在這個庫中建表插入一條數據:

  mysql> create database hi_db;
  Query OK, 1 row affected (0.00 sec)

  mysql> use hi_db;
  Database changed

  mysql>  create table hi_tb(id int(3),name char(10));
  Query OK, 0 rows affected (0.00 sec)
 
  mysql> insert into hi_tb values(001,'bobu');
  Query OK, 1 row affected (0.00 sec)

  mysql> show databases;
   +--------------------+
   | Database           |
   +--------------------+
   | information_schema |
   | hi_db                |
   | mysql                |
   | test                 |
   +--------------------+
   4 rows in set (0.00 sec)

從服務器Mysql查詢:

   mysql> show databases;

   +--------------------+
   | Database               |
   +--------------------+
   | information_schema |
   | hi_db                 |       //I'M here,你們看到了吧
   | mysql                 |
   | test          |

   +--------------------+
   4 rows in set (0.00 sec)

   mysql> use hi_db
   Database changed
   mysql> select * from hi_tb;           //查看主服務器上新增的具體數據
   +------+------+
   | id   | name |
   +------+------+
   |    1 | bobu |
   +------+------+
   1 row in set (0.00 sec)

十二、完成:

編寫一shell腳本,用nagios監控slave的兩個yes(Slave_IO及Slave_SQL進程),如發現只有一個或零個yes,就代表主從有問題了,發短信警報吧。
相關文章
相關標籤/搜索