Delete all ID's in a table except a few
DELETE FROM <tablename> WHERE id NOT IN (58,42);
A sane cPanel my.cnf
######################################################################################
###settings from http://forums.cpanel.net/f402/mysql-high-cpu-usage-374152.html
#####################################################################################
[mysqld]
port=3306
socket="/var/lib/mysql/mysql.sock"
bind-address = 127.0.0.1
skip-name-resolve
myisam_use_mmap=1
max_connections= 100
max_user_connections = 50
#log-slow-queries=mysql-slow.log
long_query_time = 0.1
#safe-show-database
query_cache_type = 1
query_cache_size = 75M
query_cache_limit = 2M
join_buffer_size=1M
read_rnd_buffer_size=1M
max_allowed_packet=268435456
tmp_table_size=25M
max_heap_table_size=25M
table_open_cache = 7500
thread_cache_size = 25
open_files_limit=30000
key_buffer_size = 500M
myisam_sort_buffer_size = 256M
innodb_file_per_table=1
innodb_buffer_pool_size = 1G
innodb_log_file_size = 64M
innodb_log_buffer_size = 8M
Recreate root user
http://www.helpfromfriend.com/database/mysql/how-to-recreate-root-account-in-mysql/
The pre requisition is a shell access on your MySQL machine. Then you have to stop the standard mysql daemon and start the database in safe mode.
/etc/init.d/mysql stop mysqld_safe --skip-grant-tables &
Then log in as a root and switch to mysql system database.
mysql -u root mysql> use mysql;
Try to check that the root user is not present in user table:
mysql> select * from user where User='root';
If the database return empty record, lets manually insert the root user with empty password and then set all the permissions which he normally needs:
mysql> insert into user (Host, User, Password) values ('localhost','root',''); Query OK, 1 rows affected (0.04 sec) mysql> update user set Select_priv='Y',Insert_priv='Y',Update_priv='Y',Delete_priv='Y',Create_priv='Y',Drop_priv='Y',Reload_priv='Y',Shutdown_priv='Y',Process_priv='Y',File_priv='Y',Grant_priv='Y',References_priv='Y',Index_priv='Y',Alter_priv='Y',Show_db_priv='Y',Super_priv='Y',Create_tmp_table_priv='Y',Lock_tables_priv='Y',Execute_priv='Y',Repl_slave_priv='Y',Repl_client_priv='Y',Create_view_priv='Y',Show_view_priv='Y',Create_routine_priv='Y',Alter_routine_priv='Y',Create_user_priv='Y' where user='root'; Query OK, 1 rows affected (0.03 sec)
Then quit the database console, kill the mysqld_safe daemon and start the standard mysql daemon again:
mysql> quit killall mysqld_safe /etc/init.d/mysql start
Try to log in into mysql console again with an empty password and for double check, try to run ‘grant’ command to see that the account is fully working:
mysql -u root mysql> grant all privileges on *.* to 'root'@'localhost' with grant option; Query OK, 1 rows affected (0.03 sec)
And thats it, your basic root account is working again. Don’t forget to change the password or add the hostname to it, but that depends on your needs.
Upgrading from 5.0 to 5.5
Backup existing data
mysqldump -uroot -pxxx --routines --triggers --databases asterisk faxout_dispatch faxout_render faxplatform flatfax gaxme_website ifaxyou opensips test web_yabroo_www > /data/backups/mysql/OldData.sql
mysql -uroot -pxxx --skip-column-names -A -e"SELECT CONCAT('SHOW GRANTS FOR ''',user,'''@''',host,''';') FROM mysql.user WHERE user<>''" | mysql -uroot -pxxx --skip-column-names -A | sed 's/$/;/g' > /data/backups/mysql/MySQLGrants.sql
Stop and remove existing mysql
/etc/init.d/mysqld stop
mv /etc/my.cnf /etc/my.cnf1
rm -rf /var/lib/mysql/*
rpm -qa | grep mysql
yum remove mysql-server-5.0.77-4.el5_6.6 mysql-5.0.77-4.el5_6.6 mysql-5.0.77-4.el5_6.6
Install using remi repos
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
yum --enablerepo=remi,remi-test install mysql-libs.x86_64 mysql.x86_64 mysql-server.x86_64
mv /etc/my.cnf1 /etc/my.cnf
service mysqld restart
Import backups
mysql -u #username# -p #database# < #dump_file#
mysql -uroot -pxxxx
source /data/backups/mysql/MySQLGrants.sql
source /data/backups/mysql/OldData.sql
-------------------------------------
/etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
#datadir=/data/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
log-slow-queries = /var/log/mysql/slowquery.log
long_query_time = 20
log-queries-not-using-indexes
query_cache_size = 52428800
query_cache_type = 1
max_allowed_packet=32M
wait_timeout=600
interactive_timeout=600
server-id = 1
replicate-same-server-id = 0
auto-increment-increment = 2
auto-increment-offset = 1
innodb_buffer_pool_size=256M
log-bin = /var/log/mysql/mysql-bin.log
binlog-do-db = opensips
binlog-do-db = faxout_dispatch
binlog-do-db = faxout_render
binlog-do-db = faxplatform
binlog-do-db = faxstats
binlog-do-db = flatfax
relay-log = /var/lib/mysql/slave-relay.log
relay-log-index = /var/lib/mysql/slave-relay-log.index
expire_logs_days = 10
max_binlog_size = 100M
innodb_lock_wait_timeout = 500
# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
Timeouts - connection errors
check interactive_timeout which will set wait timeout
check max_connections
show variables like 'max%';
show variables like 'wait%';
show variables like '%timeout%';
SHOW GLOBAL VARIABLES LIKE '%timeout%';
select @@session.wait_timeout;
SHOW GLOBAL VARIABLES LIKE '%timeout%';
SHOW GLOBAL VARIABLES LIKE 'max%';
SHOW GLOBAL VARIABLES LIKE '%timeout%';
Fix replication
mysql> STOP SLAVE;
mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;
mysql> START SLAVE;
These instructions apply to simple case of one slave replicating from one master.
Prepare the slave
Prepare the master, copy databases and restart master
Restart Replication Slave
SSH into the slave server.
Log into MySQL on the slave as root
mysql>STOP SLAVE;
mysql>RESET SLAVE;
mysql> exit;
Now exit and shutdown slave
$ mysqladmin shutdown -u root -p
Delete the databases on slave (DANGER … this deletes ALL databases on slave)
$ cd /usr/local/mysql/
$ sudo rm -r data
SSH into the master as root
log into mysql as root
Check users and kick them all off if more than yourself is connected
mysql> show processlist;
Reset the Master
mysql> RESET MASTER;
exit and shutdown immediately
$ mysqladmin shutdown -p
Now copy all the data from this master to the slave (may take a while)
# cd /usr/local/mysql
# scp -r data root@slavehost:/usr/local/mysql
Note: If you have a really huge amount of data and you have the drive space, it may be faster to make a local copy of the data directory on the master which can be then copied to the slave after you have restarted the master.
When copying is done, restart the master
$ sudo echo
$ sudo mysqld_safe &
Log into master and make sure it is logging.
mysql> show master status\G
*************************** 1. row ***************************
File: binary-log.002
Position: 280Binlog_do_db:
Binlog_ignore_db:
1 row in set (0.00 sec)
Make sure Position is increasing if insert and update activity is taking place on master.
Now you can breathe for a while!
Log into slave server and fix privileges on the data folder
$ cd /usr/local/mysql
$ sudo chown -R mysql. data
Restart server
$ sudo echo
$ sudo mysqld_safe &
Log into mysql as root
mysql> SHOW SLAVE STATUS\G;
Verify that it is replicating and you are done.
Error 37
following entries appear in the .err file:
InnoDB: Unable to lock ./ibdata1, error: 37
Solution:
mount -t nfs -o nolock IP:/data /data
Replication
http://www.howtoforge.com/mysql_database_replication
Grant remote read access to an IP range
GRANT SELECT ON db.* TO me@'164.88.15.0/255.255.255.0' IDENTIFIED BY 'pass';
To prepend and append text to a text table:
Prepend:
mysql> update pictures set caption=concat( "FEE BEARING" ,caption ) where id='846437';
Query OK, 1 row affected (0.09 sec)
Rows matched: 1 Changed: 1 Warnings: 0
Append:
mysql> update pictures set caption=concat( caption, "FEE BEARING" ) where id='844405';
Query OK, 1 row affected (0.14 sec)
Rows matched: 1 Changed: 1 Warnings: 0
Setting, Changing And Resetting MySQL Root Passwords
By SamTzu
Created 2009-01-16 19:16
Setting, Changing And Resetting MySQL Root Passwords
This tutorial explains how you can set, change and reset (if you've forgotten the password) MySQL root passwords. Time and again I see problems like mysqladmin: connect to server at 'localhost' failed error: 'Access denied for user 'root'@'localhost' (using password: YES)'. So I thought it's time to remind you how to solve MySQL related password problems. If you are just looking for a quick fix how to reset a MySQL root password you can find that at the bottom of this tutorial.
mysqladmin Command To Change Root Password
Method 1 - Set up root password for the first time
If you have never set a root password for MySQL, the server does not require a password at all for connecting as root. To set up a root password for the first time, use the mysqladmin command at the shell prompt as follows:
$ mysqladmin -u root password newpass
If you want to change (or update) a root password, then you need to use the following command:
$ mysqladmin -u root -p oldpassword newpass
Enter password:
If you get...
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: YES)'
then follow the instructions below on how to recover your MySQL password.
Change MySQL password for other users
To change a normal user password you need to type:
$ mysqladmin -u user-name -p oldpassword newpass
Method 2 - Update or change password
MySQL stores usernames and passwords in the user table inside the MySQL database. You can directly update a password using the following method to update or change passwords:
1) Login to the MySQL server, type the following command at the shell prompt:
$ mysql -u root -p
2) Use the mysql database (type commands at the mysql> prompt):
mysql> use mysql;
3) Change password for a user:
mysql> update user set password=PASSWORD("newpass") where User='ENTER-USER-NAME-HERE';
4) Reload privileges:
mysql> flush privileges;
mysql> quit
This method you need to use while using PHP or Perl scripting.
Recover MySQL root password
You can recover a MySQL database server password with the following five easy steps:
Step # 1: Stop the MySQL server process.
Step # 2: Start the MySQL (mysqld) server/daemon process with the --skip-grant-tables option so that it will not prompt for a password.
Step # 3: Connect to the MySQL server as the root user.
Step # 4: Set a new root password.
Step # 5: Exit and restart the MySQL server.
Here are the commands you need to type for each step (log in as the root user):
Step # 1 : Stop the MySQL service:
# /etc/init.d/mysql stop
Output:
Stopping MySQL database server: mysqld.
Step # 2: Start the MySQL server w/o password:
# mysqld_safe --skip-grant-tables &
Output:
[1] 5988
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[6025]: started
Step # 3: Connect to the MySQL server using the MySQL client:
# mysql -u root
Output:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 4.1.15-Debian_1-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
Step # 4: Set a new MySQL root user password:
mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit
Step # 5: Stop the MySQL server:
# /etc/init.d/mysql stop
Output:
Stopping MySQL database server: mysqld
STOPPING server from pid file /var/run/mysqld/mysqld.pid
mysqld_safe[6186]: ended
[1]+ Done mysqld_safe --skip-grant-tables
Start the MySQL server and test it:
# /etc/init.d/mysql start
# mysql -u root -p
Copyright © 2009 Sami
All Rights Reserved.
Source URL:
http://www.howtoforge.com/setting-chang ... -passwords
Commands
This is a list of handy MySQL commands that I use time and time again. At the bottom are statements, clauses, and functions you can use in MySQL. Below that are PHP and Perl API functions you can use to interface with MySQL. To use those you will need to build PHP with MySQL functionality. To use MySQL with Perl you will need to use the Perl modules DBI and DBD::mysql.
Below when you see # it means from the unix shell. When you see mysql> it means from a MySQL prompt after logging into MySQL.
To login (from unix shell) use -h only if needed.
# [mysql dir]/bin/mysql -h hostname -u root -p
Create a database on the sql server.
mysql> create database [databasename];
List all databases on the sql server.
mysql> show databases;
Switch to a database.
mysql> use [db name];
To see all the tables in the db.
mysql> show tables;
To see database's field formats.
mysql> describe [table name];
To delete a db.
mysql> drop database [database name];
To delete a table.
mysql> drop table [table name];
Show all data in a table.
mysql> SELECT * FROM [table name];
Returns the columns and column information pertaining to the designated table.
mysql> show columns from [table name];
Show certain selected rows with the value "whatever".
mysql> SELECT * FROM [table name] WHERE [field name] = "whatever";
Show all records containing the name "Bob" AND the phone number '3444444'.
mysql> SELECT * FROM [table name] WHERE name = "Bob" AND phone_number = '3444444';
Show all records not containing the name "Bob" AND the phone number '3444444' order by the phone_number field.
mysql> SELECT * FROM [table name] WHERE name != "Bob" AND phone_number = '3444444' order by phone_number;
Show all records starting with the letters 'bob' AND the phone number '3444444'.
mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444';
Show all records starting with the letters 'bob' AND the phone number '3444444' limit to records 1 through 5.
mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444' limit 1,5;
Use a regular expression to find records. Use "REGEXP BINARY" to force case-sensitivity. This finds any record beginning with a.
mysql> SELECT * FROM [table name] WHERE rec RLIKE "^a";
Show unique records.
mysql> SELECT DISTINCT [column name] FROM [table name];
Show selected records sorted in an ascending (asc) or descending (desc).
mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;
Return number of rows.
mysql> SELECT COUNT(*) FROM [table name];
Sum column.
mysql> SELECT SUM(*) FROM [table name];
Join tables on common columns.
mysql> select lookup.illustrationid, lookup.personid,person.birthday from lookup left join person on lookup.personid=person.personid=statement to join birthday in person table with primary illustration id;
Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs.
# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'));
mysql> flush privileges;
Change a users password from unix shell.
# [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p password 'new-password'
Change a users password from MySQL prompt. Login as root. Set the password. Update privs.
# mysql -u root -p
mysql> SET PASSWORD FOR 'user'@'hostname' = PASSWORD('passwordhere');
mysql> flush privileges;
Recover a MySQL root password. Stop the MySQL server process. Start again with no grant tables. Login to MySQL as root. Set new password. Exit MySQL and restart MySQL server.
# /etc/init.d/mysql stop
# mysqld_safe --skip-grant-tables &
# mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("newrootpassword") where User='root';
mysql> flush privileges;
mysql> quit
# /etc/init.d/mysql stop
# /etc/init.d/mysql start
Set a root password if there is on root password.
# mysqladmin -u root password newpassword
Update a root password.
# mysqladmin -u root -p oldpassword newpassword
Allow the user "bob" to connect to the server from localhost using the password "passwd". Login as root. Switch to the MySQL db. Give privs. Update privs.
# mysql -u root -p
mysql> use mysql;
mysql> grant usage on *.* to bob@localhost identified by 'passwd';
mysql> flush privileges;
Give user privilages for a db. Login as root. Switch to the MySQL db. Grant privs. Update privs.
# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES ('%','databasename','username','Y','Y','Y','Y','Y','N');
mysql> flush privileges;
or
mysql> grant all privileges on databasename.* to username@localhost;
mysql> flush privileges;
To update info already in a table.
mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',Update_priv = 'Y' where [field name] = 'user';
Delete a row(s) from a table.
mysql> DELETE from [table name] where [field name] = 'whatever';
Update database permissions/privilages.
mysql> flush privileges;
Delete a column.
mysql> alter table [table name] drop column [column name];
Add a new column to db.
mysql> alter table [table name] add column [new column name] varchar (20);
Change column name.
mysql> alter table [table name] change [old column name] [new column name] varchar (50);
Make a unique column so you get no dupes.
mysql> alter table [table name] add unique ([column name]);
Make a column bigger.
mysql> alter table [table name] modify [column name] VARCHAR(3);
Delete unique from table.
mysql> alter table [table name] drop index [colmn name];
Load a CSV file into a table.
mysql> LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE [table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1,field2,field3);
Dump all databases for backup. Backup file is sql commands to recreate all db's.
# [mysql dir]/bin/mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql
Dump one database for backup.
# [mysql dir]/bin/mysqldump -u username -ppassword --databases databasename >/tmp/databasename.sql
Dump a table from a database.
# [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql
Restore database (or database table) from backup.
# [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql
Create Table Example 1.
mysql> CREATE TABLE [table name] (firstname VARCHAR(20), middleinitial VARCHAR(3), lastname VARCHAR(35),suffix VARCHAR(3),officeid VARCHAR(10),userid VARCHAR(15),username VARCHAR(8),email VARCHAR(35),phone VARCHAR(25), groups VARCHAR(15),datestamp DATE,timestamp time,pgpemail VARCHAR(255));
Create Table Example 2.
mysql> create table [table name] (personid int(50) not null auto_increment primary key,firstname varchar(35),middlename varchar(50),lastnamevarchar(50) default 'bato');
MYSQL HTML Reports
> maybe you can send me a dumbed down script for mysql??
for sure
actually the first thing to do is use mutt command line to send HTML email
mutt -e 'set content_type="text/html"' -s "$subject" $to
but then your commands you piping into this baby you start with
echo "<pre>"
which is HTML for "preformatted"
and your mysql commands lash -H to format output as HTML
mysql -H ....
and then you can lash headings and stuff - any HTML as you please eg. <b> to make some stuff bolf, italics, different colours, sizes, including images - whatever
echo "<h2>jaaaaa</h2>"
the easiest way to include images like logo is if those are URL offsite your site or some site