Instalação do MySQL Database Server 5.7

Olá pessoal, neste breve artigo vou apresentar como instalar o MySQL Database Server 5.7 no Oracle Linux 6.8. Vou fazer a instalação a partir dos arquivos rpm.

MySQL Database Server

Inicialmente vamos criar o grupo mysql e o usuário mysql e atribuí-lo ao grupo:

[root@mysql-master ~]# groupadd mysql -g 1001
[root@mysql-master ~]# useradd -g 1001 -u 1001 mysql

Vamos verificar se ficou correto:

[root@mysql-master ~]# id mysql
uid=1001(mysql) gid=1001(mysql) groups=1001(mysql)

Então agora precisamos definir uma senha para o usuário mysql:

[root@mysql-master ~]# passwd mysql
Changing password for user mysql.
New password:
BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.

Neste caso estou usando a versão commercial do MySQL Database Server, mas o procedimento é exatamente o mesmo para a versão community que você pode baixar aqui.

Descompactamos o arquivo que contém os pacotes rpm do MySQL Database Server:

[root@mysql-master mysql]# unzip MySQL\ Database\ 5.7.9\ RPM\ for\ Oracle\ Linux\ \ RHEL\ 6\ x86\ \(64bit\)\ \(Patchset\).zip
Archive: MySQL Database 5.7.9 RPM for Oracle Linux RHEL 6 x86 (64bit) (Patchset).zip
extracting: mysql-commercial-devel-5.7.9-1.1.el6.x86_64.rpm
extracting: mysql-commercial-libs-5.7.9-1.1.el6.x86_64.rpm
extracting: mysql-commercial-test-5.7.9-1.1.el6.x86_64.rpm
extracting: mysql-commercial-common-5.7.9-1.1.el6.x86_64.rpm
extracting: mysql-commercial-server-5.7.9-1.1.el6.x86_64.rpm
extracting: mysql-commercial-embedded-5.7.9-1.1.el6.x86_64.rpm
extracting: mysql-commercial-embedded-devel-5.7.9-1.1.el6.x86_64.rpm
extracting: mysql-commercial-libs-compat-5.7.9-1.1.el6.x86_64.rpm
extracting: mysql-commercial-client-5.7.9-1.1.el6.x86_64.rpm
extracting: README.txt

Ajuste o arquivo /etc/hosts com seu endereço de IP e hostname:

[root@mysql-master mysql]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.56.101 mysql-master.localdomain mysql-master

Antes de instalar os pacotes do MySQL que baixamos vamos verificar se há alguma outra instalação do MySQL Database Server no nosso servidor:

[root@mysql-master mysql]# rpm -qa|grep mysql
mysql-libs-5.1.73-7.el6.x86_64

Conforme é possível observar temos o mysql-libs da versão 5.1.73 e este vai conflitar com a instalação do MySQL Database Server 5.7, então vamos removê-lo:

[root@mysql-master mysql]# yum erase mysql-libs-5.1.73-7.el6.x86_64
Loaded plugins: refresh-packagekit, security, ulninfo
Setting up Remove Process
Resolving Dependencies
--> Running transaction check
---> Package mysql-libs.x86_64 0:5.1.73-7.el6 will be erased
--> Processing Dependency: libmysqlclient.so.16()(64bit) for package: 2:postfix-2.6.6-6.el6_7.1.x86_64
......
--> Running transaction check
---> Package crontabs.noarch 0:1.10-33.el6 will be erased
---> Package sysstat.x86_64 0:9.0.4-31.el6 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================================================================================================================
Package Arch Version Repository Size
=====================================================================================================================================================================================================
Removing:
mysql-libs x86_64 5.1.73-7.el6 @anaconda-OracleLinuxServer-201605181719.x86_64/6.8 4.0 M
Removing for dependencies:
cronie x86_64 1.4.4-15.el6_7.1 @anaconda-OracleLinuxServer-201605181719.x86_64/6.8 174 k
......
redhat-lsb-graphics x86_64 4.0-7.0.2.el6 @anaconda-OracleLinuxServer-201605181719.x86_64/6.8 0.0
redhat-lsb-printing x86_64 4.0-7.0.2.el6 @anaconda-OracleLinuxServer-201605181719.x86_64/6.8 0.0
sysstat x86_64 9.0.4-31.el6 @anaconda-OracleLinuxServer-201605181719.x86_64/6.8 826 k

Transaction Summary
=====================================================================================================================================================================================================
Remove 11 Package(s)

Installed size: 15 M
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Erasing : redhat-lsb-4.0-7.0.2.el6.x86_64 1/11
Erasing : redhat-lsb-compat-4.0-7.0.2.el6.x86_64 2/11
......
Verifying : 2:postfix-2.6.6-6.el6_7.1.x86_64 9/11
Verifying : redhat-lsb-graphics-4.0-7.0.2.el6.x86_64 10/11
Verifying : redhat-lsb-4.0-7.0.2.el6.x86_64 11/11

Removed:
mysql-libs.x86_64 0:5.1.73-7.el6

