And multi-instance separate read and write

                                            Day0-MySQL . 7 and multi-instance separate read and write

  

Arrangement condition problem

Proxy 5355

Master database 52     from the library 54

Case topology

 • Adding a MySQL proxy

- to provide a unified client database interface

 

 

 Read and write separation principle

Multiple MySQL server

- are providing reading, writing services , balancing traffic

- maintain data consistency through replication from the primary

a MySQL proxy for the client

- receive SQL write request , to the server A process

- receive SQL read request , to the server B treatment

- specific policy set by the distinguished service        

1) installation package

rpm -ivh maxscale-2.1.2-1.rhel.7.x86_64.rpm

[root@host53~]#cd /soft/mysqld/

[root@host53~]#rpm -qa | grep maxscale

maxscale-2.1.2-1.x86_64

[root@host53~]#rpm -qc maxscale                

/etc/maxscale.cnf.template

[root@host53~]#

2) modify the configuration file

[root@host53~]#ls /etc/maxscale.cnf

/etc/maxscale.cnf

[root @ host53 ~] #cp /etc/maxscale.cnf /etc/maxscale.cnf.bak backup files by default

[root @ host53 ~] #vim /etc/maxscale.cnf // profile maxscale.cnf

2) make the appropriate settings based on the profile ( in 2 add user on database server

 

v // master database 52 is added, from the library 54 automatically synchronize

When monitoring database server, users connect to the database server             "Creating a monitor user"

mysql>grant replication slave,replication client on *.* to scalemon@'%'

identified by "123456";

When verifying access data, user database server connection, if there is on the database server, the user is connected ,                                               "create a route users"

mysql>grant select on mysql.*to maxscale@'%' identified by  "123456";

Create a user connection, connect to the database using this user name to access  the "Create a user access to data."

mysql> grant all on *.* to student@'%' identified by  "123456";

Check the main library authorized users from library

      mysql> select user,host from mysql.user where user in("scalemon","maxscale");

4) Verify that authorized users can log in on the proxy server

[root@host53~]#mysql -h192.168.4.52 -uscalemon -p123456

[root@host53~]#mysql -h192.168.4.54 -umaxscale -p123456

5) Start the service

[root@host53~]#maxscale -f  /etc/maxscale.cnf

6) Check service processes and ports

View port

[root@host53~]#netstat -utnlp | grep :4006

[root@host53~]#netstat -utnlp | grep:4018

[root@host53~]#maxadmin  -P4018  -uadmin  -pmariadb

MaxScale>list servers

7) Client separate read and write access to the data server

]#which mysql

]#mysql -h192.168.4.53  -p4006 -ustudent -p123456

mysql> select @@ hostname; // this command , view the host reads the host

mysql> insertion or query (at 51 and 52 is native view records)


 Configure multi-instance mysql

mysql multi-instance principle

    mysql multi-instance, simply put, it is to open a number of different on a single server mysql service port (such as     3306 , 3307 ), running multiple mysql service process. The service process through different socket monitor different service ports to provide their services.

These mysql examples share a mysql installer using different my.cnf configuration file, start the program, the data file. In providing services, MySQL multi-instance is logically appears to be independent, between each instance according to the value of the configuration file to obtain the relevant hardware resources of the server.

Requirements: host 192.168.4.56 arranged mysql multi-instance:

Run 2 database services

²  first 1 database services database directory / dataone

Service port number 3307

sock file /dataone/mysqld.sock

Log Files /dataone/mysqld.log

²  The first 2 database services

Database directory / datatow

Service port number 3308

sock file /datatwo/mysqld.sock

Log files /datatwo/mysqld.log environment ready

Netstat - utnalp | grep:3307

Netstat - utnalp | grep:3308

1) Download Software

[root@redhat~]#wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz

[root@host50~]#tar -xf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz

[root@host50~]#mv mysql-5.7.20-linux-glibc2.12-x86_64/usr/local/mysql

[root@host56~]#echo "export PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile

