lab environment
master 192.168.132.121 main library
slave 192.168.132.122 from the library
A presentation using mysql
1.1 mysql single server features
Shortcoming
If a single server disk problem, the database data is lost
Backup single server, if the data file backup to a local, it is difficult to automate regular backups
A single server can not cope with high concurrent read, only single words, reading and writing are the same, pressure data
advantage
Data does not appear inconsistencies
Simple maintenance
1.2 mysql single server recommended method
If you do only a single server, it can be considered a single server snapshot or cloud storage cloud +
1.3 mysql multiple servers to achieve master-slave replication features
Main advantages of replication from
Master multiple servers to achieve normal, consistent data from the copied mysql multiple servers, small possibility of data loss
High concurrent read pressure can be assigned to multiple servers
Master-slave replication shortcomings
Copy or write than the larger problems of the time, data may be inconsistent
Master-slave replication maintenance is too much trouble
1.4 Master-Slave Replication Works
mysql master-slave replication principle binlog
mysql master-slave replication relies mysql binary log files, master mysql server generates binary log files, is transmitted from the server to execute
mysql database synchronization from the two processes, a process used to synchronize io binlog, a sql process in the data used to perform relaylog
Currently mysql master-slave architecture is quite common, the importance of data
Mysql configuration two main library
2.1 to initialize the data generated password
[root@master ~]# mysqld --initialize --user=mysql
[root@master ~]# grep "password" /var/log/mysqld.log
2019-07-03T15:37:29.068299Z 1 [Note] A temporary password is generated for root@localhost: 0JlLA_qy1V:=
Start MySQL 2.2
[root@master ~]# systemctl start mysqld
[root@master ~]# netstat -ntlp
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1053/sshd tcp6 0 0 :::22 :::* LISTEN 1053/sshd tcp6 0 0 :::3306 :::* LISTEN 8263/mysqld
2.3 modify the configuration
Binlog need to add binary log configuration
[root@master ~]# vim /etc/my.cnf
[mysqld] bind-address=0.0.0.0 port=3306 datadir=/data/mysql socket=/data/mysql/mysql.sock user=mysql skip-name-resolve slow_query_log=on long_query_time=1 slow_query_log_file=/data/mysql/mysql-slow.log innodb-file-per-table=1 innodb_flush_log_at_trx_commit = 2 log_warnings = 1 connect_timeout = 60 of net_read_timeout = 120 performance_schema_max_table_instances = 400 Server - ID = . 1 log -bin = The MASTER bin # open days binary log -bin The MASTER-index = index directory binary log bin.index # [the mysqld_safe] log -error = / Data / MySQL / mysqld.log PID - File = / Data / MySQL / mysqld.pid
server-id #binlog configuration settings need to identify id mysql mysql server to server, each server has a different mysql id. From the beginning logo
log-bin # open binlog, mysql master server needs to open
log-bin-index #binlog stored inside the file, index which kept binlog file name
2.4 Creating a data directory
[root@master ~]# mkdir -pv /data/mysql
[root@master ~]# chown mysql:mysql /data/mysql/
2.5 restart the service, and verify
[root@master ~]# systemctl start mysqld
Machine error troubleshooting, view the log, the error log is as follows
2019-07-03T15:08:32.203843Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation. 2019-07-03T15:08:32.203862Z 0 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory. 2019-07-03T15:08:32.203871Z 0 [ERROR] InnoDB: os_file_get_status() failed on './ibdata1'. Can't determine file permissions 2019-07-03T15:08:32.203886Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error 2019-07-03T15:08:32.808009Z 0 [ERROR] Plugin 'InnoDB' init function returned error. 2019-07-03T15:08:32.808138Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed. 2019-07-03T15:08:32.808162Z 0 [ERROR] Failed to initialize builtin plugins. 2019-07-03T15:08:32.808172Z 0 [ERROR] Aborting
Show there is no authority, but the authority has opened / data / mysql's
Turn off selinux, at the start, success, the problem is selinux
[root@master ~]# getenforce Enforcing [root@master ~]# setenforce 0 [root@master ~]# getenforce Permissive [root@master ~]# systemctl start mysqld
Verify start
[root@master ~]# netstat -ntlp|grep mysql tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 10165/mysqld
Using passwords can not log in
[root@master src]# mysql -uroot -h localhost -p0JlLA_qy1V:= mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) #这个socket不在 [root@master src]# ll /data/mysql/ total 122936 -rw-r-----. 1 mysql mysql 56 Jul 3 11:37 auto.cnf -rw-r-----. 1 mysql mysql 419 Jul 3 11:37 ib_buffer_pool -rw-r-----. 1 mysql mysql 12582912 Jul 3 11:37 ibdata1 -rw-r-----. 1 mysql mysql 50331648 Jul 3 11:37 ib_logfile0 -rw-r-----. 1 mysql mysql 50331648 Jul 3 11:37 ib_logfile1 -rw-r-----. 1 mysql mysql 12582912 Jul 3 11:37 ibtmp1 -rw-r-----. 1 mysql mysql 177 Jul 3 11:37 master-bin.000001 -rw-r-----. 1 mysql mysql 154 Jul 3 11:37 master-bin.000002 -rw-r-----. 1 mysql mysql 40 Jul 3 11:37 master-bin.index drwxr-x---. 2 mysql mysql 4096 Jul 3 11:37 mysql -rw-r-----. 1 mysql mysql 350 Jul . 3 . 11 : 37 [ MySQL- slow.log . Srwxrwxrwx . 1 MySQL MySQL 0 Jul- . 3 . 11 : 37 [ mysql.sock # view here -rw -------. . 1 MySQL MySQL . 6 Jul- . 3 . 11 : 37 [ mysql.sock. Lock drwxr the -X----. 2 MySQL MySQL 8192 Jul- . 3 . 11 : 37 [ performance_schema drwxr . the -X---- 2 MySQL MySQL 8192 Jul- . 3 . 11 : 37 [ SYS [the root @ Master the src] # LN -s / Data / MySQL / mysql.sock # make a soft link, successful mysql.sock mysql.sock.lock [the root @ Master the src] # LN -s / Data / MySQL / mysql.sock / var / lib / MySQL / mysql.sock [the root @ Master the src] # MySQL -uroot--p0JlLA_qy1V -H localhost: =
View the database, you must change the password prompt
mysql> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
change Password
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
Verification success
Three start from the library
The same way
3.1 mysql from the new library configuration relay log relaylog
[mysqld] bind-address=0.0.0.0 port=3306 datadir=/data/mysql socket=/data/mysql/mysql.sock user=mysql skip-name-resolve slow_query_log=on long_query_time=1 slow_query_log_file=/data/mysql/mysql-slow.log innodb-file-per-table=1 innodb_flush_log_at_trx_commit = 2 log_warnings = 1 connect_timeout = 60 net_read_timeout = 120 performance_schema_max_table_instances = 400 server-id = 2 relay-log = relay-log relay-log-index = relay-log.index [mysqld_safe] log-error=/data/mysql/mysqld.log pid-file=/data/mysql/mysqld.pid
Configuration instructions
# server-id identifies each mysql server needs are not the same
relay-log # need to open the relay-log, relay logs from the library
relaylog-index #relaylog also placed inside the file, index where we had stored the file name relaylog
3.2 mysql start from the library
[root@slave ~]# mkdir -pv /data/mysql mkdir: created directory ‘/data’ mkdir: created directory ‘/data/mysql’ [root@slave ~]# chown mysql:mysql /data/mysql [root@slave ~]# getenforce Enforcing [root@slave ~]# setenforce 0 [root@slave ~]# getenforce Permissive [root@slave ~]# mysqld --initialize --user=mysql 2019-07-03T15:56:10.701329Z 0 [Warning] The syntax '--log_warnings/-W' is deprecated and will be removed in a future release. Please use '--log_error_verbosity' instead. 2019-07-03T15:56:10.701519Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2019-07-03T15:56:11.241974Z 0 [Warning] InnoDB: New log files created, LSN=45790 2019-07-03T15:56:11.327038Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2019-07-03T15:56:11.398579Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 141defd2-9dab-11e9-8fe3-000c2963fd11. 2019-07-03T15:56:11.401121Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2019-07-03T15:56:11.426433Z 1 [Note] A temporary password is generated for root@localhost: xJ,RO+8krdGB [root@slave ~]# systemctl start mysqld [root@slave ~]# mysql -u root -pxJ,RO+8krdGB mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) [root@slave ~]# ln -s /data/mysql/mysql.sock /var/lib/mysql/mysql.sock [root@slave ~]# mysql -u root -pxJ,RO+8krdGB mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. ...... Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql> exit
From complete library configuration
Two separate databases, but binlog file has, since the main building from yet, so it is not relaylog
3.3 binary log
[root@master ~]# ll /data/mysql/
master-bin.index record log name
[root@master ~]# cat /data/mysql/master-bin.index