1. 程式人生 > >CentOS7通過yum安裝Mysql5.7+修改預設密碼+遠端登入+主從複製+keepalive高可用

CentOS7通過yum安裝Mysql5.7+修改預設密碼+遠端登入+主從複製+keepalive高可用

CentOS7通過yum安裝Mysql5.7+修改預設密碼+遠端登入+主從複製+keepalive高可用

環境設定

mysql-mater 192.168.20.51

mysql-slave 192.168.20.52

vip:192.168.20.91

vim /etc/hosts

192.168.10.52 sqla
192.168.10.51 sqlb

 

1、配置yum源

# 下載mysql源安裝包
[[email protected] ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
# 安裝mysql源
[[email protected] ~]# yum localinstall mysql57-community-release-el7-8.noarch.rpm

檢查mysql源是否安裝成功

[[email protected] ~]# yum repolist enabled | grep "mysql.*-community.*"

2、安裝mysql

[[email protected] ~]#yum install mysql-community-server

3、啟動MySQL服務

[[email protected]

~]# systemctl start mysqld.service

[[email protected] ~]# systemctl status mysqld.service

4、開機啟動

[[email protected] ~]# systemctl enable mysqld

[[email protected] ~]# systemctl daemon-reload

[[email protected] ~]# grep 'temporary password' /var/log/mysqld.log

2018-09-10T03:43:48.298022Z 1 [Note] A temporary password is generated for

[email protected]: gVxip6#KC!2w

5、修改root預設密碼

mysql安裝完成之後,在/var/log/mysqld.log檔案中給root生成了一個預設密碼。 
檢視預設密碼

 [[email protected] ~]# grep 'temporary password' /var/log/mysqld.log

2018-09-10T03:43:48.298022Z 1 [Note] A temporary password is generated for [email protected]: gVxip6#KC!2w

其中 gVxip6#KC!2w 就是我的預設密碼

HVu8keo+x>wo

用預設密碼登入

shell> mysql -uroot -p

mysql> Enter password: gVxip6#KC!2w

修改預設密碼:

mysql> SET PASSWORD = PASSWORD('新的密碼');

# 上面的root是你的新密碼

6、允許root遠端登入

mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.20.%' IDENTIFIED BY 'MyNewPass4!' WITH GRANT OPTION;mysql>flush privileges;

第一句中”%”表示任何主機都可以遠端登入到該伺服器上訪問。如果要限制只有某臺機器可以訪問,將其換成相應的IP即可,如: 
GRANT ALL PRIVILEGES ON . TO [email protected]”192.168.20.%” IDENTIFIED BY “密碼”; 
[email protected]是登入密碼。 
第二句表示從mysql資料庫的grant表中重新載入許可權資料。因為MySQL把許可權都放在了cache中,所以在做完更改後需要重新載入。

7、修改字符集編碼 UTF-8

 

檢視當前的字元編碼

show variables like 'char%';

檢測是否都是utf-8(filesystem除外),如果不是就得改,例如這個character_set_server一般是拉丁編碼 
修改編碼

//停資料庫

systemctl stop mysqld

//進入 my.cnf 檔案,一般是在etc路徑下

vim /etc/my.cnf

//加入要修改的字符集 修改完:wq退出

在[mysqld]下追加:

character-set-server=utf8

//重啟資料庫

systemctl start mysqld

 

8、生產mysql主服務具體配置:

 

[client]

port = 3306

#socket = /tmp/mysql.sock

socket=/var/lib/mysql/mysql.sock

default-character-set = utf8mb4

 

[mysqld]

port = 3306

#socket = /tmp/mysql.sock

socket=/var/lib/mysql/mysql.sock

 

 

#basedir = /usr/local/mysql

#datadir = /var/mysql/data

#pid-file = /var/mysql/data/mysql.pid

datadir=/var/lib/mysql

pid-file=/var/run/mysqld/mysqld.pid

user = mysql

bind-address = 0.0.0.0

 

init-connect = 'SET NAMES utf8mb4'

character-set-server = utf8mb4

explicit_defaults_for_timestamp = 1

lower_case_table_names=1

skip-name-resolve

#skip-networking

back_log = 300

 

max_connections = 1000

max_connect_errors = 600000

open_files_limit = 65535

#table-definition-cache = 1024

#table_open_cache = 2048

max_allowed_packet = 16M

max_heap_table_size = 32M

tmp_table_size = 32M

 

read_buffer_size = 16M

read_rnd_buffer_size = 32M

sort_buffer_size = 8M

join_buffer_size = 8M

key_buffer_size = 8M

 

thread_cache_size = 512

 

query_cache_type = 0

query_cache_size = 0

#query_cache_limit = 2M

 

ft_min_word_len = 4

 

log_bin = mysql-bin

binlog_format = ROW

expire_logs_days = 7

server-id = 12

log-slave-updates

slave-skip-errors=all

sync_binlog=1

auto_increment_increment=2

auto_increment_offset=1

slave-parallel-type=LOGICAL_CLOCK

slave-parallel-workers=16

master_info_repository=TABLE

relay_log_info_repository=TABLE

relay_log_recovery=ON

 

 

#log_error = /var/mysql/data/mysql-error.log

log-error=/var/log/mysqld.log

slow_query_log = 1

long_query_time = 1

slow_query_log_file = /var/mysql/data/mysql-slow.log

 

performance_schema = 0

 

#lower_case_table_names = 1

 

skip-external-locking

 

default_storage_engine = InnoDB

innodb-flush-method            = O_DIRECT

innodb-log-files-in-group      = 2

innodb-log-file-size           = 128M

innodb-flush-log-at-trx-commit = 1

innodb-file-per-table          = 1

innodb-buffer-pool-size        = 18G

 

bulk_insert_buffer_size = 8M

myisam_sort_buffer_size = 8M

myisam_max_sort_file_size = 10G

myisam_repair_threads = 1

 

interactive_timeout = 28800

wait_timeout = 28800

 

[mysqldump]

quick

max_allowed_packet = 16M

 

[myisamchk]

key_buffer_size = 8M

sort_buffer_size = 8M

read_buffer = 4M

write_buffer = 4M

·

9、生產mysql從服務具體配置:

 

[client]

port = 3306

#socket = /tmp/mysql.sock

socket=/var/lib/mysql/mysql.sock

default-character-set = utf8mb4

 

[mysqld]

port = 3306

#socket = /tmp/mysql.sock

socket=/var/lib/mysql/mysql.sock

default_password_lifetime=0

#basedir = /usr/local/mysql

#datadir = /var/mysql/data

datadir=/var/lib/mysql

pid-file=/var/run/mysqld/mysqld.pid

#pid-file = /var/mysql/data/mysql.pid

user = mysql

bind-address = 0.0.0.0

 

init-connect = 'SET NAMES utf8mb4'

character-set-server = utf8mb4

explicit_defaults_for_timestamp = 1

lower_case_table_names=1

skip-name-resolve

range_optimizer_max_mem_size=0

#skip-networking

back_log = 300

 

max_connections = 1000

max_connect_errors = 600000

open_files_limit = 65535

#table_open_cache = 2048

#table-definition-cache = 1024

max_allowed_packet = 16M

max_heap_table_size = 32M

tmp_table_size = 32M

 

read_buffer_size = 16M

read_rnd_buffer_size = 32M

sort_buffer_size = 8M

join_buffer_size = 8M

key_buffer_size = 8M

 

#thread_cache_size = 30

 

query_cache_type = 0

query_cache_size = 0

#query_cache_limit = 2M

 

ft_min_word_len = 4

 

log_bin = mysql-bin

binlog_format = ROW

expire_logs_days = 7

server-id = 11

log-slave-updates

slave-skip-errors=all

sync_binlog=1

auto_increment_increment=1

auto_increment_offset=1

slave-parallel-type=LOGICAL_CLOCK

slave-parallel-workers=16

master_info_repository=TABLE

relay_log_info_repository=TABLE

relay_log_recovery=ON

 

log-error=/var/log/mysqld.log

#log_error = /var/mysql/data/mysql-error.log

slow_query_log = 1

long_query_time = 1

slow_query_log_file = /var/mysql/data/mysql-slow.log

 

performance_schema = 0

 

#lower_case_table_names = 1

 

skip-external-locking

 

default_storage_engine = InnoDB

innodb-flush-method            = O_DIRECT

innodb-log-files-in-group      = 2

innodb-log-file-size           = 128M

innodb-flush-log-at-trx-commit = 1

innodb-file-per-table          = 1

innodb-buffer-pool-size        = 6G

 

bulk_insert_buffer_size = 8M

myisam_sort_buffer_size = 8M

myisam_max_sort_file_size = 10G

myisam_repair_threads = 1

 

interactive_timeout = 28800

wait_timeout = 28800

 

[mysqldump]

quick

max_allowed_packet = 16M

 

[myisamchk]

key_buffer_size = 8M

sort_buffer_size = 8M

read_buffer = 4M

write_buffer = 4M

 

10、設定mysql主從複製

 

10.1 修改mysql配置

找到主資料庫的配置檔案my.cnf(或者my.ini),我的在/etc/mysql/my.cnf,在[mysqld]部分插入如下兩行:

[mysqld]

log-bin=mysql-bin #開啟二進位制日誌

server-id=11 #設定server-id

10.2.重啟mysql,建立用於同步的使用者賬號

開啟mysql會話shell>mysql -uroot -p

建立使用者並授權:使用者:replication密碼:使用者密碼

mysql> GRANT REPLICATION SLAVE ON *.* to 'replication'@'192.168.20.52' identified by '密碼';

mysql>flush privileges;   #重新整理許可權

10.3.檢視master狀態,記錄二進位制檔名(mysql-bin.000004)和位置(730):

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000004 |      730 |              |                  |                   |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

#查詢許可權

select * from mysql.user;

 

10.4、配置從伺服器Slave:


mysql>change master to master_host='192.168.20.51',master_user='replication ',master_password='密碼',master_log_file='mysql-bin.000004',master_log_pos=730;  

//注意不要斷開,308數字前後無單引號。

Mysql>start slave;    //啟動從伺服器複製功能

mysql> show slave status\G

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.20.51

                  Master_User: replication

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000004

          Read_Master_Log_Pos: 887

               Relay_Log_File: localhost-relay-bin.000002

                Relay_Log_Pos: 477

        Relay_Master_Log_File: mysql-bin.000004

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

10.5、主從伺服器測試:

主伺服器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)

 

