Using MySQL as Data Backend
(Raw wiki text)
Antes de começar, certifique-se de que o servidor de banco de dados MySQL está
corretamente [http://www.mysql.org/doc/refman/4.1/en/installing.html instalado]
e [http://www.mysql.org/doc/refman/4.1/en/post-installation.html rodando]. Este
processo descrito aqui foi testado com a versão 4.1, mas pode funcionar também
para outras versões.
== Conexão TCP ==
O Motiro utiliza uma conexão TCP para se comunicar com o servidor MySQL. É
necessário certificar-se que o servidor está ativo e escutando por conexões
na porta 3306 (a porta padrão para servidores MySQL). Se você não tiver
certeza, tente executar o comando:
<pre>% telnet localhost 3306</pre>
A saída deve ser parecida com
<pre>
7
<code> 4.1.18-nt</code>
<code> xH?@cU]S,�!?h3d{bUf~jv5A</code>
</pre>
Se não houver resposta, pode ser que seu servidor MySQL não esteja ativo ou
não esteja aceitando conexões TCP. Caso julgue necessário como medida de
segurança, o servidor MySQL pode ser instruído a aceitar conexões apenas da
máquina local.
Para maiores informações, por favor consulte o
[http://www.mysql.org/doc/refman/4.1/en/index.html manual do MySQL].
== Criação das bases de dados ==
O Motiro utiliza um usuário de nome 'motiro' e senha vazia para
autenticar-se junto ao servidor MySQL. Pode ser necessário acessar três
bases de dados diferentes, dependendo do contexto de execução. Os nomes
delas são:
* motiro_development: para propósitos de desenvolvimento e testes iniciais
* motiro_test: para propósitos de testes automáticos
* motiro_production: para utilização no selvagem mundo real
É preciso certificar-se que estas bases de dados existem e estão
completamente acessíveis para o usuário motiro. O script a seguir deve
resolver a bronca. É um script para interface de linha de comando que utiliza
o cliente <code>mysql</code> padrão, mas você pode usar qualquer ferramenta com a qual
sinta-se mais confortável (talvez uma com interface gráfica ou web). Deve
dar tudo certo desde que não haja erros de digitação nos nomes das bases de
dados e lembrar de dar todos os privilégios para o usuário motiro.
Para usar o cliente mysql basta acioná-lo como root e dizer algo nessa linha:
(as linhas que começam com '%' são comandos para meu shell e com 'mysql>' são
para o banco de dados MySQL, as demais são respostas do sistema)
<pre>
% mysql -u root -p
Enter password: ***********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13 to server version: 4.1.18-nt
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> create database motiro_development;
Query OK, 1 row affected (0.00 sec)
mysql> create database motiro_test;
Query OK, 1 row affected (0.00 sec)
mysql> create database motiro_production;
Query OK, 1 row affected (0.02 sec)
mysql> grant all on motiro_development.* to motiro;
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on motiro_test.* to motiro;
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on motiro_production.* to motiro;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
</pre>
== Reconfigurar para usar MySQL ==
O arquivo <code>config/database.yml</code> contém as configurações de base
de dados para o Motiro. O padrão atual é usar SQLite 3. Para usar suas
recém-nascidas bases de dados MySQL, substitua o conteúdo do arquivo
pelo trecho abaixo.
development:
adapter: mysql
database: motiro_development
host: 127.0.0.1
port: 3306
username: motiro
password:
test:
adapter: mysql
database: motiro_test
host: 127.0.0.1
port: 3306
username: motiro
password:
production:
adapter: mysql
database: motiro_production
host: 127.0.0.1
port: 3306
username: motiro
password:
== Preparação do esquema ==
O último passo para a configuração das bases de dados é estruturar os
esquemas corretamente. Felizmente, o Motiro já vem com scripts de migração
pré-cozidos para este propósito. Você só vai precisar pedir ao rake para
rodá-los.
<pre>% rake migrate</pre>
Este último passo é idêntico para a instalação padrão. A partir daqui você pode
prosseguir pelas [[Installation|instruções principais]].
--- en -------------------------------------------------------------------------
Before starting, make sure that the MySQL database server is correctly
[http://www.mysql.org/doc/refman/4.1/en/installing.html installed] and
[http://www.mysql.org/doc/refman/4.1/en/post-installation.html running]. The
process described here was tested for version 4.1, but is likely to work on
other versions too.
== TCP Connection ==
Motiro tries to connect to the MySQL server using a TCP socket. You will need
to make sure the server is up and listening for connections on port 3306
(MySQL default port). If you're not sure, try issuing the command
<pre>% telnet localhost 3306</pre>
The output should look something like
<pre>
7
<code> 4.1.18-nt</code>
<code> xH?@cU]S,�!?h3d{bUf~jv5A</code>
</pre>
You may need to set up your MySQL server to accept TCP connections, if you
are sure the server is running and it doesn't respond to the above command.
You may also set up your MySQL server to answer only to connections
originated from the localhost, if you feel opening the port is a security
issue. For more information, please see the
[http://www.mysql.org/doc/refman/4.1/en/index.html MySQL user guide].
== Database creation ==
Motiro uses the empty-passworded username 'motiro' to authenticate to the
MySQL server. It may need to access three differente databases, depending on
the execution context. The database names are
* motiro_development: for development and initial test purposes
* motiro_test: for automatic testing purposes
* motiro_production: for real-life usage
You will need to make sure those databases are present and fully-acessible
to the user motiro. The following script will do the trick. It is a
command-line script using the mysql client, but you may use any tool you'd
prefer (maybe a GUI or web-based one). Just make sure to create the
databases with the exact names and to grant all privileges to the motiro
user in them.
To use the mysql client just fire it as the root user and say (lines
starting with '%' are commands to my shell and with 'mysql>' are to the
MySQL database, others are the system responses to them):
<pre>
% mysql -u root -p
Enter password: ***********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13 to server version: 4.1.18-nt
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> create database motiro_development;
Query OK, 1 row affected (0.00 sec)
mysql> create database motiro_test;
Query OK, 1 row affected (0.00 sec)
mysql> create database motiro_production;
Query OK, 1 row affected (0.02 sec)
mysql> grant all on motiro_development.* to motiro;
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on motiro_test.* to motiro;
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on motiro_production.* to motiro;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
</pre>
== Point to MySQL ==
The file <code>config/database.yml</code> is the one that points to the
database backend that Motiro should user. The default setup right now is
to use SQLite 3. In order to use you newly-created MySQL databases,
change the file contents for what you see below.
development:
adapter: mysql
database: motiro_development
host: 127.0.0.1
port: 3306
username: motiro
password:
test:
adapter: mysql
database: motiro_test
host: 127.0.0.1
port: 3306
username: motiro
password:
production:
adapter: mysql
database: motiro_production
host: 127.0.0.1
port: 3306
username: motiro
password:
== Preparing the schema ==
Last step for setting your databases is setting up their schemas correctly.
Fortunately, Motiro comes prepackaged with migration scripts for this
purpose. You will need to ask rake to run them.
<pre>% rake migrate</pre>
This last step is exactly the same thing that should be done for the default
set up. From now on you can refer to the [[Installation|main instructions]].