MySQL Dump/Restore

Dump ALL MySQL Databases
mysqldump –user=user_name –password=password -A > /PATH/TO/DUMPFILE.SQL

Dump Individual or Multiple MySQL Databases
mysqldump –user=user_name –password=password –databases DB_NAME1 DB_NAME2 DB_NAME3 > /PATH/TO/DUMPFILE.SQL

Dump only certain tables from a MySQL Database
mysqldump –user=user_name –password=password –databases DB_NAME –tables TABLE_NAME > /PATH/TO/DUMPFILE.SQL

To make dumps compatible with older versions add the following switch:
–compatible=mysqlXXX

Use the following to reload the contents of a database:

mysql –verbose –user=user_name –password=password DB_NAME < /PATH/TO/DUMPFILE.SQL

THIS IS FOR MYSQL ONLY! NOT YOUR FIREWALL! NOT YOUR IPTABLES! NOTHIN BUT MYSQL!

Find your my.cnf
Debian/Ubuntu -> /etc/mysql/my.cnf
Red Hat Linux/Fedora/Centos -> /etc/my.cnf

sudo vi /etc/mysql/my.cnf

Make sure bind-address set to your server IP address, (the ip address of that machine, it will probably say 127.0.0.1)

bind-address = 192.158.5.1
Also make sure line skip-networking is removed or commented out
# skip-external-networking

sudo /etc/init.d/mysql restart

Let us assume that you are always making connection from remote IP called 202.54.10.20 for database called webdb for user webadmin then you need to grant access to this IP address.
At mysql> prompt type following command for existing database:

Code:
update db set Host='202.54.10.20' where Db='webdb';
update user set Host='202.54.10.20' where user='webadmin';

if you install mysql run this command please…

mysql_secure_installation

what does it do?

asks you set a password for root account
remove root accounts that are accessible from outside the local host.
remove anonymous-user accounts.
remove the test database, which by default can be accessed by anonymous users.

see… it’s important

here’s what happens

me@ubuntop:~$ mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we’ll need the current
password for the root user. If you’ve just installed MySQL, and
you haven’t set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on…

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

You already have a root password set, so you can safely answer ‘n’.

Change the root password? [Y/n] n
… skipping.

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
… Success!

Normally, root should only be allowed to connect from ‘localhost’. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
… Success!

By default, MySQL comes with a database named ‘test’ that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
– Dropping test database…
ERROR 1008 (HY000) at line 1: Can’t drop database ‘test’; database doesn’t exist
… Failed! Not critical, keep moving…
– Removing privileges on test database…
… Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
… Success!

Cleaning up…

All done! If you’ve completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

Yes I admit I have mangled MySql on my macbook very badly and it had to go…. here is what it took to do it

sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
edit /etc/hostconfig and remove the line MYSQLCOM=-YES-
rm -rf ~/Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*

Stop Mysql:
/etc/init.d/mysqld stop

Create a script to run the reset query
vi /root/mysql.reset.sql

add the reset query:
UPDATE mysql.user SET Password=PASSWORD(‘YOUR-NEW-MYSQL-PASSWORD’) WHERE User=’root’;
FLUSH PRIVILEGES;

execute the script:
# mysqld_safe –init-file=/root/mysql.reset.sql &

Sample output:
nohup: ignoring input and redirecting stderr to stdout
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[20970]: started

# killall mysqld
# /etc/init.d/mysql start

How to read & update MySQL table column comments
So why would you wanna do that? Cuz you can store info in the comments that can be read out and feed automated CMS systems

SELECT COLUMN_COMMENT
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'simpl_example' AND TABLE_NAME = 'your_table_name';

When you need to restrict users to a specific database do this….
log in as a/the admin user:

mysql -u root -h localhost -p

(enter your password)

then run these commands:

CREATE Database databasename;
CREATE USER ‘username’@’%’ IDENTIFIED BY ‘password’;
REVOKE ALL PRIVILEGES,GRANT OPTION from ‘username’@’%’;
GRANT ALL ON databasename.* TO ‘username’@’%’;

Line 1 creates a database named: databasename (mmmm… insert the database name you want here)
Line 2 creates a user named username and gives them a password
Line 3 Takes a way all username privileges
Line 4 gives username all privileges on all the tables in databasename

Many applications store MD5-crypted passwords in the database. If you want to quickly create a new MD5-ed password, or you have forgotten your password, use the following query to get a new one:

SELECT MD5(”m0ntypy7hon”);

This query will return “4f249cbc2c10d41f866b64decd365e39″ which is the encrypted version of the string “ m0ntypy7hon”.

There are other function that crypt stings in MySQL using different algorithms, most notably PASSWORD() which is using MySQL”s own crypting algorithm.

SELECT PASSWORD( “m0ntypy7hon” ) ;

returns: “5216be9f6ead4434”

UPDATE yourtable SET targetfield = REPLACE(targetfield, “foo”,“bar”);

This statement will replace all occurrences of the string “foo” with the string “bar” in all records of the “ targetfield” column. Apart from the string “bar” the rest of the text contained in the field will be unchanged.

UPDATE `my_table` SET `my_field` = (SELECT CASE `my_field` WHEN ‘1’ THEN ‘0’ ELSE ‘1’ END) WHERE `my_table_ID_Field` = ‘1’

UPDATE `my_table` SET `my_other_field` = (SELECT CASE `my_other_field` WHEN ‘foo’ THEN ‘bar’ ELSE ‘foo’ END) WHERE `my_table_ID_Field` = ‘1’

Here is the test table

CREATE TABLE `my_table` (
  `my_table_ID_Field` int(11) NOT NULL auto_increment,
  `my_field` int(11) NOT NULL,
  `my_other_field` varchar(64) NOT NULL,
  PRIMARY KEY (`my_table_ID_Field`)
);

——Dumping data for table `my_table`—

INSERT INTO `my_table` VALUES(1, 0, ‘foo’);