[root@host56~]#source /etc/profile

[root@host56~]#echo $PATH

/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

2 ) edit the configuration file

]#rm -rf /etc/my.cnf

]#vim /etc/my.cnf

[mysqld_multi]

mysqld=/usr/local/mysql/bin/mysqld_safe

mysqladmin=/usr/local/mysql/bin/mysqladmin user=root

[mysqld1]

port=3307

datadir=/dataone

socket=/dataone/mysqld.sock

log-error=/dataone/mysqld.log

pid-file=/dataone/mysqld.pid

[mysqld2]

port=3308

datadir=/datatwo

socket=/datatwo/mysqld.sock

log-error=/datatwo/mysqld.log

pid-file=/datatwo/mysqld.pid

:wq

3 ) The setting of the configuration file, corresponding configuration

3 .1 create a database directory

3 .2 create processes running owner and group mysql

[root@host56~]#mkdir -p /dataone

[root@host56~]#mkdir -p /datatwo

[root@host56~]#useradd mysql

c[root@host56~]#chown mysql:mysql /data*

3 .3 initialization authorize

]#mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/dataone  --initialize

2018-05-05T08:47:48.992696Z 1[Note]A temporary password is generated for root@localhost:bXk.5j!pjto#

[Root @ host56 ~] #ls / dataone /

]#mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/datatwo  --initialize

2018-05-05T08:50:09.429934Z 1[Note]A temporary password is generated for root@localhost:bKsaf+xzk0V3

[root@host56~]#ls /datatwo/

4 ) Start multiple instance Service

[root@host56~]#mysqld_multi start 1

[root@host56~]#mysqld_multi start 2

[root@host56~]#netstat -utnlp | grep:3308

tcp6 0 0:::3308:::*LISTEN 1156/mysqld

[root@host56~]#netstat -utnlp | grep:3307

tcp6 0 0:::3307:::*LISTEN 927/mysqld

5 ) access to multiple instances of the service

Examples of services connected 1

[root@host56~]#mysql -uroot -p'bXk.5j!pjto#' -S  /dataone/mysqld.sock

mysql>ALTER USER user()identified by"123456";

mysql>show databases;

[root@host56~#mysql -uroot -p123456 -S /dataone/mysqld.sock

6 ) connection service Example 2

[root@host56~]#mysql -uroot -p'bKsaf+xzk0V3' -S    /datatwo/mysqld.sock

mysql>alter user user() identified by"123456";

mysql>show databases;

[root@host56~]#mysql -uroot -p123456 -S  /datatwo/mysqld.sock

7) stop-start an instance of service

] #mysqld_multi --user = root --password = password   stop Example No.

[root@host56~]#mysqld_multi --user=root --password=123456 stop 1

[root@host56~]#netstat  -utnlp | grep:3307

[root@host56~]#mysqld_multi --user=root --password=123456 stop 2

[root@host56~]#netstat  -utnlp | grep:3308

[root@host56~]#mysql -uroot -p123456 -S  /datatwo/mysqld.sock

Note: not start, start the database

*************************************************


 

to sum up:

mysql optimization

1 ) Enable the slow query log

vim/etc/my.cnf

[mysqld]

slow-query-loglong-query-time=2

log-queries-not-using-indexes 

:wq

]#systemctl restart mysqld

]#mysql-uroot-p123456

mysql>select sleep(3);

mysql>select sleep(5);

View the contents of the log file

] #cat / var / lib / mysql / hostname -slow.log

Statistical information log file records

~]#mysqldumpslow /var/lib/mysql/db55-slow.log > /tmp/sql.txt

Enable query log

vim/etc/my.cnf

[mysqld]

general-log

]#systemctl restart mysqld

]#cat /var/lib/mysql/db55.log

MySQL Performance Tuning

mysql> show variables like "% variable name %";

• Improve MySQL performance, response speed of the system

- replace the problematic hardware (CPU / disk / memory, etc. )

