Php - phinx migration - php

I am using phinx for migrations in my web app.
my phinx.yml:
paths:
migrations: %%PHINX_CONFIG_DIR%%/migrations
environments:
default_migration_table: app_migrations
default_database: app_database
development:
adapter: mysql
host: localhost:8888
name: app_database
user: ''
pass: ''
port:8889
I have mamp with ports apache:8888 and mysql:8889 running.
The database app_database does exist.
The table app_migrations does not exist(it will be created while migrating right?)
I already created some migrations and now i try to run them:
php vendor/bin/phinx migrate -e development
output in console:
Phinx by Rob Morgan - https://phinx.org. version 0.5.1
using config file ./phinx.yml
using config parser yaml
using migration path /path/to/directory/migrations
using environment development
using adapter mysql
using database app_database
Then it stops and nothing happens...
Anybody could help me with this issue?
Thanskj and Greetings!

Maybe there is some config-hickup. I know this phenomenon from conneting to a mysql-database that is not reachable via a specific host / port. This always leads to timeouts but only after eg. 60 seconds.
You wrote this in your config:
host: localhost:8888
[...]
port: 8889
First remove the port from the hostname:
host: localhost
port: 8889
and as second verify the mysql-server is really listening on localhost / port 8889.
You'll find more on in the "Configuration"-chapter of phinx-documentation: http://docs.phinx.org/en/latest/configuration.html
Regards

If you're using the default settings for MAMP, the username for the database would be root. Try changing the value of user in phinx.yml to root.

Related

How to use mysql instead of mariadb in symfony 5

