Install MySQL offline on Centos

1. CentOS7.4 system comes with mariadb

# View the Mariadb that comes with the system
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64
# Uninstall Mariadb that comes with the system
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64
# Delete my.cnf in etc directory
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# rm /etc/my.cnf

2. Check whether mysql exists

# Check if mysql exists
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# rpm -qa | grep mysql
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]#

3. Check whether users and groups exist

1) Check whether the mysql combined user exists
# Check whether the mysql group and user exist, if not, create them
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# cat /etc/group | grep mysql
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# cat /etc/passwd | grep mysql
# Query all users (just record, no need to execute)
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# cat /etc/passwd|grep -v nologin|grep -v halt|grep -v shutdown|awk -F ":" '{print $1 "|" $3 "1 " $4}' | more
root|010
sync|510
mysql|99711001
2) If it does not exist, create the mysql group and user
# Create mysql user group
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# groupadd mysql
#Create a user named mysql and join the mysql user group
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# useradd -g mysql mysql
# Set password as 111111
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# passwd mysql
Changing password for user mysql.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.

4. Download the mysql tar package

https://dev.mysql.com/downloads/mysql/5.7.html#downloads

5. Upload the mysql TAR package downloaded in step 4 to the server

# Enter the /usr/local/src folder
[root@iZ2ze3hm3gyjyjz628l7rgZ ~]# cd /usr/local/
# Unzip mysql-5.7.22-el7-x86_64.tar.gz
[root@iZ2ze3hm3gyjyjz628l7rgZ src]# tar -zxvf mysql-5.7.22-el7-x86_64.tar.gz
# Move the decompressed files to the /usr/local folder
[root@iZ2ze3hm3gyjyjz628l7rgZ src]# mv mysql-5.7.22-el7-x86_64 /usr/local
# Enter /usr/local and change it to mysql
[root@iZ2ze3hm3gyjyjz628l7rgZ src]# cd /usr/local
[root@iZ2ze3hm3gyjyjz628l7rgZ local]# mv mysql-5.7.22-el7-x86_64 mysql

6. Change the groups and users to which they belong

# Change the groups and users they belong to
[root@iZ2ze3hm3gyjyjz628l7rgZ local]# chown -R mysql mysql/
[root@iZ2ze3hm3gyjyjz628l7rgZ local]# chgrp -R mysql mysql/
[root@iZ2ze3hm3gyjyjz628l7rgZ local]# cd mysql/
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# mkdir data
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chown -R mysql:mysql data

7. Create the my.cnf file under /etc

# Enter the /etc folder
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# cd /etc
#Create my.cnf file
[root@iZ2ze3hm3gyjyjz628l7rgZ etc]# touch my.cnf
# Edit my.cnf
[root@iZ2ze3hm3gyjyjz628l7rgZ etc]# vim my.cnf
1) Add the following content to my.cnf:
[mysql]
#Set the default character set of mysql client
default-character-set=utf8

[mysqld]
# Set port 3306
port=3306
#Set the installation directory of mysql
basedir=/usr/local/mysql
#Set the storage directory for the data of the mysql database
datadir=/usr/local/mysql/data
#Maximum number of connections allowed
max_connections=200
# The character set used by the server defaults to the 8-bit encoded latin1 character set
character-set-server=utf8
# The default storage engine that will be used when creating new tables
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
2) View the content of my.cnf
# View my.cnf file
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# cat /etc/my.cnf

8. Enter the mysql folder and install mysql

# Enter mysql
[root@iZ2ze3hm3gyjyjz628l7rgZ etc]# cd /usr/local/mysql/
# Install mysql
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2018-07-04 15:46:02 [WARNING] 5mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2018-07-04 15:46:05 [WARNING] The bootstrap log isn't empty:
2018-07-04 15:46:05 [WARNING] 2018-07-04T15:46:02.728710Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
2018-07-01T15:46:02.729161Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2018-07-04 T15:46:02.729167Z 0 [Warning] Changed limits: table_open_cache: 407 (requested 2000)
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chown 777 /etc/my.cnf
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chmod + x /etc/init.d/mysqld

9. Start mysql

# Start mysql
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# /etc/init.d/mysqld restart
Shutting down MySQL..
Starting MySQL.

10. Set up startup

#Set up startup
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chkconfig --level 35 mysqld on
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chkconfig --list mysqld

[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chmod + x /etc/rc.d/init.d/mysqld
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chkconfig --add mysqld
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# chkconfig --list mysqld
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# service mysqld status
 SUCCESS! MySQL running (4475)

11. Modify the configuration file

1) Modify /etc/profile and add the following content at the end
# Modify the /etc/profile file
#set mysql environment
export PATH=$PATH:/usr/local/mysql/bin
# Make the file effective
[root@iZ2ze3hm3gyjyjz628l7rgZ mysql]# source /etc/profile

12. Obtain mysql initial password

# 1. Obtain mysql initial password
[root@iZ2ze3hm3gyjyjz628l7rgZ bin]# cat /root/.mysql_secret
# Password set for user 'root@localhost' at 2017-04-17 17:40:02
_pB*3VZl5T<6
# 2. Change password
[root@iZ2ze3hm3gyjyjz628l7rgZ bin]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 53
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set PASSWORD = PASSWORD('root');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

13. Add remote access permissions

# Add remote access permissions
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set host='%' where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0

mysql> select host,user from user;
 + ----------- + --------------- +
| host | user |
 + ----------- + --------------- +
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
 + ----------- + --------------- +
3 rows in set (0.00 sec)

14. Restart mysql to take effect

# Restart mysql
[root@iZ2ze3hm3gyjyjz628l7rgZ bin]# /etc/init.d/mysqld restart
Shutting down MySQL..
Starting MySQL.

Go to: https://www.jianshu.com/p/1c6641316f41