Linux CentOS installs MySql and builds MySql master-slave replication

foreword

In the previous blogs, I have written several tutorials about the construction of mysql under linux, and I may write them again in the future, but I don’t want to repeat them, so I want to extract this separately and write a separate blog. , and record in detail some installation processes and solutions to problems encountered. By the way, the master-slave construction tutorial of MySql is also written together, which is convenient for future reference.

1. MySql installation

Before installing MySql, check whether or not MySql has been installed. If it has been installed, but it does not meet the requirements, uninstall it.
1. To find out if mysql was installed
before, enter:

rpm -qa|grep -i mysql

Check if mysql is installed
write picture description here

2. Stop the mysql service and delete the previously installed mysql
input:

ps -ef|grep mysql

delete command
enter :

rpm -e –nodeps 包名

write picture description here
If it prompts a dependency package error, use the following command to try

rpm -ev 包名 --nodeps

If it prompts an error: error: %preun(xxxxxx) scriptlet failed, exit status 1
, try with the following command:

rpm -e --noscripts 包名

3. Find and delete the mysql directory The
search results are as follows:

find / -name mysql

Delete the corresponding mysql directory
The specific steps are as shown in the figure: Find the directory and delete it
write picture description here
Note: /etc/my.cnf will not be deleted after uninstallation, it needs to be deleted manually

4. Find out if mysql is installed on the machine again

rpm -qa|grep -i mysql

write picture description here

Mysql has two installation modes, you can choose by yourself.

1, yum installation

First check if mysql is installed by
typing :

rpm -qa | grep mysql   

If it has been installed and you want to delete it,
enter :
normal delete command:

rpm -e mysql

Force delete command:

rpm -e --nodeps mysql    

Dependent files are also removed

Install mysql
input:

yum list mysql-server  

If not, download the package via wget command
Enter :

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm   

After the download is successful, enter the command to install

yum install mysql-server

During the installation process, you can choose to enter y.
write picture description here
write picture description here

After the installation is successful, enter service mysqld start to start the service
Enter :
mysqladmin -u root -p password '123456'
to set the password, press
Enter after entering (the default is no password)
and then enter
mysql -u root -p
write picture description here
to change through the authorization method Remote connection permission
Input : grant all privileges on . to 'root'@'%' identified by '123456';
Note: The first 'root' is the username, the second '%' is that all ips can be accessed remotely , the third '123456' indicates that the user password is closed if it is not commonly used
Enter : flush privileges; //refresh

After the firewall is turned off, use a tool like SQLYog to test if you can connect correctly

write picture description here

2. Compile package installation

Mysql file preparation

Upload the downloaded mysql installation package to the linux server,
decompress mysql decompression package, move it to the /usr/local directory, and rename it to mysql.
Order:

tar -xvf mysql-5.6.21-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.6.21-linux-glibc2.5-x86_64 /usr/local
cd /usr/local
mv mysql-5.6.21-linux-glibc2.5-x86_64 mysql

write picture description here

Note: The default path of mysql is /usr/local/mysql. If the installation location is changed, the corresponding configuration file needs to be changed.

install mysql

Switch to the mysql directory /usr/local/mysql and
enter:

 ./scripts/mysql_install_db --user=mysql

write picture description here
After successfully installing mysql, enter
service mysql start or /etc/init.d/mysql start

write picture description here
Check if the startup is successful
Enter :

 ps -ef|grep mysql

Switch to the /usr/local/mysql/bin directory,
set password
mysqladmin -u root password '123456', enter the mysql
input:

mysql -u root -p

Set remote connection permissions
Enter :

grant all privileges on *.* to 'root'@'%' identified by '123456'; 

Then enter:

flush privileges;

Description: The first 'root' is the username, the second '%' is that all IPs can be accessed remotely, and the third '123456' means that the user password is closed if it is not used frequently.

Use the local connection tool to connect the test

write picture description here

Second, the error solution encountered in MySql construction

1. The offline installation of mysql appears: Can't change to run as user 'mysql' ; Please check that the user exists!

Reason: The user does not exist.
Solution: Create the user!
For example:
add user:

groupadd -g 315 mysql

User join mysql:

useradd -u 315 -g mysql -d /usr/local/mysql -M mysql

re-enter

 ./scripts/mysql_install_db --user=mysql

success!
write picture description here

2. After installing mysql, enter service mysql start to prompt mysql: unrecognized service.

Cause of the problem: Because the mysql command does not exist in /etc/init.d/, it cannot be recognized.
Solution:
1. First find where the mysql.server file is.
Enter :

find  /  -name mysql.server

2. Copy mysql.server to the /etc/init.d/ directory and rename it to mysql or mysqld

enter:

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

3. Test if the command can be used by
typing :

service mysql status

or

service mysqld status

write picture description here

3, mysql data import error

Error: Got a packet bigger than 'max_allowed_packet'bytes problem

2 workarounds:

1. Temporary modification: mysql>set global max_allowed_packet=524288000; modify #512M

2. Permanent modification: Edit my.cnf, add a sentence in the [MySQLd] section (if it exists, adjust its value):
max_allowed_packet=10M

Restart mysql after success.

4. The problem of max_allowed_packet in mysql database

