centos 7 compiles and installs mysql and adds mysql to system services

preface

LNMP environment construction (I): Mysql
MySQL will be installed with a MySQL function library, which will be used when installing PHP. After PHP is successfully installed, a PHP FPM process will be generated to provide fastcgi service. After Apache or Nginx is installed, relevant settings need to be made if PHP is to be executed.
MySQL is incompatible with MariaDB. If you attempt to install on any server without removing MariaDB, the installation terminates with an error message pointing to uninstalling MariaDB.

1, Compile and install

1. Brief description of compilation and installation

Commands such as configure / make / makeinstall are used to compile and install software in linux,
These are typical installation steps of programs generated by autoconf and automake of GNU
make is used to compile. It reads instructions from Makefile and then compiles
make install is used for installation. It also reads instructions from Makefile and installs to the specified location

2.Yum (full name: Yellow dog Updater, Modified)

It is a Shell front-end package manager in Fedora, RedHat and CentOS. Based on RPM package management, it can automatically download and install RPM packages from the specified server, automatically deal with dependencies, and install all dependent software packages at one time without tedious downloading and installation.

List the components and install the developer tool components

yum group list
yum group install "Development Tools"

Before installing mysql, you need to install dependent packages to avoid problems during installation

yum -y install gcc gcc-c++ cmake ncurses-devel autoconf bison perl perl-devel

2, Compilation and installation steps

1. Create mysql installation directory and create users and user groups

mkdir create folder
-m: set access permissions for the new directory
-p: at this time, if some directories in the path do not exist, the system will automatically create those directories that do not exist

mkdir /usr/local/mysql
mkdir /usr/local/mysql/data

The useradd command is used to establish a user account and create a user's starting directory. The permission of this command is the end user. New user password is empty
- g: specify the starting group to which the user belongs.
- d: specify the start directory when the user logs in.
- s: Specifies the shell used by the user after logging in- s /sbin/nologin is a shell that does not allow login
-After g, the first mysql is the group name and the second mysql is the new user name. The new user information can be found in the / etc/passwd file

groupadd mysql
useradd -r -g mysql mysql

2. Download boost

1. Create a folder named boost under / usr/local

 	mkdir -p /usr/local/boost 

2. Enter the newly created folder and download boost

 wget http://www.sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz 

3. Decompression

 tar -xvzf boost_1_59_0.tar.gz 

3. Download the source package and start the installation

Source package address: https://github.com/mysql/mysql-server/archive/mysql-5.7.17.tar.gz

cd/usr/local/src
wget https://github.com/mysql/mysql-server/archive/mysql-5.7.17.tar.gz

1. Unzip the file to the current folder

tar backup, compression and decompression, Linux command, is also a tool
-z: it means that the tar package has been compressed by gzip, so you need to unzip it with gunzip
-x: extract the files from the tar package
-v: display details
-f xxx.tar.gz: Specifies that the file to be processed is xxx.tar.gz
Decompress tar.gz with tar zxvf and tar.bz2 with tar jxvf

tar -zxvf mysql-5.7.17.tar.gz
cd mysql-5.7.17 //Enter the extracted directory

cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=/usr/local/boost \
-DWITH_SYSTEMD=1 

make && make install

