Cannot set database config variable from Doctrine - php

This does not work:
bin/console doctrine:query:sql "SET FOREIGN_KEY_CHECKS=0" --env=test
I can execute the SQL part of the command via MySQL Workbench. On the console, it will just give me:
/var/www/html/vendor/doctrine/common/lib/Doctrine/Common/Util/Debug.php:71:int 0
Which could possibly be the correct return value as no columns are affected.
But when I do SELECT ##FOREIGN_KEY_CHECKS it is still set to 1.
To clarify: I use the same user to perform these operations via console as via MySQL client.

It seems that changing this setting does only apply to the current session, which will be closed directly after the command is executed.

Related

Laravel - Catch php artisan commands

I've did some changes in my config/app to use multiple databases selected by front-end,
now I have to tell in \Request()->header('database') which database I want access.
It's work perfectly, the problem is: when I try to do any artisan commands my logic dies, because isn't informed the database.
So I need to inform the database in artisan commands, like that:
php artisan migrate --database=sandiego_school
php artisan migrate:rollback --database=newyork_school
How can I observer all commands to get the argument?
In this case I guess you should create your own commands that overrides the commands you want to call, then in the handle method of the command you could specify the connexion you want to work on:
\DB::setDefaultConnection($connexion);
or also you can simply add header to request:
request()->headers->set('database', $dbname)

PHP Script - How to use php artisan command in my script?

I'd like to create a php script which connects to a mysql server, makes changes on a database and runs a php artisan command.
The first part I have figured out (mysql connection) but is it possible to just put (for example):
php artisan snipeit:ldap-sync --location_id=1
into my script and it will run the command, or am I missing something here?
I'd appreciate it if you could send me into the right direction wtih this. Thank you.
You can use Artisan::call().
Artisan:call('snipeit:ldap-sync', [
'--location_id' => 1
]);
It can also take a second parameter to specify an array of command parameters.
For more information, see Programmatically Executing Commands.
You can call artisan command from code like this:
Artisan::call('cache:clear');

How to fetch & store over 100k records to DB from another DB

I have school database who having more then 80 000 records and I want to update and insert into my newSchool database using php, whenever I try to run query update or insert almost 2 000 records and after some time its stopped automatically please help
You could (should) do a full dump and import that dump later. I'm not sure how to do it with php - and think you'd be better doing this with those commands on the cli:
mysqldump -u <username> -p -A -R -E --triggers --single-transaction > backup.sql
And on your localhost:
mysql -u <username> -p < backup.sql
The backup statement flags meanings from the docs:
-u
DB_USERNAME
-p
DB_PASSWORD
Don't paste your password here, but enter it after mysql asks for it. Using a password on the command line interface can be insecure.
-A
Dump all tables in all databases. This is the same as using the --databases option and naming all the databases on the command line.
-E
Include Event Scheduler events for the dumped databases in the output.
This option requires the EVENT privileges for those databases.
The output generated by using --events contains CREATE EVENT
statements to create the events. However, these statements do not
include attributes such as the event creation and modification
timestamps, so when the events are reloaded, they are created with
timestamps equal to the reload time.
If you require events to be created with their original timestamp
attributes, do not use --events. Instead, dump and reload the contents
of the mysql.event table directly, using a MySQL account that has
appropriate privileges for the mysql database.
-R
Include stored routines (procedures and functions) for the dumped
databases in the output. Use of this option requires the SELECT
privilege for the mysql.proc table.
The output generated by using --routines contains CREATE PROCEDURE and
CREATE FUNCTION statements to create the routines. However, these
statements do not include attributes such as the routine creation and
modification timestamps, so when the routines are reloaded, they are
created with timestamps equal to the reload time.
If you require routines to be created with their original timestamp
attributes, do not use --routines. Instead, dump and reload the
contents of the mysql.proc table directly, using a MySQL account that
has appropriate privileges for the mysql database.
--single-transaction
This option sets the transaction isolation mode to REPEATABLE READ and
sends a START TRANSACTION SQL statement to the server before dumping
data. It is useful only with transactional tables such as InnoDB,
because then it dumps the consistent state of the database at the time
when START TRANSACTION was issued without blocking any applications.
If you only need the data and don't need routines nor events, just skip those flags.
Be sure to do commit after a few commands, for example after 500 rows. That save memory but has the problem that in case of rollback only going back to the last commit.

How to use PHP to access MySQL shell?

Currently I have a shell script within my application that depending on what argument you pass it, it will log you into either the live or development MySQL database shell. It works fine, no problem there.
The issue/challenge I am having is that if in the case my MySQL credentials (host / port) change for either the live or development database then I will manually have to edit this shell script, updating it will the new arguments. Problem is, I have done this before but I never want to have to do it again.
What I would like to do is have a PHP script (mysql.sh.php) that when executed, depending on the argument passed to it will log you into either the live or development database shell. How it will differ from the shell script is that it will pull the current credentials (and even host and port) from a PHP configuration class and pass those as arguments to a shell command that will log into the respective database.
Below gives you an illustration of what I am attempting.
#!/usr/bin/php
<?php
include ('common.php');
//Pull info from PHP class right here
exec("mysql --host={$myhostnotyours} --user={$myusernotyours} -p{$mypassnotyours} {$thedatabase}");
What I expect or would like is
mysl>
However, the command just hangs and I am not presented with the MySQL shell.
I would like to know if there is a way to accomplish what I am trying.
Thanks!

creating mysql db from exported sql with php exec

All I want to do is create a new database off of a dump created by phpmyadmin.
This dump is located in : /var/www/iadmin/wikifresh/db/template.sql and won't ever change.
I ran this command once, and the database was created fine
mysql test < /var/www/iadmin/wikifresh/db/template.sql
and it created the database. So, I put that script inside of a php exec command:
(note: $wikiname is the name of the new wiki being created)
$dbwikiname = escapeshellarg($wikiname);
exec("mysql $dbwikiname < /var/www/iadmin/wikifresh/db/template.sql");
now, when this script runs, I get:
ERROR 1049 (42000): Unknown database 'test'
even if I try to run it from my command line I have this issue.
what am I doing wrong?
For mysql to be invoked with a default database name, the database must exist.
Some distributions of MySQL happen to ship with a database called test, which explains why your command succeeded at first. One presumes you have since dropped that database? You will need to recreate it before attempting to select it:
CREATE DATABASE IF NOT EXISTS test;
Of course, you could instead place the above command, followed by USE test; atop your template.sql file and then invoke mysql without specifying a default database.
it seems you don't have the database 'test' created in the MySQL server of that machine, and the template.sql doesn't have the 'CREATE DATABASE' command. So you have to either create it first, or add the 'CREATE DATABASE' to the template.sql

Categories