MySQL Community Server 5.7.18 deb安裝以及實現主從複製

1.從oracle官網下載mysql安裝包。html

mysql-server_5.7.18-1ubuntu16.04_amd64.deb-bundle.tar

2.解壓縮安裝包。mysql

tar -xf mysql-server_5.7.18-1ubuntu16.04_amd64.deb-bundle.tar

3.使用root用戶執行命令安裝。redis

dpkg -i mysql-common_5.7.18-1ubuntu16.04_amd64.deb mysql-community-client_5.7.18-1ubuntu16.04_amd64.deb libmysqlclient20_5.7.18-1ubuntu16.04_amd64.deb libmysqlclient-dev_5.7.18-1ubuntu16.04_amd64.deb libmysqld-dev_5.7.18-1ubuntu16.04_amd64.deb mysql-client_5.7.18-1ubuntu16.04_amd64.deb mysql-community-server_5.7.18-1ubuntu16.04_amd64.deb

4.報錯以下。sql

root@ubuntu:~$ dpkg -i mysql-common_5.7.18-1ubuntu16.04_amd64.deb mysql-community-client_5.7.18-1ubuntu16.04_amd64.deb libmysqlclient20_5.7.18-1ubuntu16.04_amd64.deb libmysqlclient-dev_5.7.18-1ubuntu16.04_amd64.deb libmysqld-dev_5.7.18-1ubuntu16.04_amd64.deb mysql-client_5.7.18-1ubuntu16.04_amd64.deb mysql-community-server_5.7.18-1ubuntu16.04_amd64.deb
Selecting previously unselected package mysql-common.
(Reading database ... 207638 files and directories currently installed.)
Preparing to unpack mysql-common_5.7.18-1ubuntu16.04_amd64.deb ...
Unpacking mysql-common (5.7.18-1ubuntu16.04) ...
Selecting previously unselected package mysql-community-client.
Preparing to unpack mysql-community-client_5.7.18-1ubuntu16.04_amd64.deb ...
Unpacking mysql-community-client (5.7.18-1ubuntu16.04) ...
Selecting previously unselected package libmysqlclient20:amd64.
Preparing to unpack libmysqlclient20_5.7.18-1ubuntu16.04_amd64.deb ...
Unpacking libmysqlclient20:amd64 (5.7.18-1ubuntu16.04) ...
Selecting previously unselected package libmysqlclient-dev.
Preparing to unpack libmysqlclient-dev_5.7.18-1ubuntu16.04_amd64.deb ...
Unpacking libmysqlclient-dev (5.7.18-1ubuntu16.04) ...
Selecting previously unselected package libmysqld-dev.
Preparing to unpack libmysqld-dev_5.7.18-1ubuntu16.04_amd64.deb ...
Unpacking libmysqld-dev (5.7.18-1ubuntu16.04) ...
Selecting previously unselected package mysql-client.
Preparing to unpack mysql-client_5.7.18-1ubuntu16.04_amd64.deb ...
Unpacking mysql-client (5.7.18-1ubuntu16.04) ...
Selecting previously unselected package mysql-community-server.
Preparing to unpack mysql-community-server_5.7.18-1ubuntu16.04_amd64.deb ...
Unpacking mysql-community-server (5.7.18-1ubuntu16.04) ...
Setting up mysql-common (5.7.18-1ubuntu16.04) ...
update-alternatives: using /etc/mysql/my.cnf.fallback to provide /etc/mysql/my.cnf (my.cnf) in auto mode
dpkg: dependency problems prevent configuration of mysql-community-client:
 mysql-community-client depends on libaio1 (>= 0.3.93); however:
  Package libaio1 is not installed.

dpkg: error processing package mysql-community-client (--install):
 dependency problems - leaving unconfigured
Setting up libmysqlclient20:amd64 (5.7.18-1ubuntu16.04) ...
Setting up libmysqlclient-dev (5.7.18-1ubuntu16.04) ...
Setting up libmysqld-dev (5.7.18-1ubuntu16.04) ...
dpkg: dependency problems prevent configuration of mysql-client:
 mysql-client depends on mysql-community-client (= 5.7.18-1ubuntu16.04); however:
  Package mysql-community-client is not configured yet.