Dependency Removed:
cronie.x86_64 0:1.4.4-15.el6_7.1 cronie-anacron.x86_64 0:1.4.4-15.el6_7.1 crontabs.noarch 0:1.10-33.el6 postfix.x86_64 2:2.6.6-6.el6_7.1
redhat-lsb.x86_64 0:4.0-7.0.2.el6 redhat-lsb-compat.x86_64 0:4.0-7.0.2.el6 redhat-lsb-core.x86_64 0:4.0-7.0.2.el6 redhat-lsb-graphics.x86_64 0:4.0-7.0.2.el6
redhat-lsb-printing.x86_64 0:4.0-7.0.2.el6 sysstat.x86_64 0:9.0.4-31.el6

Complete!

Agora sim vamos instalar os pacotes necessários para o MySQL 5.7. Instale na ordem a seguir que não haverá incompatibilidades por conta das dependências.

[root@mysql-master mysql]# rpm -ivh mysql-commercial-common-5.7.9-1.1.el6.x86_64.rpm
warning: mysql-commercial-common-5.7.9-1.1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:mysql-commercial-common########################################### [100%]
[root@mysql-master mysql]# rpm -ivh mysql-commercial-libs-5.7.9-1.1.el6.x86_64.rpm
warning: mysql-commercial-libs-5.7.9-1.1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:mysql-commercial-libs ########################################### [100%]
[root@mysql-master mysql]# rpm -ivh mysql-commercial-libs-compat-5.7.9-1.1.el6.x86_64.rpm
warning: mysql-commercial-libs-compat-5.7.9-1.1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:mysql-commercial-libs-c########################################### [100%]
[root@mysql-master mysql]# rpm -ivh mysql-commercial-client-5.7.9-1.1.el6.x86_64.rpm
warning: mysql-commercial-client-5.7.9-1.1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:mysql-commercial-client########################################### [100%]
[root@mysql-master mysql]# rpm -ivh mysql-commercial-server-5.7.9-1.1.el6.x86_64.rpm
warning: mysql-commercial-server-5.7.9-1.1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:mysql-commercial-server########################################### [100%]

Verifique se o diretório /var/lib/mysql pertence ao usuário mysql, senão execute o seguinte:

[root@mysql-master mysql]# chown -R mysql:mysql /var/lib/mysql

Verifique o arquivo /etc/my.cnf para conferir qual é o valor do parâmetro datadir:

[root@mysql-master ~]# grep datadir /etc/my.cnf
datadir=/var/lib/mysql

Com este parâmetro inicialize o MySQL Database Server através do daemon mysqld com o usuário mysql e acompanhe o log através do arquivo /var/log/mysqld.log.

[mysql@mysql-master ~]$ mysqld --initialize --datadir=/var/lib/mysql

Após a inicialização o MySQL vai gerar uma senha aleatória:

[root@mysql-master mysql]# tail -n 2 /var/log/mysqld.log
2017-02-14T23:46:55.173234Z 0 [Warning] CA certificate ca.pem is self signed.
2017-02-14T23:46:55.500197Z 1 [Note] A temporary password is generated for root@localhost: ?GZ*=u2uHMpa

Em posse desta senha (?GZ*=u2uHMpa) vamos executar o procedimento abaixo para alterar esta senha e definir alguns contextos de segurança:

[mysql@mysql-master ~]$ mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:

The existing password for the user account root has expired. Please set a new password.

New password:

Re-enter new password:

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: N
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : N

... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N

... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
Success.

All done!

Faça uma conexão ao MySQL Database Server usando o client mysql e a nova senha definida:

[mysql@mysql-master ~]$ mysql -u root -poracle
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.9-enterprise-commercial-advanced MySQL Enterprise Server - Advanced Edition (Commercial)

Copyright (c) 2000, 2015, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)

Para verificar o status, parar e iniciar o MySQL Database Server execute:

[root@mysql-master ~]# service mysqld status
mysqld (pid  774) is running...
[root@mysql-master ~]# service mysqld stop
Stopping mysqld:                                           [  OK  ]
[root@mysql-master ~]# service mysqld start
Starting mysqld:                                           [  OK  ]

Vamos verificar se o MySQL está configurado para iniciar automaticamente no boot do servidor:

[root@mysql-master ~]# chkconfig --list mysqld
mysqld          0:off   1:off   2:off   3:off   4:off   5:off   6:off

Observamos que em todos os modos de inicialização do Linux o daemon mysqld não irá iniciar, portanto vamos configurar para que ele faça isso:

[root@mysql-master ~]# chkconfig mysqld on

Vamos também inserir uma exceção no nosso firewall para que a porta 3306 do MySQL esteja liberada para acesso externo:

[root@mysql-master ~]# service iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
2    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0
4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
5    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination

[root@mysql-master ~]# iptables -I INPUT -p tcp -m tcp --dport 3306 -j ACCEPT
[root@mysql-master ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[root@mysql-master ~]# service iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:3306
2    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
3    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0
4    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0
5    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
6    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination

Vamos também desabilitar o SELinux para evitar problemas:

[root@mysql-master ~]# vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted

[root@mysql-master ~]# setenforce 0

Por hoje é só galera. A instalação do MySQL Database Server é bastante simples. Aos poucos vou apresentar e explorar um pouco mais este banco de dados. Dúvidas, sugestões ou críticas construtivas é só deixar um comentário.

Abraços,

Franky

  • David Ricardo

    Parabéns pelo Post Franky, muito bom e didático. Grande abraço