Archive for the ‘ MySQL ’ Category

Bulk show/hide pages in WordPress

We use the plugin Exclude Pages to show or hide pages in the navigation of our WordPress sites.

exclude-1
It’s very useful, but if you suddenly need to show or hide a lot of pages, it becomes tedious and a waste of time to do it through the WordPress backend.

That’s why I looked at how to bulk edit this in the database directly. Start by locating the correct table in MySQL:

exclude-2

Then add or remove page ids as needed. (Note: The numbers that appear here are the pages that are hidden from the user)

Displaying the contents of a mysql table in a web page using php

Here is a PHP script I made to display the contents of a workshop registered participants table.

<?php
// STATE YOUR VARIABLES
////////////////////////////////////////////////////////////////////
$user = "workshops";
$pass = "FV8sbyJuJ7bdT5Rj";
$db   = "workshops";
$sql  = "SELECT * FROM `wp_workshops_fm_data_15` LIMIT 0, 100";

// ESTABLISH THE CONNECTION
////////////////////////////////////////////////////////////////////
$cxn=mysqli_connect("localhost","$user","$pass","$db");   //Declare the mysqli connection parameters.
$cxn->set_charset("utf8");   // Define the default character set.
if (mysqli_connect_errno())  // Create a check for connection, if nothing then die.
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$result = mysqli_query($cxn,"$sql");

// GET NUMBER OF ATTENDEES
////////////////////////////////////////////////////////////////////
$rows = mysqli_num_rows($result);
printf("<h2>Current number of registered attendees: %d</h2>",$rows);

// CREATE THE TABLE
////////////////////////////////////////////////////////////////////
echo "<table>
        <tr>
                <th>First name</th>
                <th>Last name</th>
                <th>Job title</th>
                <th>Organisation</th>
                <th>Country</th>
                <th>Email address</th>
        </tr>\n";

while($row = mysqli_fetch_array($result))
  {
  echo "        <tr>\n";
  echo "                <td>" . "<b>" . $row['text-526647958e563'] . "</b>" . "</td>\n";
  echo "                <td>" . "<b>" . $row['text-5266498080c9d'] . "</b>" . "</td>\n";
  echo "                <td>" . $row['text-52664992cbe3f'] . "</td>\n";
  echo "                <td>" . $row['text-526649a164f02'] . "</td>\n";
  echo "                <td>" . $row['text-526649bb26849'] . "</td>\n";
  echo "                <td>" . "<a href=\"mailto:" .$row['text-526649d2a727a'] . "\">" .$row['text-526649d2a727a'] . "</a>" . "</td>\n";
  echo "        </tr>\n";
  }
echo "</table>";

// CLOSE THE CONNECTION
////////////////////////////////////////////////////////////////////
mysqli_close($cxn);

?>

UTF-8 encoded data corrupted in CSV exports

When exporting from PHPMyAdmin as CSV, Excel usually doesn’t know how to deal with the special characters, resulting in corrupted data.

csv_corruption

 

In order to avoid this, we use the following method:

  1. Open a new Excel book
  2. On the “Data” tab, click on the “From Text” button:
    data
  3. Locate your CSV file and click on “Import”
  4. In the dialog box, select the appropriate encoding (i.e. UTF-8) from the dropdown list:
    utf-8
  5. Next, select the delimiter for the CSV (i.e. comma):
    delimit
  6. Then click on Finish (you will be asked where to insert the data – normally [A1] is the place to put it).
  7. Now the data is correctly displayed in Excel:
    csv_fixed

SQL manipulations for WordPress comments and trackbacks

Disable comments and trackbacks globally:

UPDATE wp_posts SET comment_status = 'closed';
UPDATE wp_posts SET ping_status = 'closed';

Disable comments and trackbacks on post type:

UPDATE wp_posts SET ping_status = 'closed' WHERE post_type = 'post|page';

Disable comments and trackbacks on post status:

UPDATE wp_posts SET ping_status = 'closed' WHERE post_status = 'publish|inherit|pending|private|future|draft|trash' AND post_type = 'post';

MySQL commands

Extracting one database from a large dump file

Use the following command:

sed -n '/^-- Current Database: `test`/,/^-- Current Database: `/p' fulldump.sql > oneDB.sql

Using “SED” command to replace URL strings in SQL files

We want to change all references of events.ewea.org to www.ewea.org in an SQL dump, so a sed command line for this may be:

sed 's/OLDPHRASE/NEWPHRASE/g' database.sql > database.new.sql

sed 's/events\.ewea\.org/www\.ewea\.org/g' event.sql > event.new.sql

Note: We have to escape the periods and any slashes.

This is particularly useful when migrating a WordPress website off the development server and onto the production server.

Restoring a single database from a complete MySQL database dump

Found here.

I had a collection of database dumps from a server that had been created using MySQL’s –all-databases option. This contained the databases for several Drupal websites, but I wanted to restore the Drupal database for just one of the sites. After a bit of Googling I came across two simple solutions:

The first option is to pass the whole SQL dump to MySQL command line, but restrict it’s operation using the “–one-database” option.