The main reason is that the JDBC connection of mysql limits the maximum packet length to 1024B, that is,
the packet length of the 1KB query of the current database is displayed: show VARIABLES like '%max_allowed_packet%';
modify this parameter
Solution 1 (temporary): Enter msyql
input: change the maximum value

set global max_allowed_packet = 2*1024*10

Solution 2 (permanent): Modify mysql.cnf (my.ini under windows), and modify it in the [mysqld] section or the server configuration section of mysql.

max_allowed_packet = 20K。

After the modification is successful, restart mysql.

3. Some commands used by MySql

1. View the number of mysql connections

  SHOW FULL PROCESSLIST;

2. View the configuration of mysql

show variables like "%innodb%";

3. Check whether the mysql event is enabled

show variables like 'event_scheduler';

4. View the status of the mysql lock

Whether to lock the table:

 SHOW OPEN TABLES WHERE In_use > 0;
SHOW INNODB STATUS/G;

5. View the storage location of mysql data

SHOW VARIABLES LIKE '%datadir%'

6. View the timeout setting of mysql

show variables like '%timeout%';

Fourth, MySql change password

1. Know the root password of the original myql database;

Modify at the terminal command line

enter

 mysqladmin -u root -p password "新密码"

Enter

 Enter password: 【输入原来的旧密码】

Login mysql system modification

Enter mysql
input:

 mysql -uroot -p 回车 Enter password: 【输入原来的密码】

After entering,
enter :

use mysql;
update user set password=password("新密码") where user='root';
flush privileges;

Then use the new password you just entered to log in.

2. I don't know the root password of the original myql;

First, you must have root privileges on the operating system. If you don't even have root privileges on the system, consider the root system first and then follow the steps below. Similar to safe mode login system.
You need to stop the mysql service first

service mysql stop

or

/etc/init.d/mysql stop

Note: If mysql does not work, use mysqld
to proceed to the next step after prompting that mysql has stopped

Enter at the terminal command line:

mysqld_safe --skip-grant-tables &  

Enter mysql to log in to the mysql system

use mysql;
UPDATE user SET password=password("新密码") WHERE user='root';      
flush privileges;
exit;

Restart the mysql service
so that the new root password is set successfully.

Five, MySql master-slave construction

Master-slave construction reference: http://www.cnblogs.com/phpstudy2015-6/p/6485819.html#_label2

Note: When setting up the master-slave of MySql, if there is an error in copying the name, try to use the manual input command.

1. Prepare in advance

Information
Operating system : CentOS6.8
Mysql: 5.6
IP: 192.169.2.156 (master), 192.169.2.98 (slave)

Make sure MySql has been successfully installed!

2. Configuration changes

Change the my.cnf file in mysql and
add as follows:
Master:


#任意自然数n,只要保证两台MySQL主机不重复就可以了。
server-id=1
#开启二进制日志
log-bin=mysql-bin  
#步进值auto_imcrement。一般有n台主MySQL就填n 
auto_increment_increment=2
#起始值。一般填第n台主MySQL。此时为第一台主MySQL   
 auto_increment_offset=1 
#忽略指定的数据库 
binlog-ignore=mysql   
binlog-ignore=information_schema  
#要同步的数据库(多个可以使用逗号隔开),默认所有库
replicate-do-db=DBTEST  

From (Slave):

server-id=2
log-bin=mysql-bin
auto_increment_increment=2
auto_increment_offset=2
replicate-do-db=DBTEST 

Reboot after making configuration changes!

3. Master-slave configuration

The master (Master)
master creates a new slave user After
entering MySql, enter:

grant replication slave on *.* to 'slave_account'@'192.169.2.98' identified by '123456'; 

Description : Set a slave_account user to have REPLICATION SLAVE authority, the ip that can be accessed is 192.169.2.98, and the password is set to 123456.
If you want to be able to log in to a server on a certain network segment, you can use 192.168.2.%. It means that all servers from 192.168.2.0-192.168.2.255 can log in to the master server with the slave_account user.
If you want all servers to be able to log in, you can set it to %.

Then enter:

flush privileges;

View the information and record the file name and partition ID

SHOW MASTER STATUS;

write picture description here

From (Slave):
After obtaining the authorization information of the master, enter the authorization command of the slave:

change master to master_host='192.169.2.156',master_user='slave_account',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=427;

Ensure successful execution!

4. Master-slave test

Slave:
start master-slave replication:
input:

START SLAVE;

Check whether master-slave replication is configured successfully:

SHOW SLAVE STATUS\G

write picture description here

Description: When you see Slave_IO_Running: YES, Slave_SQL_Running: YES, it means success, otherwise it fails.

The master (Master)
first creates a DBTEST database, then creates the test table, and inserts two pieces of data.
In order to make it clearer here, I am using the SQLYog tool to operate.
Enter mysql and execute the following script:

CREATE DATABASE`DBTEST`;
USE `DBTEST`;
CREATE TABLE `test` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(16) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

insert  into `test`(`id`,`name`) values (1,'张三'),(2,'李四');

View
input :

select * from test;

write picture description here

Slave:
View
Input :

select * from test;

write picture description here

The script is not executed from the database, but the information is consistent, indicating that the synchronization is successful.

Continue the test, delete a piece of data on the master (master), and view the data from the slave.
Master
write picture description here

From (Slave):
write picture description here

You can see that it has been completely synchronized!

So this is the end of this tutorial, thank you for reading!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325477945&siteId=291194637