当前位置:网站首页>MySQL installation, architecture and management

MySQL installation, architecture and management

2022-06-13 05:11:00 I love Qianxi

A、mysql Preparation for installation

One 、 install

1、Mysql 5.7.26 Binary version installation ( Software Guide :www.mysql.com==>download archives)

Download and upload the software to /server/tools, And decompress it and move it to a dedicated /application Under the table of contents , Name it mysql

[[email protected] ~]# cd /server/tools
[[email protected] tools]# ls
mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
[[email protected] tools]# tar -xf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
[[email protected] tools]# mkdir /application
[[email protected] tools]# ls
mysql-5.7.26-linux-glibc2.12-x86_64
mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
[[email protected] tools]# mv mysql-5.7.26-linux-glibc2.12-x86_64 /application/mysql

2、 Process the initial environment of the system and create users

Dealing with the original environment

[[email protected] tools]# rpm -qa|grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[[email protected] tools]# ls /etc/my.cnf
/etc/my.cnf

 The existence of this library and this configuration file will affect our future mysql Database initialization , There are two solutions :
1. Delete or remove configuration files ;2. Delete this mariadb software package 

 Here, choose the uninstall method :
[[email protected] ~]# rpm -e mariadb-libs-5.5.60-1.el7_5.x86_64
 error : Dependency detection failed :
        libmysqlclient.so.18()(64bit)  By  ( Installed ) postfix-2:2.10.1-7.el7.x86_64  need 
        libmysqlclient.so.18(libmysqlclient_18)(64bit)  By  ( Installed ) postfix-2:2.10.1-7.el7.x86_64  need 
[[email protected] ~]# yum remove -y mariadb-libs-5.5.60-1.el7_5.x86_64
rpm If you can't uninstall, use yum Mode unload 

Create user

[[email protected] ~]# useradd -s /sbin/nologin mysql
/sbin/nologin  Login not allowed 

3、 Set the environment variable

[[email protected] ~]# vim /etc/profile
[[email protected] ~]# cat /etc/profile|tail -1
export PATH=/application/mysql/bin:$PATH
[[email protected] ~]# source /etc/profile
[[email protected] ~]# mysql -V
mysql  Ver 14.14 Distrib 5.7.26, for linux-glibc2.12 (x86_64) using  EditLine wrapper
 You can see the version description that the environment variable is set successfully 

4、 Create a data disk and authorize

(1) Add a new disk to simulate... In production mysql Data disk
Edit virtual machine settings ( Shutdown status ), Keep clicking next , add to 5G Hard disk

 Insert picture description here

Here's the picture , Just add

 Insert picture description here

(2) Format and mount

[[email protected] ~]# mkfs.xfs /dev/sdb
[[email protected] ~]# mkdir /data     # Create mount directory 
[[email protected] ~]# blkid           # Check the disk UUID Number 
/dev/sdb: UUID="a7bc02f4-cc2d-4bd2-b336-4d980994300c" TYPE="xfs"
[[email protected] ~]# cat /etc/fstab|tail -1   # Add the following content to the mount file 
UUID=a7bc02f4-cc2d-4bd2-b336-4d980994300c /data xfs defaults 0 0
[[email protected] ~]# mount -a     # take /etc/fstab All the content of is reloaded 
[[email protected] ~]# df -h        # Check disk status 
 file system                   Capacity    Already used    You can use   Already used %  Mount point 
/dev/mapper/centos-root   10G  4.7G  5.4G   47% /
devtmpfs                 475M     0  475M    0% /dev
tmpfs                    487M     0  487M    0% /dev/shm
tmpfs                    487M  7.7M  479M    2% /run
tmpfs                    487M     0  487M    0% /sys/fs/cgroup
/dev/sr0                  11G   11G     0  100% /media
/dev/sda1                197M  111M   87M   57% /boot
tmpfs                     98M     0   98M    0% /run/user/0
/dev/sdb                 5.0G   33M  5.0G    1% /data

(3) to grant authorization

[[email protected] ~]# mkdir /data/mysql/data -p
[[email protected] ~]# chown -R mysql:mysql /application/mysql
[[email protected] ~]# chown -R mysql:mysql /data/