CMAKE Parameter Description: Note: there must be no space after the \ parameter
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql / / installation path
-DMYSQL_DATADIR=/usr/local/mysql/data / / data file storage location
-DSYSCONFDIR=/etc //my.cnf path
-DWITH_MYISAM_STORAGE_ENGINE=1 / / MyIASM engine is supported
-DWITH_INNOBASE_STORAGE_ENGINE=1 / / supports InnoDB engine
-DWITH_MEMORY_STORAGE_ENGINE=1 / / Memory engine is supported
-DWITH_READLINE=1 / / shortcut function (I haven't used it)
-DMYSQL_ UNIX_ Addr = / usr / local / MySQL / MySQL. Socket / / the socket path connecting to the database
-DMYSQL_TCP_PORT=3306 / / port
-DENABLED_LOCAL_INFILE=1 / / allow importing data locally
-DWITH_PARTITION_STORAGE_ENGINE=1 / / database partition is supported during installation
-DEXTRA_CHARSETS=all / / install all character sets
-DDEFAULT_CHARSET=utf8 / / default character
-DDEFAULT_COLLATION=utf8_general_ci / / set default proofing rules

matters needing attention:
If the installation fails, you need to clear the old object files and cache information when recompiling.

make clean
rm -f CMakeCache.txt
rm -rf /etc/my.cnf

2. Set directory permissions

The chown command changes the owner and group of a file or directory.
-R: recursively change the owner of the specified directory and all subdirectories and files under it.
-v: displays the work done by the chown command.

cd /usr/local/mysql
chown -R mysql:mysql .
chown -R mysql:mysql data

3. Add the mysql startup service to the system service

cp support-files/my-default.cnf /etc/my.cnf

3, Use dependent installation

After MySQL was acquired by Oracle, the default database provided in the image warehouse of CentOS became MariaDB

The libraries and included files used in MySQL devel development. If you do not do C development, you can not install it. This is true for any - devel package
mysql client + server
mysql client client, which provides "mysql" command line program.
MySQL server database server

1. Download the mysql source installation package and install the mysql source

wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
// Check whether the yum source is successfully installed
sudo yum repolist enabled | grep "mysql.*-community.*"
# mysql-connectors-community/x86_64        MySQL Connectors Community           74
# mysql-tools-community/x86_64             MySQL Tools Community                74
# mysql56-community/x86_64                 MySQL 5.6 Community Server          429

2. Install Mysql

yum install mysql-server

3. Disable password policy and specify password verification policy

vi /etc/my.cnf

# Specify password verification policy: 0=LOW, 1=MEDIUM, 2=STRONG
validate_password_policy=0
# Disable password policy
validate_password = off

# restart
sudo systemctl restart mysqld
# Change password
set password = password('123456');

4. Install mysql, set automatic startup and start service

systemctl enable mysqld.service
systemctl start mysqld.service
# install
mysql_secure_installation
# Modify code
vi /etc/my.cnf
[client]
default-character-set = utf8
[mysqld]
default-storage-engine = INNODB
character-set-server = utf8
collation-server = utf8_general_ci

5. Start Mysql

The following two commands work the same

systemctl [stop|start|restart] service name
Service service name [stop|start|restart]

sudo systemctl restart mysqld
#Set new password
mysqladmin -u root password

6. The ability to add remote connections for root

#Enter mysql
mysql -u root -p
 
mysql>use mysql;
mysql>desc user;
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root"; //The ability to add remote connections to root.
mysql>update user set Password = password('xxxxxx') where User='root';
mysql>select Host,User,Password from user where User='root'; 
mysql>flush privileges;  //Refresh permissions
mysql>exit  //sign out

#Here are the remaining four GRANT examples
#User user1 from 192.168.155.1 is assigned the permission to SELECT,INSERT,UPDATE,DELETE,CREATE,DROP and other operations on the tablename table of database dbname, and the password is set to 123456.

#There are many permissions for table operations, such as ALTER
mysql>GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON dbname.tablename TO 'user1'@'192.168.155.1' IDENTIFIED BY '123456';

#User user2 from 192.168.155.1 is assigned the permission to perform all operations on all tables of database dbname, and the password is set to 123456.
mysql>GRANT ALL PRIVILEGES ON dbname.* TO 'user2'@'192.168.155.1' IDENTIFIED BY '123456';

#User user3 from 192.168.155.1 is assigned the permission to perform all operations on all tables of all databases, and the password is set to 123456.
mysql>GRANT ALL PRIVILEGES ON *.* TO 'user3'@'192.168.155.1' IDENTIFIED BY '123456';

#Assign the local user user4 the permission to perform all operations on all tables of all databases, and set the password to 123456.
mysql>GRANT ALL PRIVILEGES ON *.* TO 'user4'@'localhost' IDENTIFIED BY '123456';

7. Open the external access of mysql3306 port of firewall

After CentOS was upgraded to 7, firewalld was used instead of iptables. The following is a record of how to use firewalld to open the Linux port
– zone: the scope and network area define the trust level of the network connection. This is a one to many relationship, which means that a connection can only be part of a region, and a region can be used for many connections
– add port: add port and communication protocol. The format is port / communication protocol. The protocol is tcp or udp
– permanent: it takes effect permanently. Without this parameter, port access fails after system restart

firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload

summary

Unfinished

Tags: Linux CentOS Back-end

Posted by komquat on Tue, 12 Oct 2021 09:17:30 +0530