11、安裝keepalived,並配置

 

11.1主從安裝

keepalived 安裝
#安裝keepavlived
yum -y install keepalived

#設定開機啟動
systemctl enable keepalived.service

#啟動服務
service keepalived start

11.2 主keepalived配置

! Configuration File for keepalived

 

global_defs {

   notification_email {

     [email protected]

     [email protected]

     [email protected]

   }

   notification_email_from [email protected]

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

 

vrrp_script check_mysql {

    script "/etc/keepalived/scripts/mysql_check.sh"

    interval 2

}

 

vrrp_sync_group VG1 {

    group {

        VI_1

    }

}

 

vrrp_instance VI_1 {

    state MASTER

    interface em1

    virtual_router_id 88

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    track_script {

        check_mysql

    }

 

    virtual_ipaddress {

        192.168.20.91 label em1

    }

 

    notify_master /etc/keepalived/scripts/master.sh

    notify_backup /etc/keepalived/scripts/backup.sh

    notify_stop /etc/keepalived/scripts/stop.sh

}

11.3 從keepalived配置

! Configuration File for keepalived

 

global_defs {

   notification_email {

     [email protected]

     [email protected]

     [email protected]

   }

   notification_email_from [email protected]

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

 

vrrp_script check_mysql {

    script "/etc/keepalived/scripts/mysql_check.sh"

    interval 2

}

 

vrrp_sync_group VG1 {

    group {

        VI_1

    }

}

 

vrrp_instance VI_1 {

    state BACKUP

    interface em1

    virtual_router_id 88

    priority 80 

    advert_int 1

#    nopreempt #不進行搶佔操作

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    track_script {

        check_mysql

    }

 

    virtual_ipaddress {

        192.168.20.91 label em1

    }

 

    notify_master /etc/keepalived/scripts/master.sh

    notify_backup /etc/keepalived/scripts/backup.sh

    notify_stop /etc/keepalived/scripts/stop.sh

}

注意:一定一定要給/etc/keepalived/scripts許可權