i think this might be a stupid question for some, but i swear that i can't find the answer by myself actually.
I'm still learning Symfony so please be nice.
Actually i have set up the access to my database in the .env file :
[...]
DATABASE_URL=mysql://root:db_password#127.0.0.1:3308/db_name
[...]
And then i have define the environnement variable in the config/doctrine.yaml :
doctrine:
dbal:
default_connection: default
connections:
default:
dbname: Symfony
user: root
password: null
host: localhost
server_version: '8.0'
driver: pdo_mysql
charset: UTF8
So i run the following command :
> php bin/console doctrine:database:create
But the 'Symfony' database have been created under mariadb.
:( I don't understand why because i have define precisely that i use a 8.0 version and this version is not a mariadb version.
I've also try this :
# .symfony/services.yaml
mydatabase:
# supported versions: 5.7, 8.0
type: oracle-mysql:8.0
disk: 1024
As the symfony documentation saying to do, but it still not working at all.
I have got the following error message as mydatabase is not a symfony services :
C:\Users\vincp\Symfony\charming_tuto>php bin/console doctrine:database:create
In FileLoader.php line 173:
There is no extension able to load the configuration for "mydatabase" (in "C:\Users\vincp\Symfony\charming_tuto\con
fig/services.yaml"). Looked for namespace "mydatabase", found ""framework", "sensio_framework_extra", "twig", "twig
_extra", "web_profiler", "monolog", "debug", "webpack_encore", "doctrine", "doctrine_migrations", "maker"" in C:\Us
ers\vincp\Symfony\charming_tuto\config/services.yaml (which is being imported from "C:\Users\vincp\Symfony\charming
_tuto\src\Kernel.php").
I wanna use mysql because this is the DBMS that i use usually.
(If you think that i should use mariadb instead of mysql i'm open to that, but give me some arguments. I'd be really interseted in)
Maybe it is a server issue. So first, make sure that MySQL is running on your server besides MariaDB (how to do it depends on the type of server you are using).
If yes, it might be a port setting issue: I see that you set the database port to 3308. That is probably the port on which MariaDB is listenning. Try to determine if there is another db running on your server listening to another port (for example 3306 is very often the default MySQL port). To do so, on a Linux server you could try the following command :
netstat -tlnp | grep mysql
If you find only x x.x.x.x:3308 address or so, I bet that MySQL is not running on your server and you only run a MariaDB instance. But if you find addresses on other ports (for example x x.x.x.x:3306) chances are that a MySQL instance is running and listens that other port. If that's the case, you should consequently change the DATABASE_URL in your .env file.

Docker-compose: Connect to database using localhost & mysql as hostname

I'm currently trying to dockerize my app for local development. For a bit of context, it is using Magento.
I have a configuration file where I'm used to set 127.0.0.1 as MySQL hostname as the web app is running on the same host as MariaDB.
Initially, I've tried to link my container within my docker-compose file using 'links' (below an extract of my docker-compose setting at this point)
mariadb:
image: mariadb:5.5
php-fpm:
build: docker/php-fpm
links:
- "mariadb:mysql"
At this point, MariaDB was reachable by setting mysql as hostname in my configuration file instead of 127.0.0.1. However, I wanted to keep 127.0.0.1.
After a bit of digging, I've found this blog post where it is explained how to set up containers so that it can be reached through localhost or 127.0.0.1
This is working as I'm expecting but it has a flaw.
Without Docker, I'm able to run PHP scripts which leverage magento core modules by loading it. But with Docker and the blog post configuration, I'm not able to do it anymore as Magento is weirdly expecting for a DB hostname called "mysql".
Is there anyway through docker-compose to have a container be reachable with localhost and an hostname?
Without Docker, if I install MariaDB on my host machine, I am able to connect to its instance through 127.0.0.1:3306 or mysql://. I want to get a similar behaviour.
As said by #Sai Kumar you can connect the docker containers to the host network and then can use localhost to access services. But the problem is the port will be reserved by that container and will not be available till it is deleted.
But from your question, the following sentence caught my attention
Without Docker, I'm able to run PHP scripts which leverage magento
core modules by loading it. But with Docker and the blog post
configuration, I'm not able to do it anymore as Magento is weirdly
expecting for a DB hostname called "mysql"
So If I understand properly Magento is expecting to connect to MySQL with mysql as hostname instead of localhost. If so this can be easily solved.
How?
So in docker, there is a concept called service discovery. I've explained the same in many of my answers. Basically what it does is it resolves IP of containers by container hostname/aliases.So, instead of connecting between containers using IP address you can connect between them by using their hostname such that even if container restarts(which results in change of IP), Docker will take care of resolving it to respective container.
This works only with user-defined networks. So what you can do is create a bridge network and connect both Magento and Mysql to it. and give the container_name as mysql for mysql or you can also use alias as mentioned here. So putting it all together a sample docker compose will be
version: '3'
services:
mariadb:
image: mariadb:5.5
container_name: mysql #This can also be used but I always prefer using aliases
networks:
test:
aliases:
- mysql #Any container connected to test network can access this simply by using mysql as hostname
php-fpm:
build: docker/php-fpm
networks:
test:
aliases:
- php-fpm
networks:
test:
external: true
More references
1. Networking in Compose.
2. Docker Networking.
Yes you can connect through your db with localhost or 127.0.0.1 But this is only possible when you create docker-compose network in host mode.
But when you set your docker network in host mode then containerising concept will fail. So you have to choose host or bridge network mode
You can find networking in docker-compose
network_mode: "host"

Problems connecting to Mysql on a docker container via PDO, but command line works

A brief summary of my question:
What characteristic of my docker-compose is colliding with PDO that prevents Host -> mysql-docker via PDO, but allows Host -> mysql-docker from all other tools?
My App's config file:
database:
host: mysql
port: 3306
name: <name>
username: <username>
password: <pass>
outsideContainerConnections:
host: 127.0.0.1
port: 3307
Everything is running fine within the container, and I am able to access the database from the host using PHPStorm or Mysql on the command line.
If I run this command from the host, it connects
mysql --port=3307 -h 127.0.0.1 -u <username> -p
However, if I try to connect from php on the host, using PDO, it fails with
PDOException: SQLSTATE[HY000] [2002] Connection refused
PDO DSNs I've tried:
mysql:host=127.0.0.1:3307;port=;dbname=<name>;&charset=utf8;
or
mysql:host=127.0.0.1;port=3307;dbname=<name>;&charset=utf8;
or
mysql:host=127.0.0.1:3307;dbname=<name>;&charset=utf8;
I have read about using localhost vs 127.0.0.1 to force TCP, and I am doing that here.
Here also is the relevant section of my docker-composer. Again, using command line or other tools from the host work fine, it is only PDO that seems to have an issue. For what it's worth, PDO on another container in the docker-compose network is behaving.
mysql:
build: './mysql_docker'
command: --lower_case_table_names=0
ports:
- '3307:3306'
volumes:
- ./volumes/mysql:/var/lib/mysql
- ./volumes/my.cnf:/etc/my.cnf
networks:
- app-tier
Thank you for reading.
I've solved it– it's simple but hopefully a good lesson to pass on.
In one case the PHP file was being run by cron, and so it was being run locally, outside of the docker container. I had a small syntax or other error in my file so cron was having some trouble.
In order to debug this situation, I was opening the file in my web browser–and that's the kicker. The connection errors I saw in the web browser were not the same problems cron was having, because that page was being served by the docker container. Thus, the connection details had to be different. The same setup could not work both from the host and from the neighbor container.
What I learned is this; what seems like a really heady technical problem that requires lots of manual reading and research COULD be a simple bad assumption. Sometimes it's worth going back to the drawing board and sketching an outline of the situation from the ground up.
In a way that is what I did with asking this question, so thank you for the space to do so.
try "mysql --port=3307 -h 127.0.0.1 -u -p" from client if no

Cant create migration phinx

When I try create migration I get this exception, but I have not got curly braces in my migration path
[Exception]
You probably used curly braces to define the migration path in your Phinx configuration file, but no directories have been matched using this pattern. You need to create a migration directory manually.
My config:
paths:
migrations: %%PHINX_CONFIG_DIR%%/db/migrations
seeds: %%PHINX_CONFIG_DIR%%/db/seeds
environments:
default_migration_table: phinxlog
default_database: development
production:
adapter: mysql
host: localhost
name: production_db
user: root
pass: ''
port: 3306
charset: utf8
development:
adapter: mysql
host: localhost
name: development_db
user: root
pass: ''
port: 3306
charset: utf8
testing:
adapter: mysql
host: localhost
name: testing_db
user: root
pass: ''
port: 3306
charset: utf8
version_order: creation
Just came across this same error when trying to setup CakePHP & Phinx.
Was caused by missing out the directory when running the Phinx command.
Docs say to init the system type :
phinx init .
(If Phinx was installed via composer use : vendor/bin/phinx init . )
If you miss out the '.' you get the error you have described. It looks like Phinx doesn't have a default setting of using the currently working directory.
http://docs.phinx.org/en/latest/commands.html#the-init-command.
You can fix the issue by ether re-running the Phinx command or by manually creating the folders :
mkdir -p db/migrations db/seeds
Hope this helps anyone searching in the future.
Not sure if you ever found your answer, but I fixed this error by manually creating the db/migrations directory in the root. You might have to do the same with db/seeds.
Probably you are running "php vendor/bin/phinx " from inside the migration directory. Try this from root directory. I was getting same error and fixed from root folder.
If you are using CakePHP 3.0 or 4.0 your paths might need updating..
'paths' => [
'migrations' => '%%PHINX_CONFIG_DIR%%/config/Migrations',
'seeds' => '%%PHINX_CONFIG_DIR%%/config/Seeds'
],
Worked for me.

Codeception - pointing DB to MAMP MySQL server

Is there a way to point the DB module for Functional tests in codeception to your MAMP MySQL server rather than the built in server?
class_name: TestGuy
modules:
enabled: [Db, Filesystem, TestHelper]
config:
Db:
dsn: 'mysql:host=localhost;dbname=testdb'
user: 'root'
password: 'root'
dump: 'tests/_data/dump.sql'
populate: true
cleanup: false
Codeception uses PDO to connect to the database. It will pass the "dsn" string straight into the PDO constructor. Thus, according to http://www.php.net//manual/en/ref.pdo-mysql.connection.php, your configuration should be the following in order to connect to the MAMP MySQL Server running on port 8889:
class_name: TestGuy
modules:
enabled: [Db, Filesystem, TestHelper]
config:
Db:
dsn: 'mysql:host=localhost;port=8889;dbname=testdb'
user: 'root'
password: 'root'
dump: 'tests/_data/dump.sql'
populate: true
cleanup: false
This is what fixed the issue for me.
In MAMP I ticket the box "Allow network access to MySQL"
My dsn setting in codeception.yml is:
dsn: 'mysql:host=127.0.0.1;port=3306;dbname=la51'
Have you tried this at all - might work for you too..
class_name: TestGuy
modules:
enabled: [Filesystem, TestHelper, WebHelper, PhpBrowser]
config:
PhpBrowser:
url: 'http://localhost/yourapp/'
curl:
CURL_RETURNTRANSFER: true
Let me know how that works for you :)
http://codeception.com/docs/modules/PhpBrowser
I'd double check your mysql table within the dump
Once setup just add something like this to your tests
$I->seeInDatabase('users', array('name' => 'Davert', 'email' => 'davert#mail.com'));
Thiss will check the 'users' table and look for the row with the name & email as supplied in the array - works for me.
Two things—I believe—would fix this your problem
Make sure the module for which you are setting the database parameters is the same. DB is different from Db
MAMP runs its MySQL on port 8889. If you only specify localhost in the dsn string, Codeception will try to connect to a MySQL instance running on port 3306—which is MySQL's default port. Change localhost to localhost:8889
I just fixed this problem myself. Comment for further clarifications if need be.
Cheers
EDIT
Recently, I revisited the project which I used Codeception. This solution did not work for me. I ran into two issues
Timezone Exception You can fix this by...
Making sure the timezone is set correctly in your php.ini file, which is usually in /Applications/MAMP/bin/php/php5.x/conf/
AND/OR aliasing php to use MAMP's by doing alias phpm="/Applications/MAMP/bin/php5.x/bin/php"
[Codeception\Exception\Module] (Exception in Db) This is primarily because of the configuration of the module. I removed the port number from my configuration and everything seems to work fine.

Categories