MySQL: Write All Executed Queries Into a Log File

Submitted by Peter Majmesku on Tue, 05/23/2017 - 13:54
  1. Open the MySQL config file, which is located at /etc/mysql/my.cnf on Linux systems.
  2. Then write the following lines at the end of that file.

[mysqld]
general_log_file = /var/log/mysql/general_mysql.log
general_log = on

Restart your MySQL server (e.g. by executing "sudo service mysql restart" on Ubuntu Linux systems). Now you can follow all new queries as they are executed by this command:

tail -f /var/log/mysql/general_mysql.log

You can leave this program in command line by pressing CTRL+C.

For disabling, just set "general_log" to "off" or comment the out the previous lines in /etc/mysql/my.cnf by writing the # character at the beginng of the lines, save and restart the MySQL server again.

Tags

Show Free Disk Space on Linux

Submitted by Peter Majmesku on Tue, 05/23/2017 - 13:17

df

The "disk free" program shows the free space on a storage device via command line.

  • Show free space for the entire filesystem:
    •  df -h
  • Show free space for the local filesystem:
    • df -hl
  • Show more documentation regarding the "df" program:
    • man df

du

The "disk usage" program shows the used, not free, space on a storage device via command line.

  • Show the used space amount on the entire file system:
    • du -sh /
  • Show the used space of the folder named "media" by excluding ".bmp"-files:
    • du --exclude="*.bmp*" -sh media/ 
  • Show the used space of the current directory:
    • du -sh ./
  • Sort the folders in the current directory (recursion depth 1) by their disk usage:
    • du -h --max-depth 1 ./ | sort -h
  • Show more documentation regarding the "du" program:
    • man du
Tags

MySQL: Fixing Foreign Key Constraint Errors

Submitted by Peter Majmesku on Wed, 05/17/2017 - 19:12

Login to your MySQL server via command line. Then execute the following statement:

SHOW ENGINE INNODB STATUS;

You will get a error message like:

Foreign key constraint fails for table `my-table`.`JobExchange`:
,
  CONSTRAINT `FK_42760B1EE7A1254A` FOREIGN KEY (`contact_id`) REFERENCES `contact` (`id`) ON DELETE SET NULL
Trying to add to index IDX_42760B1EE7A1254A tuple:
DATA TUPLE: 2 fields;
 0: len 4; hex 80000001; asc     ;;
 1: len 4; hex 8000000d; asc     ;;

But the parent table `my-table`.`contact`
or its .ibd file does not currently exist!
 

This shows you, that MySQL cannot find the corresponding database table. This can be due to lowercase-uppercase mismatch of the table names. Check your MySQL server settings:

show variables like 'lower%';

This will show something like this:

+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| lower_case_file_system | OFF |
| lower_case_table_names | 0 |
+------------------------+-------+
2 rows in set (0,01 sec)

Show detailed information about the primary-foreign key references

mysql> SELECT
-> TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
-> FROM
-> INFORMATION_SCHEMA.KEY_COLUMN_USAGE
-> WHERE
-> REFERENCED_TABLE_SCHEMA = 'my-table' AND
-> REFERENCED_TABLE_NAME = 'JobExchange';


+-----------------------------------+----------------+---------------------+-----------------------+------------------------+
| TABLE_NAME | COLUMN_NAME | CONSTRAINT_NAME | REFERENCED_TABLE_NAME | REFERENCED_COLUMN_NAME |
+-----------------------------------+----------------+---------------------+-----------------------+------------------------+
| JobExchangePdf | jobexchange_id | FK_7A971E966193B1C5 | jobexchange | id |
| jobexchange_jobemploymentcategory | jobexchange_id | FK_F5E32C9C6193B1C5 | jobexchange | id |
+-----------------------------------+----------------+---------------------+-----------------------+------------------------+
2 rows in set (3,59 sec)

MySQL Workbench Vs. PhpStorm

MySQL Workbench shows the lower-case named table names correctly. PhpStorm does not. PhpStorm is guessing the right table name.

Possible fix

Drop the foreign key constraint and create a new one. F.e. with MySQL Workbench. You can drop the foreign key with the following command:

ALTER TABLE `my-table`.`JobExchange`
DROP FOREIGN KEY `FK_42760B1E6C7B4691`;
ALTER TABLE `my-table`.`JobExchange`
DROP INDEX `IDX_42760B1E6C7B4691` ;

The foreign key will be added like by this command:

ALTER TABLE JobExchange ADD CONSTRAINT FK_42760B1E6C7B4691 FOREIGN KEY (jobtype_id) REFERENCES JobExchangeCategory (id) ON DELETE SET NULL;
CREATE INDEX IDX_42760B1E6C7B4691 ON JobExchange (jobtype_id);
Tags

Ubuntu 16.04: Install NPM, Yarn and Gulp Frontend Tools

Submitted by Peter Majmesku on Sun, 05/14/2017 - 14:36
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install -y yarn nodejs
sudo npm install --global gulp-cli
Tags

Drupal 8: Debug Kernel, Functional and Unit Tests in PhpStorm by Xdebug

Submitted by Peter Majmesku on Sun, 04/16/2017 - 20:21

The following article is valid for unix based systems like Ubuntu Linux. It teaches you, how to setup your PhpStorm IDE to debug your unit tests via the Xdebug PHP extension in Drupal 8.

  1. Make sure you have read the article at https://www.drupal.org/docs/8/phpunit/running-phpunit-tests to understand how to run automated tests in Drupal 8.
  2. Make sure you have read the article at https://www.jetbrains.com/help/phpstorm/2017.1/configuring-xdebug.html to understand how to setup the Xdebug PHP extension on your system and configure it within PhpStorm.

phpunit.xml Configuration File

For the PhpStorm config you need to make sure to have the phpunit.xml file created by copying the ~/PhpstormProjects/drupal-8.x/core/phpunit.xml.dist into the same directory. Afterwards you must set the environment variables for 

  • SIMPLETEST_BASE_URL
  • SIMPLETEST_DB
  • BROWSERTEST_OUTPUT_DIRECTORY
    • Example value: ~/PhpstormProjects/drupal-8.4.x-dev/sites/simpletest/browser_output
  • PHP_IDE_CONFIG
    • Example value: serverName=d8-4x.dev
    • Be careful in this line! Here you set a key-value pair in the value field. Do not use any quotes here. The full line entry in my config is: <env name="PHP_IDE_CONFIG" value="serverName=d8-4x.dev"/>
  • XDEBUG_CONFIG
    • Example value: idekey=PHPSTORM
    • Again: a key-value pair as value.

in your newly created phpunit.xml file.

PHPUnit Config in Your PhpStorm Project

Make sure you have configured:

  • path to the autoloader script from composer. It is located in ~/PhpstormProjects/drupal-8.4/vendor/autoload.php
  • path to your phpunit.xml file

The config window must look according to the following screenshot (be careful about the paths on your system):

 

PHPUnit config

Define the Server Setting and Map the Tests Directory

Set exactly the same value, which you have previously set in your phpunit.xml file inside the PHP_IDE_CONFIG environment variable for your server. You must also map the directory where your tests are. Do your settings according to the screenshot below.

Server setting directory mapping.

Path to CLI PHP Interpreter

Set the path to the PHP interpreter which is running your command line based scripts. It is usually located at /usr/bin/php. The config must look like in the following screenshots.

php interpreter
php cli

Run a Test File

For being able to execute your test quickly, open the PHP file with your test and hit the debug icon.

Run the test

Step Into Your PHP Code

Chances are, that PhpStorm will output a error message like

Remote file path '/home/your-user/PhpstormProjects/drupal-8.4.x-dev/-' is not mapped to any file path in project.

You can ignore this error and just click the "resum program" button. PhpStorm will reveal the PHP interpreter on your breakpoint quickly. If you know, how the reason for this message can be solved - please put me a hint in the comments. However you could create a file with the name "-" and map to this file in the path settings, but this is not a real fix.

Path settings
Happy debugging

Re-initialize HTML selector after DOM was changed via AJAX

Submitted by Peter Majmesku on Tue, 03/14/2017 - 10:07

Once I was searching for a method to trigger my HTML markup, after I have changed it via an AJAX request. I could not find any non-irritating solution. So I have implemented a "common" cheap solution for this, which was documented at many places in the web (like Stackoverflow threads with many votes on a post). In the old approach, I have loaded JavaScript, which was placed within the fetched HTML markup. This resulted in an ugly structure. Because my HTML and JavaScript was mixed and not entirely separated in files.

Here is an example code with the better solution, which works by separated files:

var initFilterInput = function(el) {
    
el.find('form input').click(function(event) {
        
el.load('/reduced-filters', function() {
            
console.log"Load was performed." );

            
initFilterInput(el);
        });
    });
};

initFilterInput($('.filter-group')); 

In this example I initialize an event listener on a HTML element, which is located by the "filter-group" CSS class. This happens after the entire page load. After the click event is triggered, some HTML markup will be replaced in the current HTML DOM tree. Then this function will call itself by

