Install MySQL on linux (tar)

Posted by sharal on Sat, 19 Feb 2022 20:35:32 +0100

preface

MySQL is generally installed in three ways: binary version, compiled version and RPM package. The more common is the binary version installation, which is convenient and simple. Compared with the compilation and installation, if it does not pursue the ultimate performance, it is not very different in use. This tutorial takes the binary version as an example, and the system is CentOS 6 8. MySQL version is 5.7.20.

Download installation package

First go to the official website to download the binary installation package and enter the official website download page mysql download page , click to download the binary 64 bit version of 5.7.20

After downloading, move the installation package to the target Linux

Environmental preparation

1 installation dependency

yum install libaio numactl -y

2 create users and user groups

groupadd mysql
useradd -m -r -g mysql mysql

3. Configure ulimit

We usually use mysql users to start the database. If the number of file openings and processes are not set, there will be problems later

Open / etc / security / limits Conf file, write at the end of the file

mysql     soft    nproc    65536
mysql     hard    nproc    65536
mysql     soft    nofile   65536
mysql     hard    nofile   65536

Open / etc / security / limits d/90-nproc. Conf adjust the parameter to

*          soft    nproc     65536
root       soft    nproc     unlimited
*          soft    nofile    65536
root       soft    nofile    unlimited

4 add native parsing to the hosts file

If you don't set hosts native parsing, some strange problems will occur occasionally, so it's better to add it

vim /etc/hosts

Add: host on the last side_ ip host_ name

5 start time synchronization

Time synchronization is very important for the database. Please ensure that the time of all database nodes is consistent

yum install -y ntp
service ntpd start

6 turn off iptable

The database is generally not allowed to access the Internet. If you are not familiar with the firewall, you can turn it off; If the network environment is not secure enough, you can turn it on

service iptables stop
chkconfig iptables off

7 close selinux

selinux may be unable to write data. If you are not familiar with it, it is recommended to turn it off

View selinux status

getenforce

Temporarily Closed

setenforce 0

Permanent closure

Open / etc/sysconfig/selinux

Change SELinux = forcing to SELINUX=disabled

install

The data directory of this installation is / data/mysql_db /, log directory is / data/mysql_log /, started by mysql

1 extract the file

Extract the binary files and put them in the / user/local / directory

tar -xvzf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.20-linux-glibc2.12-x86_64 /usr/local/mysql57

If you want to access the MySQL client globally, you can add / usr/local/mysql57/bin to the system path. I will directly create a soft connection here

ln -s  /usr/local/mysql57/bin/mysql /usr/bin/mysql
ln -s  /usr/local/mysql57/bin/mysqlbinlog /usr/bin/mysqlbinlog

2 create data and log directories

mkdir -p /data/mysql_db/mysql_test
mkdir -p /data/mysql_log/mysql_test

3 initialization directory

/usr/local/mysql57/bin/mysqld --initialize-insecure --basedir=/usr/local/mysql57 --datadir=/data/mysql_db/mysql_test --user=mysql
/usr/local/mysql57/bin/mysql_ssl_rsa_setup --basedir=/usr/local/mysql57 --datadir=/data/mysql_db/mysql_test

Normal output

[root@mysql-test ~]# /usr/local/mysql57/bin/mysqld --initialize-insecure --basedir=/usr/local/mysql57 --datadir=/data/mysql_db/mysql_test --user=mysql
2018-06-03T05:19:31.469841Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-06-03T05:19:32.525091Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-06-03T05:19:32.751099Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-06-03T05:19:32.861647Z 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: b2d883be-66ed-11e8-bd72-90b11c1a653a.
2018-06-03T05:19:32.877177Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-06-03T05:19:32.877705Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
[root@mysql-test ~]# /usr/local/mysql57/bin/mysql_ssl_rsa_setup --basedir=/usr/local/mysql57 --datadir=/data/mysql_db/mysql_test
Generating a 2048 bit RSA private key
............................................................................+++
....+++
writing new private key to 'ca-key.pem'
-----
Generating a 2048 bit RSA private key
......................................+++
...........................+++
writing new private key to 'server-key.pem'
-----
Generating a 2048 bit RSA private key
..............................................................................+++
..................................................+++
writing new private key to 'client-key.pem'
-----

4 generate configuration file

There may be multiple MySQL instances on one machine. We can put my The CNF file is placed in the data directory of the corresponding instance. Since MySQL will be affected by the default configuration file, we need to put / etc / my Remove CNF and create a new my CNF file

rm -f /etc/my.cnf
vim /data/mysql_db/mysql_test/my.cnf

This installation refers to the configuration, and the specific parameters are adjusted in combination with the situation of the server

# my.cnf
[client]
port = 3306
default-character-set =utf8
socket = /data/mysql_db/mysql_test/mysql.sock

[mysqld]
datadir = /data/mysql_db/mysql_test
basedir = /usr/local/mysql57
tmpdir = /tmp
socket = /data/mysql_db/mysql_test/mysql.sock
pid-file = /data/mysql_db/mysql_test/mysql.pid
skip-external-locking = 1
skip-name-resolve = 1
port = 3306
server_id = 1

default-storage-engine = InnoDB
character-set-server = utf8
default_password_lifetime=0

#### log ####
binlog_cache_size = 16M
log_bin = /data/mysql_log/mysql_test/mysql-bin
log_bin_index = /data/mysql_log/mysql_test/mysql-bin.index
binlog_format = row
expire_logs_days = 15

relay_log_recovery=ON
relay_log=/data/mysql_log/mysql_test/mysql-relay-bin
relay_log_index=/data/mysql_log/mysql_test/mysql-relay-bin.index

log_error = /data/mysql_log/mysql_test/mysql-error.log
log_queries_not_using_indexes = /data/mysql_log/mysql_test/nouseindex.log

slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql_log/mysql_test/mysql-slow.log

#### innodb ####
innodb_buffer_pool_size = 4G
innodb_buffer_pool_instances = 8
innodb_log_group_home_dir = /data/mysql_log/mysql_test/
innodb_undo_directory = /data/mysql_log/mysql_test/
innodb_undo_logs = 128
innodb_flush_neighbors = 1
innodb_log_file_size =  1G
innodb_file_per_table = on 

bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 64M
myisam_max_sort_file_size = 1G
myisam_repair_threads = 1

log_timestamps=system

[mysqldump]
quick
max_allowed_packet = 64M

[myisamchk]
key_buffer_size = 32M
sort_buffer_size = 32M
read_buffer = 16M
write_buffer = 16M

5 generate startup file

cp /usr/local/mysql57/support-files/mysql.server /etc/init.d/mysqld
vim /etc/init.d/mysqld

hold

basedir=
datadir=
lock_file_path="$lockdir/mysql"
$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &

Instead, manually specify the configuration file and data directory

basedir=/usr/local/mysql57
datadir=/data/mysql_db/mysql_test
default_file=/data/mysql_db/mysql_test/my.cnf
lock_file_path="$lockdir/mysqld"
$bindir/mysqld_safe  --defaults-file="$default_file" --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>&1 &

Change the owner of the data directory to mysql

chown mysql.mysql /etc/init.d/mysqld-test 
chown mysql.mysql -R /data/mysql*

Start instance

We start with mysql user

su - mysql
[mysql@chengqm ~]$ /etc/init.d/mysqld-test start
Starting MySQL.. SUCCESS!

Enter mysql client

mysql -S /data/mysql_db/mysql_test/mysql.sock

At this point, MySQL has been successfully installed and started. You can change your password later

Topics: Linux MySQL