dpkg: error processing package mysql-client (--install):
 dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of mysql-community-server:
 mysql-community-server depends on mysql-client (= 5.7.18-1ubuntu16.04); however:
  Package mysql-client is not configured yet.
 mysql-community-server depends on libaio1 (>= 0.3.93); however:
  Package libaio1 is not installed.
 mysql-community-server depends on libmecab2 (>= 0.996-1.2ubuntu1); however:
  Package libmecab2 is not installed.

dpkg: error processing package mysql-community-server (--install):
 dependency problems - leaving unconfigured
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for libc-bin (2.23-0ubuntu7) ...
Processing triggers for systemd (229-4ubuntu10) ...
Processing triggers for ureadahead (0.100.0-19) ...
Errors were encountered while processing:
 mysql-community-client
 mysql-client
 mysql-community-server

5.執行依賴安裝,依然報錯以下。數據庫

root@ubuntu:~# apt-get install libaio1 libmecab2
Reading package lists... Done
Building dependency tree       
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
 openssh-server : Depends: openssh-client (= 1:7.2p2-4ubuntu2.2)
 openssh-sftp-server : Depends: openssh-client (= 1:7.2p2-4ubuntu2.2)
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).

6.執行命令安依賴包。ubuntu

root@ubuntu:~# apt-get install libaio1 libmecab2 openssh-client
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Suggested packages:
  ssh-askpass libpam-ssh keychain monkeysphere
The following NEW packages will be installed:
  libaio1 libmecab2
The following packages will be upgraded:
  openssh-client
1 upgraded, 2 newly installed, 0 to remove and 316 not upgraded.
3 not fully installed or removed.
Need to get 271 kB/857 kB of archives.
After this operation, 1,958 kB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 libaio1 amd64 0.3.110-2 [6,356 B]
Get:2 http://us.archive.ubuntu.com/ubuntu xenial/universe amd64 libmecab2 amd64 0.996-1.2ubuntu1 [264 kB]
Fetched 271 kB in 3s (79.4 kB/s)    
Selecting previously unselected package libaio1:amd64.
(Reading database ... 208013 files and directories currently installed.)
Preparing to unpack .../libaio1_0.3.110-2_amd64.deb ...
Unpacking libaio1:amd64 (0.3.110-2) ...
Selecting previously unselected package libmecab2.
Preparing to unpack .../libmecab2_0.996-1.2ubuntu1_amd64.deb ...
Unpacking libmecab2 (0.996-1.2ubuntu1) ...
Preparing to unpack .../openssh-client_1%3a7.2p2-4ubuntu2.2_amd64.deb ...
Unpacking openssh-client (1:7.2p2-4ubuntu2.2) over (1:7.2p2-4ubuntu2.1) ...
Processing triggers for libc-bin (2.23-0ubuntu7) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up libaio1:amd64 (0.3.110-2) ...
Setting up mysql-community-client (5.7.18-1ubuntu16.04) ...
Setting up mysql-client (5.7.18-1ubuntu16.04) ...
Setting up libmecab2 (0.996-1.2ubuntu1) ...
Setting up mysql-community-server (5.7.18-1ubuntu16.04) ...
Package configuration

7.要求輸入root用戶密碼,輸入之後mysql安裝成功。vim

update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode                                                                 
Setting up openssh-client (1:7.2p2-4ubuntu2.2) ...
Processing triggers for libc-bin (2.23-0ubuntu7) ...
Processing triggers for systemd (229-4ubuntu10) ...
Processing triggers for ureadahead (0.100.0-19) ...

8.執行mysql免密碼登錄報錯。服務器

root@ubuntu:~# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

9.進入/etc/mysql目錄,編輯mysqld.cnf文件。在文件最後一行加入配置「skip-grant-tables」,執行命令「service mysql restart」重啓mysql。oracle

root@ubuntu:/etc/mysql/mysql.conf.d# vim mysqld.cnf
# Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
log-error       = /var/log/mysql/error.log
# By default we only accept connections from localhost
bind-address    = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
skip-grant-tables

10.執行mysql命令,能夠正確登錄mysql。ssh

root@ubuntu:/etc/mysql/mysql.conf.d# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.18 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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>

