Installing mysql5.7 on Centos7.x

Note: This article mainly describes how to install mysql5.7.2 on CentOS 7. It is best to set SELINUX=disabled in the system / etc/selinux/config file before installation

Compressed version installation

1 view operating system version

cat /etc/redhat-release

2 environmental preparation before installation

Check whether mysql has been installed
First, check whether MySQL is installed in the Linux operating system:

rpm -qa | grep -i mysql


Uninstall installation package
If any information appears, delete it. The command is as follows:

rpm -e --nodeps Package name

Delete the development header files and libraries of the old version of mysql

rpm -e --nodeps Package name

Note: after uninstallation, the data in / var/lib/mysql and / etc/my.cnf will not be deleted. If it is determined to be useless, delete it manually:

rm -f /etc/my.cnf
rm -fr /var/lib/mysql

After the deletion is completed, the installation can be carried out, otherwise the installation will make an error.

Also check the old mysql installation directory

whereis mysql

Delete old installation directory

rm -rf catalogue

3. Create a new soft directory (soft is a directory specially created by myself for storing uploaded tar compressed packages)

cd ~
mkdir soft
cd soft/

4 installation process

4.1 installation package preparation
Before installation, prepare the compressed installation package or download it from the mysq official website, and upload the installation package to the root/soft / directory

Provide mysql installation package network disk resources
Link: https://pan.baidu.com/s/1riVvgzoGe9RXI2Qea4Y4eQ
Extraction code: yyds

4.2 unzip to the specified directory
tar -zxvf installation package - C /usr/local

tar -zxvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz  -C /usr/local/


The directory name is too long. Rename it to mysql

mv mysql-5.7.20-linux-glibc2.12-x86_64 mysql


Create a new directory data under the mysql directory

4.3 view mysql users and groups. If they exist, they need to be deleted
Check whether the mysql user or group exists

id mysql


To delete users and groups:

groupdel mysql
userdel -f mysql

If not, go to vi /etc/grpup to find mysql and add#
As follows, there are no users

4.4 add mysql user groups and users, and associate mysql users with mysql groups
Create mysql group

groupadd mysql

Check whether the creation is successful

cat /etc/group | grep mysql


Create mysql user

useradd -r -g mysql mysql 

Check whether the creation is successful

cat /etc/passwd | grep mysql


Set password for mysql user: mengxuegu1234

passwd mysql


4.5 authorize mysql to read, write and execute necessary files

chown -R mysql:mysql /usr/local/mysql
chmod -R 775 /usr/local/mysql


4.6 enter the contents of MySQL / support files folder
Check whether there is a my-default.cnf configuration file. If there is no default configuration file, you need to manually create a my-default.cnf configuration file

4.7 the content of my-default.cnf configuration file can be directly copied as follows

vi my-default.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
#mysql installation directory
basedir = /usr/local/mysql
#data in mysql installation directory
datadir = /usr/local/mysql/data
bind-address = 0.0.0.0
port = 3306
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 128M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 2M
myisam_sort_buffer_size = 8M
thread_cache_size = 8
query_cache_size= 16M
max_connections = 213
wait_timeout = 31536000
interactive_timeout = 30
max_connect_errors = 9
long_query_time = 1
tmp_table_size = 16M
#log-bin=mysql-bin
#binlog_format=mixed
#server-id = 1
lower_case_table_names = 1
[mysqldump]
quick
max_allowed_packet = 8M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 12M
sort_buffer_size = 1M
read_buffer = 1M
write_buffer = 1M
[mysqlhotcopy]
interactive-timeout

4.8 reauthorization

chown -R mysql:mysql /usr/local/mysql
chmod -R 775 /usr/local/mysql

4.9 copy my-default.cnf and mysql.server under the support files file as follows

cp my-default.cnf /etc/my.cnf
cp mysql.server /etc/init.d/mysqld

4.10 edit mysqld and modify it to the specified path of mysql

vi /etc/init.d/mysqld

Enter the editing page, press i to edit and modify, press ctrl+c to stop the modification, and then: wq save the modification


