MYSQL Optimize Statement - mysql

I am trying to use the optimize statement on the Shell of the MYSQL system and have tried using
mysqlcheck -o --all-databases;
mysqlcheck -o <databasename>;
However, it does not work and showed me an error,
Is there any other command that could make it work? I am running the script on xampp shell for mysql and would like to just check the optimization for the table. I know that there would errors for it however I would like to view it.

mysqlcheck is a command to give to the OS via its "shell". It is not a command to use inside the "commandline tool" mysql; that would be OPTIMIZE TABLE as mentioned in a Comment.
But... Why do you think you want to Optimize all the tables. While the name "optimize" is tempting, it is rarely worth bothering with.

Related

MySQL Optimize Table script

I am in the process of upgrading our AWS Aurora RDS cluster from MySQL 5.7 to MySQL 8.0. During the upgrade of the cluster, the process failed, and according to AWS, they recommended we run Optimize Table on a number of DB tables before proceeding again. I was able to generate a list of tables that need to be optimized by running a script from AWS, but there are over 1200 tables that need this to be run on. I am not very experienced with SQL scripting, but I have used Powershell and Bash scripting before many times.
What is the best way to script this out, so I can provide a list of tables in a text file or a db table then run Optimize Table on each row or line.
I have done similar scripts in Bash or Powershell as For Each loops and have the tables listed in a text file, but I am not sure how to do a similar process in SQL. Any help is appreciated.
Thanks!
Output list of tables formatted into OPTIMIZE TABLE statements:
mysql -B -N -e "select concat('optimize table ',table_schema,'.',table_name,';')
from information_schema.tables where table_type='BASE TABLE' AND ..." > myscript.sql
Add any conditions you want where I put ..., to filter the tables you want to list. For example: AND TABLE_SCHEMA='myschema'.
Run that result as an SQL script:
mysql -e "source myscript.sql"
(I omitted options for --hostname, --user, and --password or any other options you may need to use to connect to your instance. I usually put these into ~/.my.cnf anyway.)

How to access data from a SQL snapshot

I have been given a SQL database snapshot in a file sqlfile.sql
I want to access these data, but I do not know how to proceed. I am new to SQL.
I have a Macbook pro with MacOSX Sierra (10.12) and I have installed mysql with Homebrew. The version of mysql is 8.0.16
running mysql works, I can access the mysql prompt line.
I would like to be able to access the data, in python if possible, but if it has to be through the mysql command line, it is fine. Once I can access the tables, I know how to query the data, that is not any problem.
I tried with MySQL Workbench as well, but it does not work either.
Can someone point me towards some guide on how to proceed? I have spend hours trying to find some clue, but I did not succeed.
Thanks a lot!!!
Finally I managed to solve the problem. It was not really complicated, but I had problems finding the right combination of commands.
What I did was, in the command line enter into the mysql prompt. Once here:
mysql> create database mydatabase
Once done, go outside the mysql prompt and in the normal command line execute:
mysql -uroot -p mydatabase < db_snapshot_file.sql
This populates the database called mydatabase. Once done, from Python I could access it. But first, since I had some problem with the authentication, I had to use this answer to solve it.
To access from Python, I used this information which gave me some indications on how to start.

Why is MySQLSlap giving me additional prompt instead of running?

I am trying to benchmark some queries using mysqlslap. I've never used it before. I am trying to follow some tutorials and am trying to run a simple command to get a hang of how it works like from this tutorial.
When I run the command mysqlslap --concurrency=20 --iterations=4 --query="sELCT * FROM listusers;" --create-schema=my_database instead of actually testing and returning some metrics, I just get another prompt like
I have tried this command with username and password fields as well with the same results.
What is this prompt and how do I get the command to actually run the metrics?
mysqlslap is not a MySQL command, but a separate binary tool.
So instead of trying to use it inside mysql console, use it directly in bash/shell.

Scheduled MySQL optimization

I am looking for a way to schedule a database table optimisation on all MySQL data.
Currently I can do it by using the mysqlcheck -o --all-databases command, and I thought about scheduling it with cron, but the problem is that the password would remain cleartext inside /etc/crontab.
There is a scheduling feature in MySQL, but I don't know how it could fit the need, it sends MySQL commands, it doesn't launch shell programs.
Any tips?
Can use cron, use the --defaults-extra-file option to specify a file, in my.cnf format. It can contain the username/password needed.
Use standard linux permissions to make it only readable to the user running the cron task.
See third bullet-point here: http://dev.mysql.com/doc/refman/5.1/en/password-security-user.html

MySQL: How to import a sql dump.. inside mysql command line?

I often keep a shell open to a remote server where I'm spending a lot of time in mysql. Exiting mysql and logging back in seems like a pain I shouldn't have to deal with if I just want to run a sql file.
When I'm running MySql from the command line, how can I run a dump file?
Right now, I'm using the same approach outlined in this post.
mysql> use db_name;
mysql> source backup-file.sql;
You may want to take a look at this answer: https://stackoverflow.com/a/17666285