Installing and configuring MySQL for Linux(centos7) (tar. GZ package)

Posted by sarun on Fri, 03 Dec 2021 08:22:13 +0100

Baidu cloud disk: link: https://pan.baidu.com/s/1PyR_UiaKZetXlAuJ7KwYUw  
Extraction code: 0601

Package preparation  

-rw-r--r-- 1 root root 729891578 12 February 18:25 mysql-5.7.34-el7-x86_64.tar.gz

Let's first check whether mariadb (centos) is installed on the system. If so, uninstall it

[root@master module]# rpm -qa |grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@master local]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64

Unzip the software package to the specified location and rename it. Here I am / opt/module (the module is newly created for myself), but generally unzip it to the / usr/local directory. Readers can choose by themselves

#decompression
[root@master software]# tar -zxvf mysql-5.7.34-el7-x86_64.tar.gz -C /opt/module/
#rename
[root@master software]# mv /opt/module/mysql-5.7.34-el7-x86_64/ /opt/module/mysql

Then check whether there are mysql users and mysql user groups. If not, create and group mysql users and user groups

[root@master software]# cat /etc/group |grep mysql    #Check whether the mysql user group exists
[root@master software]# cat /etc/passwd |grep mysql   #Check whether there are mysql users

[root@master software]# groupadd mysql    #Create a new mysql user group
[root@master software]# useradd -r -g mysql -d /opt/module/mysql mysql   #Create a new mysql user and put it into the mysql group and set its home directory

  Copy the environment variable configuration file to the mysql home directory

[root@master mysql]# cp /etc/skel/.* /opt/module/mysql

Assign mysql files to mysql users and mysql user groups

[root@master module]# chown -R mysql:mysql /opt/module/mysql/
[root@master module]# ll
 Total consumption 12
-rw-r--r--  1 root  root  7448 10 December 15:20 dump.rdb
drwxr-xr-x 11 root  root   173 9 August 28-21:32 hadoop-3.1.3
drwxr-xr-x  8 root  root   273 6 September 21:58 jdk1.8.0_301
drwxr-xr-x  9 mysql mysql #Note that the previous 129 December 2 18:33 mysql
drwxrwxr-x  6 root  root   309 11 June 22, 2018 redis-5.0.2
drwxr-xr-x 11 root  root  4096 9 August 28-21:11 zookeeper-3.4.6

Create a data directory to store data files and authorize

[root@master module]# mkdir /opt/module/mysql/data
[root@master mysql]# chown -R mysql:mysql /opt/module/mysql/data/
[root@master module]# chmod 777 /opt/module/mysql/

Create or modify a mysql configuration file as follows

[mysql]
#Modify default character set
default-character-set=utf8

[mysqld]
basedir=/opt/module/mysql
datadir=/opt/module/mysql/data
port = 3306
#ignore case
lower_case_table_names=1
#Server default character set
character-set-server=utf8
#maximum connection
max_connections=2000
#Default data engine when creating database
default-storage-engine=INNODB
explicit_defaults_for_timestamp=true

Modify the permissions of my.cnf file and install libaio library

[root@master etc]# chmod 640 /etc/my.cnf 

[root@master etc]# yum -y install libaio

Configure the environment variables so that the mysql command can be used in the global environment. Modify the / etc/profile file or create a new my in the / etc/profile.d / directory_ Env.sh file (create my_env.sh file according to personal habits)

[root@master etc]# vim /etc/profile.d/my_env.sh 

Add the following

#MYSQL_HOME
export MYSQL_HOME=/opt/module/mysql
export PATH=$PATH:$MYSQL_HOME/bin

Copy the mysql related configuration to the system service directory and grant the execution permission

[root@master init.d]# cp /opt/module/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@master init.d]# chmod +x /etc/init.d/mysqld 

Set mysql to start automatically

chkconfig --add mysqld
chkconfig --list #3, 4 and 5 display on / on, if off / off

#Enter the following command
chkconfig --level 345 mysqld on

First, make the environment variable we configured take effect, switch to mysql user, and then initialize the database

#Make environment variables effective
[root@master init.d]# source /etc/profile
#Initialize database
[root@master mysql]# mysqld --initialize  --user=mysql --basedir=/opt/module/mysql/ --datadir=/opt/module/mysql/data/
2021-12-02T12:13:36.343671Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-12-02T12:13:36.379941Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-12-02T12:13:36.442682Z 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: 46b2a3dd-5369-11ec-a99b-000c299eacfe.
2021-12-02T12:13:36.443806Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-12-02T12:13:36.866598Z 0 [Warning] CA certificate ca.pem is self signed.
2021-12-02T12:13:37.165110Z 1 [Note] A temporary password is generated for root@localhost: upLu19y!CbKk #Database initial password

Start the database and view the startup status

[root@master mysql]# systemctl start mysqld
[root@master mysql]# systemctl status mysqld
● mysqld.service - LSB: start and stop MySQL
   Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
   Active: active (running) since IV 2021-12-02 20:28:09 CST; 7s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 10872 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
    Tasks: 28
   Memory: 182.7M
   CGroup: /system.slice/mysqld.service
           ├─10887 /bin/sh /opt/module/mysql/bin/mysqld_safe --datadir=/opt/module/mysql/data --pid-file=/opt/module/mysql...
           └─11074 /opt/module/mysql/bin/mysqld --basedir=/opt/module/mysql --datadir=/opt/module/mysql/data --plugin-dir=...

12 February 20:28:08 master systemd[1]: Starting LSB: start and stop MySQL...
12 February 20:28:08 master mysqld[10872]: Starting MySQL.Logging to '/opt/module/mysql/data/master.err'.
12 February 20:28:09 master mysqld[10872]: SUCCESS!
12 February 20:28:09 master systemd[1]: Started LSB: start and stop MySQL.

Log in to the mysql client, modify the password, and set allow remote connection

#Login database client
[root@master mysql]# mysql -u root -p
Enter password: #Password generated during initialization: upLu19y!CbKk (everyone is different)

#Modify database password
mysql> alter user root@localhost identified by '123456';
Query OK, 0 rows affected (0.00 sec)

#Set allow remote login
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' ;
Query OK, 0 rows affected, 1 warning (0.00 sec)
#Refresh for configuration to take effect
mysql> flush privileges;    
Query OK, 0 rows affected (0.00 sec)
#sign out
mysql> quit;
Bye

This installation method is mainly used in the company's production environment to optimize the database according to the needs.

If only ordinary users learn to use it, you can choose to install it in RPM or yum. It is relatively simple. The following is how to install mysql in rpm

mysql(rpm package) installation and configuration under Linux(centos7)_ GLGN blog - CSDN blog software package preparation Baidu online disk: link: https://pan.baidu.com/s/1h7kyhiBwlPKyRaTJtmop3Q Extraction code: 0601 alicloud link: https://www.aliyundrive.com/s/3BqiZmsfUJX[root@zabbix software]# ll total usage 206280-rw-r -- R --. 1 root 26597364 November 30 20:31 mysql-community-client-5.7.34-1.el7.x86_64.rp..https://blog.csdn.net/qq_57193542/article/details/121642197

Topics: Linux MySQL