Mysql installation and master-slave replication configuration in linux (64) centos (6.7) environment

 It mainly consists of three parts: installation of mysql, configuration of mysql master and slave, and testing of master-slave replication. 1.
Installation of mysql in linux (64) centos (6.7) environment
1. Download mysql-5.7.19-linux-glibc2 from the official website .12-x86_64.tar.gz
official website: https://dev.mysql.com/downloads/mysql/


2. Check whether mysql has been installed under Linux and uninstall it cleanly


#rpm -qa|grep -i mysql
mysql-5.7 .18-linux-glibc2.5-x86_64
shows that the library file has been installed. Uninstall it first. Note that the --nodeps option is used during uninstallation and the dependencies are ignored:
#rpm -e mysql-5.7.13-linux-glibc2 .5-x86_64 --nodeps


3. Create mysql user group/user, data directory and user directory.
First ensure that the created user and user group do not exist, otherwise an error will be reported. When deleting, delete the user first and then delete the user group name. .


  # userdel mysql // Delete user 
  # groupdel mysql // Delete user group name
  # mkdir /home/mysql //Create the folder mysql under the home folder
  # mkdir /home/mysql/data // Create the folder data under the mysql folder   
  # groupadd mysql //Create a user group named mysql        
  # useradd -g mysql -d /home/mysql mysql // Create a user under the user group
4. Unzip the installation package and copy the contents of the unzipped package to the mysql installation directory /home/mysql


# tar -xzvf mysql-5.7.19 -linux-glibc2.12-x86_64.tar.gz # Unzip the file
# cd mysql-5.7.19-linux-glibc2.12-x86_64 # Enter
# mv * /home/mysql # Move to the mysql folder I created before .
5. Initialize the mysql database
# cd /home/mysql # Enter the installation directory
# ./bin/mysqld --user=mysql --basedir=/home/mysql --datadir=/home/mysql/data --initialize
# ./mysqld --user=mysql --basedir=/home/mysql - -datadir=/home/mysql/data --initialize
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 
[ERROR] --initialize specified but the data directory has files in it. Aborting. 2017-09-1201:46:53.155879Z 0 
[ERROR]
The above error occurred in Aborting because we did not clear the data directory of mysql. Execute the clearing command as follows:  
# cd /home/mysql/data # Enter Data directory under the installation directory
# rm -fr * # Clear data
# cd /home/mysql       
# ./bin/mysqld --user=mysql --basedir=/home/mysql --datadir=/home/mysql/data --initialize 
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2016-04-08T01:47:59.945537Z 0 
[Warning] InnoDB: New log files created, LSN=45790 2017-09-12T01:48:00.333528Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2016-04-08T01:48:00.434908Z 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: ece26421-fd2b-11e5-a1e3-00163e001e5c. 2016-04-08T01:48:00.440125Z 0 
[Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2017-09-12T01:48:00.440904Z 1 [ 
Note] A temporary password is generated for root@localhost: %&*T ,#x98sWRemember
the random password above, as above %&*T,#x98sW, which is needed when changing the password.


6. Check whether the mysql service can be started.
The default installation directory of the tar.gz installation package of mysql is /usr/local/mysql. You need to modify the basedir and datadir directory paths of the /support-files/mysql.server file to the mysql environment. The basedir and datadir paths are as follows:


# vim support-files/mysql.server 
-------------------------- 
... 
basedir=/home /mysql 
datadir=/home/mysql/data 
... 
--------------------------
# ./support-files/mysql.server start
start Starting MySQL.. OK! 


7. Create a soft link


# ln -s /home/mysql/bin/mysql /usr/bin/mysql
8. Create configuration file
# vim /etc/my.cnf


[mysqld]


basedir = /home/mysql
datadir = /home/mysql/data


character_set_server= utf8
init_connect='SET NAMES utf8'




[client]
default-character-set=utf8


9. Configure the mysql service to start automatically at boot
# cp /home/mysql/support-files/mysql.server /etc/init.d/mysqld // Copy the startup file to /etc/init.d/ and re-enter the command as mysqld
# chmod 755 /etc/init.d/mysqld // Increase execution permissions
# chkconfig --list mysqld // Check that there is no mysqld in the self-startup item list.
# chkconfig --add mysqld // If not, add mysqld:
# chkconfig mysqld on // Use this command to set the startup:


10. Start/restart/stop of the mysql service
# service mysqld start # Start the service
# service mysqld restart # Restart the service
# service mysqld stop # Stop the service
11. Initialize the mysql user root Password
[root@testfornss ~]# mysql -u root -p
Enter password: the random password just now
mysql> SET PASSWORD = PASSWORD('123456'); # 123456 in PASSWORD() is the new password set
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 1
mysql> ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 1
12.更改一些编码
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> \s
--------------
/usr/soft/mysql/bin/mysql  Ver 14.14 Distrib 5.7.13, for linux-glibc2.5 (x86_64) using  EditLine wrapper


Connection id: 2
Current database: mysql
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.13 MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: latin1 --- needs to be modified to the encoding you want
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /tmp/mysql.sock
Uptime: 27 sec


Threads: 1  Questions: 43  Slow queries: 0  Opens: 136  Flush tables: 1  Open tables: 129  Queries per second avg: 1.592
--------------


mysql> show variables like 'character%';  
+--------------------------+---------------------------------+
| Variable_name            | Value                           |
+--------------------------+---------------------------------+
| character_set_client     | utf8                            |
| character_set_connection | utf8                            |
| character_set_database   | latin1                          |
| character_set_filesystem | binary                          |
| character_set_results    | utf8                            |
| character_set_server     | utf8                            |
| character_set_system     | utf8                            |
| character_sets_dir       | /usr/soft/mysql/share/charsets/ |
+--------------------------+---------------------------------+
8 rows in set (0.00 sec)


mysql> SET character_set_database = utf8;                     ----设置编码
Query OK, 0 rows affected, 1 warning (0.00 sec)


mysql> show variables like 'character%'; 
+--------------------------+---------------------------------+
| Variable_name            | Value                           |
+--------------------------+---------------------------------+
| character_set_client     | utf8                            |
| character_set_connection | utf8                            |
| character_set_database   | utf8                            |
| character_set_filesystem | binary                          |
| character_set_results    | utf8                            |
| character_set_server     | utf8                            |
| character_set_system     | utf8                            |
| character_sets_dir       | /usr/soft/mysql/share/charsets/ |
+--------------------------+---------------------------------+
8 rows in set (0.00 sec)


mysql> \s
--------------
/usr/soft/mysql/bin/mysql  Ver 14.14 Distrib 5.7.13, for linux-glibc2.5 (x86_64) using  EditLine wrapper


Connection id: 2
Current database: mysql
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.13 MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db     characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /tmp/mysql.sock
Uptime: 3 min 28 sec


Threads: 1 Questions: 52 Slow queries: 0 Opens: 137 Flush tables: 1 Open tables: 130 Queries per second avg: 0.250
--- -----------


mysql> 
  


13. mysql remote authorization


[root@testfornss ~]# mysql -u root -p
Enter password:
mysql> grant all privileges on *.* to 'root'@'% ' identified by '123456' with GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.01 sec)


