第一章 安裝mysql
mysql安裝包:
MySQL-server-5.6.11-1.rhel5.x86_64.rpm MySQL-client-5.6.11-1.rhel5.x86_64.rpm |
安裝mysql:
[root@SALT1 software]# rpm -ivh MySQL-server-5.6.11-1.rhel5.x86_64.rpm Preparing... ########################################### [100%] 1:MySQL-server ########################################### [100%] 2017-03-30 17:10:00 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2017-03-30 17:10:00 24684 [Note] InnoDB: The InnoDB memory heap is disabled 2017-03-30 17:10:00 24684 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2017-03-30 17:10:00 24684 [Note] InnoDB: Compressed tables use zlib 1.2.3 2017-03-30 17:10:00 24684 [Note] InnoDB: Using Linux native AIO 2017-03-30 17:10:00 24684 [Note] InnoDB: Using CPU crc32 instructions 2017-03-30 17:10:00 24684 [Note] InnoDB: Initializing buffer pool, size = 128.0M 2017-03-30 17:10:00 24684 [Note] InnoDB: Completed initialization of buffer pool 2017-03-30 17:10:00 24684 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created! 2017-03-30 17:10:00 24684 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB 2017-03-30 17:10:00 24684 [Note] InnoDB: Database physically writes the file full: wait... 2017-03-30 17:10:00 24684 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB 2017-03-30 17:10:00 24684 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB 2017-03-30 17:10:01 24684 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0 2017-03-30 17:10:01 24684 [Warning] InnoDB: New log files created, LSN=45781 2017-03-30 17:10:01 24684 [Note] InnoDB: Doublewrite buffer not found: creating new 2017-03-30 17:10:01 24684 [Note] InnoDB: Doublewrite buffer created 2017-03-30 17:10:01 24684 [Note] InnoDB: 128 rollback segment(s) are active. 2017-03-30 17:10:01 24684 [Warning] InnoDB: Creating foreign key constraint system tables. 2017-03-30 17:10:01 24684 [Note] InnoDB: Foreign key constraint system tables created 2017-03-30 17:10:01 24684 [Note] InnoDB: Creating tablespace and datafile system tables. 2017-03-30 17:10:01 24684 [Note] InnoDB: Tablespace and datafile system tables created. 2017-03-30 17:10:01 24684 [Note] InnoDB: Waiting for purge to start 2017-03-30 17:10:01 24684 [Note] InnoDB: 5.6.11 started; log sequence number 0 A random root password has been set. You will find it in '/root/.mysql_secret'. 2017-03-30 17:10:01 24684 [Note] Binlog end 2017-03-30 17:10:01 24684 [Note] InnoDB: FTS optimize thread exiting. 2017-03-30 17:10:01 24684 [Note] InnoDB: Starting shutdown... 2017-03-30 17:10:03 24684 [Note] InnoDB: Shutdown completed; log sequence number 1625977
2017-03-30 17:10:03 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2017-03-30 17:10:03 24707 [Note] InnoDB: The InnoDB memory heap is disabled 2017-03-30 17:10:03 24707 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2017-03-30 17:10:03 24707 [Note] InnoDB: Compressed tables use zlib 1.2.3 2017-03-30 17:10:03 24707 [Note] InnoDB: Using Linux native AIO 2017-03-30 17:10:03 24707 [Note] InnoDB: Using CPU crc32 instructions 2017-03-30 17:10:03 24707 [Note] InnoDB: Initializing buffer pool, size = 128.0M 2017-03-30 17:10:03 24707 [Note] InnoDB: Completed initialization of buffer pool 2017-03-30 17:10:03 24707 [Note] InnoDB: Highest supported file format is Barracuda. 2017-03-30 17:10:03 24707 [Note] InnoDB: 128 rollback segment(s) are active. 2017-03-30 17:10:03 24707 [Note] InnoDB: Waiting for purge to start 2017-03-30 17:10:03 24707 [Note] InnoDB: 5.6.11 started; log sequence number 1625977 2017-03-30 17:10:03 24707 [Note] Binlog end 2017-03-30 17:10:03 24707 [Note] InnoDB: FTS optimize thread exiting. 2017-03-30 17:10:03 24707 [Note] InnoDB: Starting shutdown... 2017-03-30 17:10:05 24707 [Note] InnoDB: Shutdown completed; log sequence number 1625987
A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER ! You will find that password in '/root/.mysql_secret'.
You must change that password on your first connect, no other statement but 'SET PASSWORD' will be accepted. See the manual for the semantics of the 'password expired' flag.
Also, the account for the anonymous user has been removed.
In addition, you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test database. This is strongly recommended for production servers.
See the manual for more instructions.
Please report any problems with the /usr/bin/mysqlbug script!
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
New default config file was created as /usr/my.cnf and will be used by default by the server when you start it. You may edit this file to change server settings
[root@SALT1 software]# |
安裝mysql client端:
[root@SALT1 software]# rpm -ivh MySQL-client-5.6.11-1.rhel5.x86_64.rpm Preparing... ########################################### [100%] 1:MySQL-client ########################################### [100%] |
第二章 配置mysql
拷貝配置文件到/etc目錄下:
[root@SALT1 software]# cd /usr/share/mysql/ [root@SALT1 mysql]# cp my-default.cnf /etc/my.cnf |
修改配置文件:在[mysqld]下面添加如下紅色標註的配置
[root@SALT1 mysql]# vi /etc/my.cnf … [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock lower_case_table_names=1 event_scheduler=ON character_set_server=utf8 init_connect='SET NAMES utf8' max_allowed_packet=256M max_connections=1000 … |
保存退出
root密碼重置
查看mysql狀態:
[root@SALT1 mysql]# /etc/init.d/mysql status ERROR! MySQL is not running |
修改配置文件:在[mysqld]下面添加如下紅色標註的配置
[root@SALT1 mysql]# vi /etc/my.cnf … [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock lower_case_table_names=1 event_scheduler=ON character_set_server=utf8 init_connect='SET NAMES utf8' max_allowed_packet=256M max_connections=1000 skip-grant-tables … |
啓動mysql:
[root@SALT1 mysql]# /etc/init.d/mysql start Starting MySQL SUCCESS! |
重置root密碼
[root@SALT1 mysql]# mysql -uroot -p Enter password: 【直接回車就可以】 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.11 MySQL Community Server (GPL)
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> 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 user set password=password('rootroot') where user='root'; Query OK, 4 rows affected (0.00 sec) Rows matched: 4 Changed: 4 Warnings: 0
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
mysql> exit |
修改my.cnf,刪掉skip-grant-tables,開啓權限認證
[root@SALT1 mysql]# /etc/init.d/mysql stop Shutting down MySQL.. SUCCESS! [root@SALT1 software]# vi /etc/my.cnf … [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock lower_case_table_names=1 event_scheduler=ON character_set_server=utf8 init_connect='SET NAMES utf8' max_allowed_packet=256M max_connections=1000 … |
保存退出,啓動mysql
[root@SALT1 mysql]# /etc/init.d/mysql start Starting MySQL. SUCCESS! |
修改root密碼:
[root@SALT1 mysql]# 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.6.11
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> use mysql; ERROR 1820 (HY000): You must SET PASSWORD before executing this statement mysql> set password=password('rootroot'); #密碼可根據實際情況改變 Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
mysql> exit Bye |
上述步驟主庫和從庫都需要操作
第三章 mysql主從同步配置
主庫配置:
停止mysql
[root@SALT1 mysql]# /etc/init.d/mysql stop Shutting down MySQL.. SUCCESS! |
增加主庫配置:增加紅色標註的配置
[root@SALT1 mysql]# vi /etc/my.cnf [mysqld] …. datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock lower_case_table_names=1 event_scheduler=ON character_set_server=utf8 init_connect='SET NAMES utf8' max_allowed_packet=256M max_connections=1000 log-bin=mysql-bin #啓用二進制日誌 binlog_format=mixed #指定二進制日誌格式 server-id=163 #指定服務器ID(建議使用ip地址的主機位) …. |
啓動mysql:
[root@SALT1 mysql]# /etc/init.d/mysql start Starting MySQL SUCCESS! |
創建同步賬號:
[root@SALT1 software]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.6.11-log MySQL Community Server (GPL)
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> GRANT REPLICATION SLAVE ON *.* to 'slave'@'%' identified by 'password123'; Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 398 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
mysql> mysql> mysql> mysql> mysql> exit Bye [root@SALT1 software]# |
現在主庫配置完畢,記錄下File和Position的值,此時主庫不要做任何更改!!!
GRANT REPLICATION SLAVE ON *.* to 'slave'@'%'identified by 'password123'; 命令解析:
創建一個slave的賬號,密碼爲password123,給予該賬號對所有database的replication權限,%代表任意主機都可以用該賬號登錄到mysql,
從庫配置:
停止mysql
[root@SALT1 mysql]# /etc/init.d/mysql stop Shutting down MySQL.. SUCCESS! |
增加從庫配置:增加紅色標註的配置:
[root@SALT1 mysql]# vi /etc/my.cnf [mysqld] …. datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock lower_case_table_names=1 event_scheduler=ON character_set_server=utf8 init_connect='SET NAMES utf8' max_allowed_packet=256M max_connections=1000 log-bin=mysql-bin #啓用二進制日誌 binlog_format=mixed #指定二進制日誌格式 server-id=164 #指定服務器ID(建議使用ip地址的主機位) …. |
啓動mysql:
[root@SALT1 mysql]# /etc/init.d/mysql start Starting MySQL SUCCESS! |
設置從服務器:
[root@SALT1 software]# 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.6.11-log MySQL Community Server (GPL)
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> change master to master_host='192.168.2.163',master_user='slave',master_password='password123',master_log_file='mysql-bin.000001',master_log_pos=398; Query OK, 0 rows affected, 2 warnings (0.03 sec)
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
mysql> start slave; #啓動同步 Query OK, 0 rows affected (0.01 sec)
mysql> show slave status;
+----------------------------------+---------------+-------------+-------------+---------------+------------------+---------------------+------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+ | Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID | Master_Info_File | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | +----------------------------------+---------------+-------------+-------------+---------------+------------------+---------------------+------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+ | Waiting for master to send event | 192.168.2.163 | slave | 3306 | 60 | mysql-bin.000001 | 398 | SALT1-relay-bin.000002 | 283 | mysql-bin.000001 | Yes | Yes | | | | | | | 0 | | 0 | 398 | 456 | None | | 0 | No | | | | | | 0 | No | 0 | | 0 | | | 163 | 0490786c-1519-11e7-a1c4-000c29623e16 | /var/lib/mysql/master.info | 0 | NULL | Slave has read all relay log; waiting for the slave I/O thread to update it | 86400 | | | | | | | | 0 | +----------------------------------+---------------+-------------+-------------+---------------+------------------+---------------------+------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+ 1 row in set (0.00 sec)
mysql> |
參數解釋:MASTER_HOST : 設置要連接的主服務器的ip地址
MASTER_USER : 設置要連接的主服務器的用戶名
MASTER_PASSWORD : 設置要連接的主服務器的密碼
MASTER_LOG_FILE : 設置要連接的主服務器的bin日誌的日誌名稱,即主機查詢到的File的值;
MASTER_LOG_POS : 設置要連接的主服務器的bin日誌的記錄位置,即主機查詢到的Position的值,(這裏注意,最後一項不需要加引號。否則配置失敗)
將結果複製到notepad中:
只要這兩個值爲YES就說明主從配置成功。
測試:在主庫創建一個db,查看備庫是否同步。
主庫163:
[root@SALT1 software]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.6.11-log MySQL Community Server (GPL)
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> create database yqqdb character set utf8; Query OK, 1 row affected (0.00 sec)
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | yqqdb | +--------------------+ 5 rows in set (0.00 sec)
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 593 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
mysql> Ctrl-C -- exit! Aborted [root@SALT1 software]# |
查看從庫164:
mysql> show slave status; +----------------------------------+---------------+-------------+-------------+---------------+------------------+---------------------+------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+ | Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID | Master_Info_File | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | +----------------------------------+---------------+-------------+-------------+---------------+------------------+---------------------+------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+ | Waiting for master to send event | 192.168.2.163 | slave | 3306 | 60 | mysql-bin.000001 | 593 | SALT1-relay-bin.000002 | 478 | mysql-bin.000001 | Yes | Yes | | | | | | | 0 | | 0 | 593 | 651 | None | | 0 | No | | | | | | 0 | No | 0 | | 0 | | | 163 | 0490786c-1519-11e7-a1c4-000c29623e16 | /var/lib/mysql/master.info | 0 | NULL | Slave has read all relay log; waiting for the slave I/O thread to update it | 86400 | | | | | | | | 0 | +----------------------------------+---------------+-------------+-------------+---------------+------------------+---------------------+------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+ 1 row in set (0.00 sec)
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | yqqdb | +--------------------+ 5 rows in set (0.01 sec)
mysql> |
從庫可以看到主庫創建的yqqdb,說明主從配置成功。