5、 Initialization data ( Create system data )

mysql Work needs Software part and data part , The data part does not come with , There are tools or commands to create initialization data , This command is located at /application/mysql/bin Under the mysqld(5.7 edition )

1、 In the manner of official requirements

[[email protected] bin]# mysqld --initialize --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data/
 annotated :
#mysqld yes /application/mysql/bin Under the mysqld
#--initialize  Customize the password complexity ,12 position ,4 Kind of , to [email protected] Set temporary password 

If the following sentence appears, the initialization is successful

2021-05-19T10:46:34.660347Z 1 [Note] A temporary password is generated for [email protected]:  Temporary password 

Refer to the solution if there are the following errors
 Insert picture description here
2、 Used more in production

In execution 1 Method , To execute 2 command , You have to delete /data/mysql/data The initialized contents in

 add to --initialize-insecure   unlimited , No temporary password 
[[email protected] data]# mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data/
2021-05-19T11:00:56.068499Z 1 [Warning] [email protected] is created with an empty password ! Please consider switching off the --initialize-insecure option.
 That's a success 

Two 、 Preparation of configuration file

The function of the initialization configuration file : Affect database startup (mysqld,mysqld_safe,server) And client connection (mysql,mysqldump,mysqladmin,client)

[[email protected] data]# vim /etc/my.cnf
[mysqld]      # Server side 
user=mysql      # Manage the user name of the user 
basedir=/application/mysql   # Directory of programs 
datadir=/data/mysql/data       # Data path 
socket=/tmp/mysql.sock        #socket File path 
server_id=6     # The scope is 1-65535
port=3306       # Specify your own port number 
log_error=/data/mysql/data/mysql.log
log_bin=/data/mysql/data/mysql-bin   # A log dedicated to master-slave replication and backup recovery 
[mysql]      # client 
socket=/tmp/mysql.sock

3、 ... and 、 start-up mysql database

sys-v Methods can be started in the following two ways

1、 Start with a configuration file

[[email protected] data]# cd /application/mysql/support-files/
[[email protected] support-files]# ll
 Total usage  24
-rw-r--r--. 1 mysql mysql   773 4 month   13 2019 magic
-rwxr-xr-x. 1 mysql mysql  1061 4 month   13 2019 mysqld_multi.server
-rwxr-xr-x. 1 mysql mysql   894 4 month   13 2019 mysql-log-rotate
-rwxr-xr-x. 1 mysql mysql 10576 4 month   13 2019 mysql.server
[[email protected] support-files]# ./mysql.server start
Starting MySQL.Logging to '/data/mysql/data/mysql.err'.
. SUCCESS!

Don't you think the above method is troublesome ? Every time you start, you need to go to the directory and start with a file , So there is a second way

2、 use service The way to call

[[email protected] init.d]# cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld
[[email protected] init.d]# service mysqld status
 SUCCESS! MySQL running (7680)

7 Of systemd Management method startup
You need to customize a startup script

[[email protected] init.d]# vim /etc/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WanteBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
[[email protected] init.d]# systemctl start mysqld
[[email protected] init.d]# netstat -tupln|grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      8165/mysqld      
 Successful launch 

Before using this method to start, you should first use service The service that started was stopped (service mysqld stop)

Four 、mysql Unable to start error encountered

1、without updating PID Similar mistakes
Check the log : stay /data/mysql/data/ Host name .err ==>[ERROR]
The reasons for the error may be :

1、/etc/my.cnf Configuration error
2、/data/mysql/data Whether the primary group of the directory is mysql

5、 ... and 、 Administrator password setting and retrieval

[[email protected] data]# mysqladmin -uroot -p password 123456

If you have an old password , Want to set a new password , It can be set as follows , Bring your old password

[[email protected] data]# mysqladmin -uroot -p123456 password abcd

After setting the password, you can log in like this

[[email protected] data]# mysql -uroot -p
 For the sake of safety , Enter and press the password , Do not enter the password on the screen 

What if the administrator user password is forgotten ?

1、 Close the database , If you use systemd Starting up , Just use systemctl stop mysql

2、 Start the database to maintenance mode