4.11 add the mysql path to the environment variable to facilitate the operation of commands

vi /etc/profile

Add at the end of file

export MYSQL=/usr/local/mysql
export PATH=$PATH:$MYSQL/bin:

4.12 make the modified profile file effective

source /etc/profile

4.13 perform database initialization
The execution file is MySQL in the bin folder under mysql_install_db
basedir is the installation directory and datadir is the data file directory.
Note: mysql_install_db is located in the scripts folder in version 5.6, which has been cancelled in 5.7 and merged into bin:

cd /usr/local/mysql/bin
./mysqld --basedir=/usr/local/mysql --user=mysql --datadir=/usr/local/mysql/data --initialize


The above installation is successful

If error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory is reported, libaio needs to be installed

yum install libaio

If [error] - initialize specified but the data directory has files in it. Aborting

Note: it has been executed once. The '/ usr/local/mysql/data directory needs to be deleted, re created and re authorized

[root@centOS mysql]# cd /usr/local/mysql
[root@centOS mysql]# rm -rf data
[root@centOS mysql]# mkdir data
[root@centOS mysql]# chown -R mysql:mysql /usr/local/mysql
[root@centOS mysql]# chmod -R 775 /usr/local/mysql
[root@centOS mysql]#

Then execute:

cd /usr/local/mysql/bin
 ./mysqld --basedir=/usr/local/mysql --user=mysql --datadir=/usr/local/mysql/data --initialize

4.14 after completion, start the mysql service

service mysqld start


At this time, the service can be started, but the whole project is only half completed.
Note: the new version may report an error because mysqld_ Because of safe, there is no error when installing the old version. You just need to put mysqld_ Modify the default path in safe to your own target path, and then execute it.

5. Change password

5.1 shut down the service first

service mysqld stop


5.2 MySQL / bin / mysqld_ Replace usr/local/mysql in safe with the specified installation path name, and then execute the following code.

mysqld_safe --user=mysql --skip-grant-tables --skip-networking &

At this time, it has entered the filter free state and started to modify the root password. Note: the password field in the user table of version 5.6 is password, which is changed to authentication after 5.7_ String, no more password.
The password set above is the root password for us to log in to mysql

[root@localhost bin]# mysql
mysql> use mysql;
mysql> update user set authentication_string=PASSWORD('root') where user='root';
mysql> flush privileges;
mysql> exit;

At this point, the password has been modified. Log out and log in again. I thought it was done, but an error was reported when creating the database. The error is: ERROR 1820 (HY000): You must reset your password using ALTER USER statement. The original first login, but also to set the password again, after modification, the creation is successful.

mysql -u root -p
mysql> alter user 'root'@'localhost' identified by 'root';
mysql> flush privileges;
mysql> exit;

Set remote login permissions

If you log in through a remote connection, an error will be reported
As follows:

1. Determine the mysql port number,

mysql> show global variables like 'port';


0 is 3306 by default

If it is not 3306, you can modify the mysql port number

1.# vi /etc/my.cnf
2.modify port=3306
3.restart # service mysqld restart

2. Modify the remote connection permission,
Very simple, you only need to modify the host of the root user in the user table. After the change, remember to restart the service and connect remotely again

# mysql -uroot -p
mysql> use mysql;
mysql> update user set host='%' where user='root';
mysql> exit;
# service mysqld restart


3 turn off the firewall,
If 10060 unknown error is not reported, close it

The default firewall of CentOS 7. X is firewall instead of iptables
Check the firewall status (not running is displayed when it is turned off and running is displayed when it is turned on)

firewall-cmd --state

Stop firewall

systemctl stop firewalld.service

Disable firewall startup

systemctl disable firewalld.service

The default firewall of CentOS6 is iptables
1. The firewall takes effect immediately and recovers after restart:
close

service iptables stop

open

service iptables start

2. The firewall takes effect permanently and will not be restored after restart
close:

chkconfig iptables off

On:

chkconfig iptables on

Tags: Linux Database MySQL

Posted on Mon, 25 Oct 2021 00:24:31 -0400 by kilby