- adjust the operating parameters of the service program

- for SQL optimization inquiry

1 ) View variables

mysql>show variables like"%time%";

2 ) set the wait time is connected

mysql>set innodb_lock_wait_timeout=100;

3 ) Check the maximum allowed concurrent connections

mysql>show variables like"%max_connections%";

4 ) Change the connection time

mysql>set global max_connections=300;

5 ) Use the largest connector

mysql>show global status like"max_used_connections";

6 ) Refresh

mysql>flush status;

7 ) wait timeout in seconds to establish connection , the default 10 Miao , only effective when log

mysql>show variables like"%connect_timeout%";

8) waits for closing the connection inactivity timeout in seconds , default 28800 seconds (8 hours )

mysql>show variables like"%wait_timeout%";

9 ) Cache parameter control unit: bytes

• Buffer, the number of threads, the number of open tables

Option item Meaning

key_buffer-size for MyISAM key index cache engine size

sort_buffer_size it allocates buffer space of this size for each thread to be sorted

read_buffer_size to sequentially read record reserved buffer size table

thread_cache_size allows you to keep the number of threads in the cache to be reused

table_open_cache for all open thread cache of the number of tables

query_cache query cache

+------------------------------+---------+--------------

|Variable_name|Value|

+------------------------------+---------+--------------

|query_cache_limit|1048576|

| Query_cache_min_res_unit | 4096 |

|query_cache_size|1048576|

|query_cache_type|OFF|

|query_cache_wlock_invalidate|OFF|

+------------------------------+---------+---------------

10 ) view the current query cache statistics

mysql>show global status like"qcache%";

11) All software is no independent log storage place

[root@host52~]#vim /var/log/messages

12 ) record slow queries

²  Option Meaning

slow-query-log enable slow query

slow-query-log-file Specifies the slow query log file

long-query-time exceeds the specified number of seconds ( default 10 seconds ) query record was only

log-queries-not-using- indexes recorded not use the index query

²  adjustment service configuration

[root@dbsvr1~]#vim /etc/my.cnf

[mysqld]

....

slow_query_log=1

slow_query_log_file=mysql-slow.log

long_query_time=5

log_queries_not_using_indexes=1

[root@dbsvr1~]#service mysql restart

  • Common Log Types and Options < generally open >

²  type configuration uses

Error Logging Start / Run / stop cycle L OG-error [= name] Error messages 

Query logging client connections and query operations general-log

general-log-file=

Slow query logging takes a long time or do not use indexes of query operations slow-query-log

slow-query-log-file=

long-query-time=

[root@host52 mysql]#vim /etc/my.cnf

[mysqld]

general-log // query log host52.log

slow-query-log // slow query log host52-slow.log

long-query-time=5

log-queries-not-using-indexes

[root@host52 mysql]#systemctl restart mysqld

1 ) to save the new file

[root@host52 mysql]#mysqldumpslow host52-slow.log > /tmp/sql.txt

2 ) dynamic query log files record

[root@host52 mysql]#tail -f host52-slow.log

  ###########################################   


 

Apply for quick settings from the main library library

    Semi-open synchronous replicated    /et/my.cnf

       plugin-load= "rpl_semi_sync_master=semisync_master.so;

       rpl_semi_sync_slave=semisync_slave.so"

       rpl-semi-sync-master-enabled = 1

       rpl-semi-sync-slave-enabled = 1

 

master library set   /et/my.cnf

[mysqld]

#skip-grant-tables

plugin-load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"

rpl-semi-sync-master-enabled = 1

rpl-semi-sync-slave-enabled = 1

server_id=54

log-bin=master54

binlog-format="mixed"

lower_case_table_names = 1

validate_password_policy=0

validate_password_length=6

 

from the library   /et/my.cnf

[mysqld]

skip-grant-tables

server_id=56

#validate_password_policy=0

#validate_password_length=6

 

 

Guess you like

Origin www.cnblogs.com/qingbai/p/12015255.html