mysqld_safe --skip-grant-tables --skip-networking &

3、 Log in and change the password
Enter into mysql, Then change the password

mysql> flush privileges;
mysql> alter user [email protected]'localhost' identified by '1'; # Set the password to 1

4、 Close the database , Normal start , Verify with the password just set

B、mysql Architecture and management

One 、mysql Structure and examples

1、mysql c/s Structure is introduced
Two connections :

1、TCP/IP:mysql -uroot -p 123456 -h 10.0.0.1 -P 3306
2、Socket:mysql -uroot -p 123456 -S /tmp/mysql.sock

2、mysql The composition of the instance
amount to company = Boss + The manager + staff + Administrative Area
example =mysqld+master thread + Working thread + Pre allocated memory

Two 、 One sql The process of statement execution

 Insert picture description here

1、 adjoining course

(1) Provide connection protocol
socket
tcp/ip
(2) Verify user name , The legality of the password , Match with a special authorization table
(3) Derive a dedicated connection thread ( receive sql sentence , Return results ), use show processlist; You can see the connection thread

reflection : The user mentioned above has forgotten the password and can use mysqld_safe --skip-grant-tables --skip-networking It's doing tricks on the connection layer , The first parameter is to skip the authorization table , The second parameter is not used tcp/ip agreement , use socket agreement , For the sake of safety

2、SQL layer ( Specialized in the processing of statements , Optimization is critical )

(1) verification sql Grammar and sql_mode(mysql Supported basic syntax and verification rules )
(2) Validation semantics
(3) Verify permissions
(4) The parser parses statements , Generate execution plan ( The parse tree )
(5) Optimizer ( Find the lowest cost execution plan according to the algorithm ) cost (cpu,io,mem)
(6) The executor follows the execution plan selected by the optimizer , perform sql sentence , Get the method of getting data
(7) Provide query cache( The query cache ), Every time I execute sql The statement will have a cache (sql id), Get the cache directly next time you encounter the same statement ( It's not on by default , use redis replace )
(8) Record operation log (binlog), Only record the operation of modifying the class , It's not on by default

3、 Storage engine layer ( A level that really deals with disk )

according to SQL The data retrieval method provided by the layer , Get the data , Return to SQL, Structure into a table , Then the connection layer thread returns it to the user

3、 ... and 、mysql Logical and physical structure of

1、 Logical structure
 Insert picture description here
2、 Physical structure
On the macro :
library , Stored in the directory of the operating system
surface :

user surface (MyISAM):
user.frm ------> Column definition information
user.MYD ------> data row
user.MYI -------> Index information
time_zone surface (InnoDB):
time_zone.frm -----> Column definition information
time_zone.ibd -----> Data rows and indexes
Interview questions : Please explain MyISAM and InnoDB Similarities and differences in storage methods

On the micro :
A watch is a paragraph ,mysql Allocate at least one area when allocating space , Each zone defaults to 1M(64 individual pages),mysql The smallest io Unit is page(16k)

Four 、mysql user 、 Permission and connection management

1、 User definition
user name @’ White list ’
2、 User management
effect : Sign in , Management database
a、 Create user

mysql> create user [email protected]'192.168.0.%' identified by '123456';

b、 Query the user

mysql> select user,host,authentication_string from mysql.user;
+---------------+-------------+-------------------------------------------+
| user          | host        | authentication_string                     |
+---------------+-------------+-------------------------------------------+
| root          | localhost   | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| mysql.session | localhost   | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys     | localhost   | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| qianxi        | 192.168.0.% | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+---------------+-------------+-------------------------------------------+
4 rows in set (0.00 sec)

c、 Delete user

mysql> drop user [email protected]'172.16.1.%';
Query OK, 0 rows affected (0.00 sec)

d、 Modify the user

mysql> alter user q192.168.0.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

3、 Rights management

effect : Control the ability of users to log in to mysql Object to do what commands

Definition :mysql The definition of permission is sql sentence ,all It's all rights , It is owned by ordinary administrators
with grant option: Functions authorized by super administrator to other users

mysql> grant all on *.* to [email protected]'10.0.0.%' identified by '123456' with grant option;

The above code is to generate a super administrator

grant ALL on wordpress.* to [email protected]'10.0.0.%' identified by '123';

Explanation of order :
grant jurisdiction on Range to user identified by ‘ password ’;

Example :
1、 from windows Medium navicat Software use root management mysql database
2、 establish zhihu Business users are able to zhihu The business library performs business

1、grant all on *.* to [email protected]'10.0.0.%' identified by '123';
2、grant select,update,delete,insert on zhihu.* to [email protected]'10.0.0.*' identified by '123';

mysql Permissions in can be inherited , Multiple authorizations are superimposed , So I want to cancel a certain permission , Must be achieved by recycling , Instead of repeatedly authorizing

a、 How to view user permissions

mysql> show grants for [email protected]'192.168.0.%';

b、 Recycling permissions

mysql> revoke delete on zhihu.* from [email protected]'10.0.0.%';
 Recycling zhihu User delete jurisdiction 

Interview questions :
1、 Developers are looking for dba Open user , need dba What to communicate with developers ?

1、 What permissions do you want to do
2、 From what address do you want to log in to the database
3、 Which libraries and tables to operate on

2、 Developers are looking for dba Ask the Administrator root User's password , As dba How do you handle it? ?
4、mysql Connection management
Native Client Tools

mysql Parameters
-u user name
-p password
-h The address of the database
-S Followed by socket file , use socket How to connect
-P Pick up 3306 port
-e Non interactive execution Mysql The order in

If you use socket Connection rule show processlist Just look root user Connect , If it is tcp Remote connection , be show processlist Just look Hostname user Connect , If both are connected , with tcp Connection priority

5、 ... and 、 Multi instance configuration

 Insert picture description here
1、 Create directory

[[email protected] mysql]# mkdir -p /data/330{
    7,8,9}/data