initFilterInput(el);

and the event triggering still works on the new HTML markup.

Ubuntu: Install Elasticsearch as Docker Container and manage it nicely in GUI

Submitted by Peter Majmesku on Fri, 12/09/2016 - 14:16

Install Docker

Firstly install Docker via this article: https://docs.docker.com/engine/installation/linux/ubuntulinux/. Mind to setup the user and group permissions according to the article. So that you are able to start Docker as non-root user.

Check your user's group setting

Make sure that a group with the name "docker" exists and your user is a member of that group. To add a user to an existing group, execute the following command:

sudo usermod -a -G groupName userName

After your user (mine f.e. is "peter") is being successfully added into the "docker" group, logout and login to ensure that you are acting from the docker user group.

Install Kitematic

Dowload the Kitematic GUI for Docker Debian package via https://github.com/docker/kitematic/releases. This program will empower you, to manage your Docker containers easily via a graphical user interface. Mind that this application is currently beta. Then install it by

sudo dpkg -i dist/Kitematic_0.12.9_amd64.deb
Kitematic - GUI for Docker

Install Elasticsearch as Docker Container by Kitematic

Follow the following steps in Kitematic:

  1. Hit the "New"-Button
  2. Search for "elasticsearch" to see the official Elasticsearch Docker Container in the search result list
  3. Choose the version you want via the tag (version 1.7.2 is the version which works for me)
  4. Hit the button which is labeled with "Create". Kitematic will now install the Elasticsearch Docker container for you.
Kitematic Elasticsearch installation

GUI for Elasticsearch: Jettro

To be able to execute easily queries within Elasticsearch, check status etc., you can install Jettro (https://github.com/jettro/elasticsearch-gui). Just execute the following command inside your Elasticsearch Docker container to install the Elasticsearch plugin:

bin/plugin install jettro/elasticsearch-gui 

Jettro - GUI for Elasticsearch inside the webbrowser

Start the Docker Container automatically on system startup as a Daemon

An official tutorial about this can be found here: https://docs.docker.com/engine/admin/host_integration/. In particular, you can follow the steps below.

Create a file at 

/etc/systemd/system/docker-elasticsearch.service

with the following content:

[Unit]
Description=Elasticsearch container
Requires=docker.service
After=docker.service

[Service]
Restart=always
ExecStart=/usr/bin/docker start -a elasticsearch
ExecStop=/usr/bin/docker stop -t 2 elasticsearch

[Install]
WantedBy=default.target

Now reload the daemon:

sudo systemctl daemon-reload

Start the Elasticsearch Docker container as a service:

sudo systemctl start docker-elasticsearch.service

Enable the service at system startup:

sudo systemctl enable docker-elasticsearch.service

Voilà! Happy search index querying. (:

Disable IPv6 on Ubuntu Linux and Windows 10

Submitted by Peter Majmesku on Wed, 12/07/2016 - 19:57

I had this issue with "sudo apt update" or at trying to install something with composer in the WSL. The issue has been connected with IPv6. So I have disabled IPv6 and the requests where working much faster.

In Ubuntu (not Windows 10) disable IPv6 that way: Add the following lines at the bottom of /etc/sysctl.conf:

net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1

Run sudo sysctl -p to reload the /etc/sysctl.conf settings.

To disable IPv6 only for apt-get:

sudoedit /etc/apt/apt.conf.d/99force-ipv4


Put the following contents in it:

Acquire::ForceIPv4 "true";


Save the file, and you're good to go. If you want to force IPv6 instead, change the 4 to 6 in the file name and settings. To choose between one of them, test with the fast method and see which works better.

In Windows 10 I could fix my issue for the Bash by the execution of a small program from Microsoft: https://support.microsoft.com/en-us/kb/929852

Tags

Linux: Mass rename files by number and timestamp + mass edit of the last-edit date

Submitted by Peter Majmesku on Sun, 12/04/2016 - 21:26

This can be useful for masses of images editing. I needed to change the filenames of my photos to bring them in order for the popular german online print image service PosterXXL. To have all my photos ordered by specific locations, they needed ascending numeric filenames. Here I have stumbled upon useful console commands.

Rename all files to a number plus the current timestamp

ls | cat -n | while read n f; do mv "$f" "file-$(date +%s)-$n.jpg"; done

Change the last edit date of all files in a folder - not the filenames, but in the meta-information of the files

find -print | while read filename; do     touch -d "2 hours ago" "$filename"; done

 

Mass image rename in Linux