11.安裝完成,登錄之後能夠執行命令修改mysql密碼。

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> update mysql.user set authentication_string=password('root') where user='root';
Query OK, 1 row affected, 1 warning (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 1
mysql> flush privileges; 
Query OK, 0 rows affected (0.08 sec)
mysql> exit

12.將/etc/mysql/mysql.conf.d/mysqld.cnf最後一行刪除,並執行命令「service mysql restart」重啓mysql。

# Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
log-error       = /var/log/mysql/error.log
# By default we only accept connections from localhost
bind-address    = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

13.輸入命令「mysql -u root -p」,輸入密碼「root」,登錄成功。

root@ubuntu:/etc/mysql/mysql.conf.d# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.18 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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>

14.至此,MySQL Community Server 5.7.18 deb安裝成功。

15.在兩臺服務器上分別安裝MySQL Community Server 5.7.18 deb版本,修改主庫配置文件
/etc/mysql/mysql.conf.d/mysqld.cnf,增長最後兩行配置。

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
log-error       = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address   = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
server-id=107
log-bin=/var/lib/mysql/mysql-bin

16.從庫也須要修改配置文件/etc/mysql/mysql.conf.d/mysqld.cnf,一樣增長最後兩行,注意server-id要不一樣。

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
log-error       = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address   = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=/var/lib/mysql/mysql-bin
server-id=109

17.重啓兩臺服務器mysql服務,service mysql restart。

18.主庫創建帳戶並授予slave權限

mysql> GRANT REPLICATION SLAVE ON *.* to 'test'@'%' identified by 'test';
Query OK, 0 rows affected, 1 warning (0.01 sec)

19.查詢主庫狀態

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

20.配置從庫,中止從庫複製功能;配置與主庫同步信息,master_host爲主庫ip地址,master_user,master_password爲18步建立的用戶名和密碼,master_log_file和master_log_pos爲19步查詢出的File和Position。 配置完成後,啓動從庫複製功能。

mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

mysql> change master to
    -> master_host='192.168.244.128',
    -> master_user='test',
    -> master_password='test',
    -> master_log_file='mysql-bin.000004',
    -> master_log_pos=1343;
Query OK, 0 rows affected, 2 warnings (0.03 sec)

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

21.檢查下從庫的配置信息,注意Slave_IO_Running: Yes和Slave_SQL_Running: Yes兩個配置項必須爲Yes,不然存在問題。須要從新執行19,20步進行配置。

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.244.128
                  Master_User: test
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 1343
               Relay_Log_File: ubuntu-relay-bin.000002
                Relay_Log_Pos: 320
        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: 1343
              Relay_Log_Space: 528
              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: 107
                  Master_UUID: 2073267a-44e9-11e7-a5ce-000c29c1f067
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           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
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

22.配置完成,進行下測試。在主庫建立database和table。

mysql> create database db_test;
Query OK, 1 row affected (0.01 sec)

mysql> use db_test;
Database changed
mysql> create table db_table(name char(13),age int(3));
Query OK, 0 rows affected (0.03 sec)

mysql> insert into db_table values('tom', 19);
Query OK, 1 row affected (0.01 sec)

23.查詢下主庫信息。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db_test            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
8 rows in set (0.00 sec)

mysql> select * from db_table;
+------+------+
| name | age  |
+------+------+
| tom  |   19 |
+------+------+
1 row in set (0.00 sec)

24.查詢下從庫信息。發現從庫中已經從主庫中同步了db_test database以及db_table表數據。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db_test            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
7 rows in set (0.00 sec)

mysql> use db_test;
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> select * from db_table;
+------+------+
| name | age  |
+------+------+
| tom  |   19 |
+------+------+
1 row in set (0.00 sec)

25.至此,Mysql完成了主從數據庫同步配置和數據同步。

26.操做過程當中遇到一個問題。在配置從庫之後,出現Slave_IO_RunningConnecting和Slave_SQL_Running: Yes狀態,查看下面錯誤信息,發現報錯沒法鏈接到主庫ip,手動鏈接了一下報錯「ERROR 2003 (HY000): Can't connect to MySQL server on 'xxx.xxx.xxx.xxx'(111)錯誤」,沒法鏈接到主庫。
解決過程:將主庫/etc/mysql/mysql.conf.d/mysqld.cnf配置文件中bind-address= 127.0.0.1註銷掉,重啓主庫便可,不要綁定本地迴環地址。

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
log-error       = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address   = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
server-id=107
log-bin=/var/lib/mysql/mysql-bin
相關文章
相關標籤/搜索