MySQL: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 4: | Line 4: | ||
</pre> | </pre> | ||
==Check/repair/optimize== | |||
<pre> | |||
mysqlcheck -c DBNAME | |||
mysqlcheck -r DBNAME | |||
mysqlcheck -o DBNAME | |||
</pre> | |||
==Repair all databases on the server== | |||
<pre> | |||
mysqlcheck -A --auto-repair | |||
</pre> | |||
==Slow Query Enable== | |||
<pre> | |||
cp /etc/my.cnf /etc/my.cnf-bak | |||
touch /var/log/mysql-slow.log | |||
chown mysql:root /var/log/mysql-slow.log | |||
chmod 640 /var/log/mysql-slow.log | |||
</pre> | |||
Then Add the following to the [mysqld] section of /etc/my.cnf | |||
<pre> | |||
slow_query_log= 1 | |||
slow_query_log_file="/var/log/mysql-slow.log" | |||
long_query_time = 60; | |||
</pre> | |||
Restart MySQL | |||
<pre> | |||
/usr/local/cpanel/scripts/restartsrv_mysql | |||
</pre> | |||
==General query log== | |||
<pre> | |||
cp /etc/my.cnf /etc/my.cnf-bak | |||
touch /var/log/mysql-general.log | |||
chown mysql:root /var/log/mysql-general.log | |||
chmod 640 /var/log/mysql-general.log | |||
</pre> | |||
Then, Add to the [mysqld] section of /etc/my.cnf | |||
<pre> | |||
general_log=1 | |||
general_log_file="/var/log/mysql-general.log" | |||
</pre> | |||
Restart MySQL the end: | |||
<pre> | |||
/usr/local/cpanel/scripts/restartsrv_mysql | |||
</pre> | |||
==Database Export== | ==Database Export== | ||
| Line 79: | Line 130: | ||
delete from tablemame where Column_ID between 200 and 25134; | delete from tablemame where Column_ID between 200 and 25134; | ||
</pre> | </pre> | ||
==Strict mode== | |||
===Check Strict Mode=== | |||
<pre> | |||
mysql -e 'select @@GLOBAL.sql_mode;' | |||
mysql -u root -e "SHOW VARIABLES LIKE 'sql_mode';" | |||
</pre> | |||
It will show output like this: | |||
<pre>STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION </pre> | |||
===Disable Strict Mode=== | |||
To disable strict mode, first check the strict mode as shown above, then copy the output and add it to the my.cnf without STRICT_TRANS_TABLES | |||
<pre> | |||
sql_mode="ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" | |||
</pre> | |||
At the end, restart mysql: | |||
<pre>/usr/local/cpanel/scripts/restartsrv_mysql</pre> | |||
==Database Map Tool== | |||
If database exists in /var/lib/mysql/ but not in cPanel, map the db to the user in WHM >> Database Map Tool or: | |||
<pre> | |||
/usr/local/cpanel/bin/dbmaptool USER --type 'mysql' --dbs 'DBNAME' | |||
</pre> | |||
If you get "db is already owned by user", run this and then the above again: | |||
<pre>/usr/local/cpanel/bin/dbindex</pre> | |||
Latest revision as of 10:48, 20 June 2024
Check MySQL Version
mysql -V
Check/repair/optimize
mysqlcheck -c DBNAME mysqlcheck -r DBNAME mysqlcheck -o DBNAME
Repair all databases on the server
mysqlcheck -A --auto-repair
Slow Query Enable
cp /etc/my.cnf /etc/my.cnf-bak touch /var/log/mysql-slow.log chown mysql:root /var/log/mysql-slow.log chmod 640 /var/log/mysql-slow.log
Then Add the following to the [mysqld] section of /etc/my.cnf
slow_query_log= 1 slow_query_log_file="/var/log/mysql-slow.log" long_query_time = 60;
Restart MySQL
/usr/local/cpanel/scripts/restartsrv_mysql
General query log
cp /etc/my.cnf /etc/my.cnf-bak touch /var/log/mysql-general.log chown mysql:root /var/log/mysql-general.log chmod 640 /var/log/mysql-general.log
Then, Add to the [mysqld] section of /etc/my.cnf
general_log=1 general_log_file="/var/log/mysql-general.log"
Restart MySQL the end:
/usr/local/cpanel/scripts/restartsrv_mysql
Database Export
Backup All Databases
mysqldump --all-databases > backup.sql
Backup database using User
mysqldump -u dbuser -p dbname > dbname.sql
Exclude a table from MySQL Dump
--ignore-table=dbname.tablename Syntax: mysqldump -u DBuser -p DBname --ignore-table=DBname.tablename > dbname.sql
Export Single Table
mysqldump -u username -p database-name table-name > backup.sql
Export Table Structure Only
mysqldump -u DBUser -p --no-data dbname > schema.sql
Import Databases
Import a database
mysql -u DBuser -p DBname < dbdump.sql
Import Zip backup
gunzip < Backup.sql.gz | mysql -u USER -p DATABASE
Import .tar.gz gzipped
tar -xzOf your_db_dump.sql.tar.gz | mysql -u USERNAME -pPASSWORD your_database
Check the size of tables
SELECT table_name AS "Table", ROUND(((data_length + index_length) / 1024 / 1024), 2) AS "Size (MB)" FROM information_schema.TABLES WHERE table_schema = "database_name" ORDER BY (data_length + index_length) DESC;
Check MySQL Max Connections
SHOW VARIABLES LIKE "max_connections";
cPanel showing incorrect MySQL size
/scripts/update_db_cache
Delete Selected Rows
mysql use DBName; delete from tablemame where Column_ID between 200 and 25134;
Strict mode
Check Strict Mode
mysql -e 'select @@GLOBAL.sql_mode;' mysql -u root -e "SHOW VARIABLES LIKE 'sql_mode';"
It will show output like this:
STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Disable Strict Mode
To disable strict mode, first check the strict mode as shown above, then copy the output and add it to the my.cnf without STRICT_TRANS_TABLES
sql_mode="ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
At the end, restart mysql:
/usr/local/cpanel/scripts/restartsrv_mysql
Database Map Tool
If database exists in /var/lib/mysql/ but not in cPanel, map the db to the user in WHM >> Database Map Tool or:
/usr/local/cpanel/bin/dbmaptool USER --type 'mysql' --dbs 'DBNAME'
If you get "db is already owned by user", run this and then the above again:
/usr/local/cpanel/bin/dbindex