mysql -u root -p --one-database oneDB < fulldump.sql

In the above code substitute oneDB with the database name you want to restore, and fulldump.sql with the name of your full DB backup. If you would rather just extract the database dump of the single database from the –all-databases dump file, you can do this with sed using this command:

sed -n '/^-- Current Database: `oneDB`/,/^-- Current Database: `/p' fulldump.sql > oneDB.sql

Where dbname is replaced with the database name of the database to extract, and alldatabases.sql is the name of your dump file. The result will be saved into the file oneDB.sql.

Backup database and rsync to offsite server

This is the current bash script for exporting the databases from each web server and sending them to the backup server.

#!/bin/bash
# db.sh
# Backup of database to offsite server
# Jason Bickley, Web Manager EWEA
# 9 JULY 2013

#==== RECEIVE VARIABLES FROM COMMAND LINE ====#
FREQ=$1
SERVER=$2

#==== SCRIPT OPTIONS ====#
USER=backup
PASS="mwbubCEsxCU6XVsW"
DIR=/root/backup/mysql/
FILE=localhost.sql.gz
DEST=backup.ewea.org
DATE=$(date +"%Y%m%d %T")
LOG=/var/log/backupDB_log

#==== EXCUTION OF COMMANDS ====#
# Change operating directory
cd $DIR

#==== Export database ====#
mysqldump -u$USER -p$PASS --all-databases --lock-all-tables | gzip > $SERVER.$FREQ.$FILE
chmod 600 $SERVER.$FREQ.$FILE

#==== rsync the export to offsite server ====#
rsync -aze "ssh -p 10022 -i /root/.ssh/"$SERVER"_prv_key" $SERVER.$FREQ.$FILE root@$DEST:/backups/$FREQ/$SERVER/db/

#==== delete the exported file ====#
rm -f $SERVER.$FREQ.$FILE

#==== Confirm success in log file ====#
echo $DATE Backup successful! $DEST:/backups/$FREQ/$SERVER/db/$SERVER.$FREQ.$FILE >> $LOG

To use it, you just have to run: db.sh {freq} {servername} {option}

cd /root/Scripts/backup/
./db.sh daily main

Creating a new Centos 5 server

Step by Step Guide to Installing on Centos 5.2 64bit
Last modified by kevin.connor on Mon, December 7, 2009 15:16
Source|Old Revisions

How to install Centos 5.2, PHP 5.2x, Mysql 5.1x and Magento 1.2.x Assuming that this is a server behind a firewall

Install Centos 5.2 (64bit) with correct IP settings and no packages selected except “base” restart and login

Update the system and download kernel headers in case they are needed for future software

yum update
yum install kernel*
reboot

Set the hosts file and disable ip6

nano /etc/hosts
Add IP and a hostname
nano /etc/modprobe.conf
Add 'alias ipv6 off'
Add 'net-pf-10 off'
Reboot

Disable un-needed services (look them up if you are interested)

chkconfig NetworkManager off
chkconfig NetworkManagerDispatcher off
chkconfig anacron off
chkconfig atd off
chkconfig bluetooth off
chkconfig cpuspeed off
chkconfig cups off
chkconfig gpm off
chkconfig hidd off
chkconfig ip6tables off
chkconfig iptables off
chkconfig irda off
chkconfig mdmonitor off
chkconfig mdmpd off
chkconfig pcscd off
chkconfig portmap off
chkconfig yum-updatesd off
chkconfig smartd off
service smartd stop
service NetworkManager stop
service NetworkManagerDispatcher stop
service anacron stop
service atd stop
service bluetooth stop
service cpuspeed stop
service cups stop
service gpm stop
service hidd stop
service ip6tables stop
service iptables stop
service irda stop
service mdmonitor stop
service mdmpd stop
service pcscd stop
service portmap stop
service yum-updatesd stop

Install Apache and OpenSSL

yum install httpd
yum install openssl
yum install httpd

Install the remi repositories for updated versions of PHP and MySQL not offered by Centos

wget http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm
wget http://rpms.famillecollet.com/el5.i386/remi-release-5-7.el5.remi.noarch.rpm
rpm -Uvh remi-release-5*.rpm epel-release-5*.rpm

Install php and required php extensions for Magento

yum --enablerepo=remi install php-common
yum --enablerepo=remi install php
yum install gd gd-devel
yum --enablerepo=remi install php-mcrypt php-xml php-xml php-devel php-imap php-soap php-mbstring php-mysql
yum --enablerepo=remi install php-mhash php-simplexml php-dom php-gd

(php-mhash extension no longer required as of php5.3(which is what you’ll get following these commands)-replaced by HASH Message Digest Framework in php core)

Install/Configure newest Mysql and Php extensions and enable in php.ini

yum --enablerepo=remi install mysql mysql-server
yum --enablerepo=remi install php-mysql php-pdo
nano /etc/php.ini
Add 'extension=pdo.so'
Add 'extension=pdo_mysql.so'
mysql_install_db
mysqladmin -u root password SOMEPASSWORD