2、 Prepare the configuration file

[[email protected] mysql]# vim /data/3307/my.cnf
[mysqld]
basedir=/application/mysql
datadir=/data/3307/data
socket=/data/3307/mysql.sock
server_id=7
port=3307
log_error=/data/3307/mysql.log
log_bin=/data/3307/mysql-bin
 The other two 3308 and 3309 The configuration file value of needs to be modified 

3、 initialization 3 Set of data

[[email protected] ~]# mv /etc/my.cnf{
    ,.bak}
[[email protected] ~]# mysqld --initialize-insecure --user=mysql --datadir=/data/3307/data/ --basedir=/application/mysql/
[[email protected] ~]# mysqld --initialize-insecure --user=mysql --datadir=/data/3308/data/ --basedir=/application/mysql/
[[email protected] ~]# mysqld --initialize-insecure --user=mysql --datadir=/data/3309/data/ --basedir=/application/mysql/

4、systemd Manage multiple instances

[[email protected] ~]# cd /etc/systemd/system
[[email protected] system]# cp mysqld.service mysqld3307.service
[[email protected] system]# cp mysqld.service mysqld3308.service
[[email protected] system]# cp mysqld.service mysqld3309.service

[[email protected] system]# vim mysqld3307.service
ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf
[[email protected] system]# vim mysqld3308.service
ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf
[[email protected] system]# vim mysqld3309.service
ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf

5、 to grant authorization

[[email protected] system]# chown -R mysql:mysql /data/* 

6、 start-up

[[email protected] system]# systemctl start mysqld3307.service
[[email protected] system]# systemctl start mysqld3308.service
[[email protected] system]# systemctl start mysqld3309.service

7、 Validate multiple instances

[[email protected] system]# netstat -tupln|grep 330
tcp6       0      0 :::3306                 :::*                    LISTEN      7598/mysq
tcp6       0      0 :::3307                 :::*                    LISTEN      7902/mysq
tcp6       0      0 :::3308                 :::*                    LISTEN      7936/mysq
tcp6       0      0 :::3309                 :::*                    LISTEN      7983/mysq
[[email protected] system]# mysql -S /data/3307/mysql.sock -e "select @@server_id"
+-------------+
| @@server_id |
+-------------+
|           7 |
+-------------+
原网站

版权声明
本文为[I love Qianxi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280515160474.html