If you do not set up a remote connection, you cannot connect using the mysql database connection tool! ! !


14. Use the database connection tool to test whether the installation is successful
. First, you need to know the IP address of the Linux system to be connected remotely. Make sure that the local machine can ping successfully before connecting.
2. Mysql master-slave configuration
Follow the steps above to install mysql5.7.19 on the machines to be used as master and slave.
1. Environment preparation
Two Linux hosts, both operating systems are CentOS6.7, and both have the same version of MySQL installed (MySQL5.7.19).
Turn off chkconfig iptables off on both the master and slave server firewalls.
The relevant information is as follows:
[Master server]
IP: 192.168.6.207
[Slave server]
IP: 192.168.6.208
2. First modify the configuration /etc/my.cnf to support the binary log function
[Master server]
Add the following to the configuration file Three lines of code:
vim /etc/my.cnf
socket=/tmp/mysql.sock
log-bin=mysql-bin  
binlog_format=mixed  
server-id=207 //100 in server-id is the last digit of host 207. Easy to distinguish. Of course, other values ​​can also be set, but they should not be the same as the slave server.
After user=mysql
port=3306
is added, save it and restart MySQL.


[Slave server]
Add the code as above on the slave server, but the server-id is different.
vim /etc/my.cnf
socket=/tmp/mysql.sock
log-bin=mysql-bin  
binlog_format=mixed  
server-id=208 
user=mysql
port=3306 
slave-skip-errors=all
Save the settings and restart MySQL.


3. Assign an account to the slave server on the master server, just like a key. Only with this key can the slave server go to the master server to share the master server's log files.
[Main Server]
Use the root user to log in to the MySQL database of the main server and execute the following command to create an account.
GRANT replication slave ON *.* TO 'slave'@'%' IDENTIFIED BY '123456'; 
4.mysql>show master status; View the BIN log information of the master server (record the values ​​of File and Position after execution, and then Do not do anything before configuring the slave server, because these two values ​​​​of the server will change every time you operate the server)
5. [Slave server]
Use the root user to log in to MySQL on the slave server. 
Close the slave (if master-slave replication has been set up before) )
stop slave;  
CHANGE MASTER TO MASTER_HOST="192.168.6.207",MASTER_USER="slave",MASTER_PASSWORD="123456",MASTER_LOG_FILE="mysql-bin.000001",MASTER_LOG_POS=438; 6. Start the slave server start slave; view the slave  
server
status  
, If the values ​​of Slave_IO_Running and Slave_SQL_Running are both YES, it means the configuration is successful
mysql> show slave status;  
3. Test
1. First check which databases are on the master and slave servers


[master server]
[html] view plain copy
mysql> show databases;  
+--------------------+  
| Database |  
+ --------------------+  
| information_schema |  
| mysql |  
| performance_schema |  
| sys |  
+---------------- ----+  


【Slave server】
[html] view plain copy
mysql> show databases;  
+--------------------------+  
| Database |  
+------------- -------+  
| information_schema |  
| mysql |  
| performance_schema |  
| sys |  
+--------------------+  


2. Create a new one on the main server Database testdb
[html] view plain copy
create database testdb;  


[html] view plain copy
mysql> show databases;  
+--------------------+  
| Database |  
+- ------------------+  
| information_schema |  
| testdb |  
| mysql |  
| performance_schema |  
| sys |  
+--------------------+  
5 rows in set (0.00 sec)  


3. Check whether the database testdb is also available on the slave server. If Yes, it means that master-slave replication is normal. You're done!


[html] view plain copy
mysql> show databases;  
+--------------------------+  
| Database |  
+------------- -------+  
| information_schema |  
| testdb |  
| mysql |  
| performance_schema |  
| sys |  
+--------------------+  

Guess you like

Origin blog.csdn.net/qq_28453175/article/details/77968331