MYSQL SHELL - SQL MODE from SHELL SCRIPT - mysql

I am writing a shell script to run a sql select query using mysqlsh.
So I had some issues and I found that I need to use "\sql" to change it from
mysql-js> to mysql-sql>
I need to run /sql before I run the query from a file.
"C:\mysql-shell-1.0.11-windows-x86-64bit\bin\mysqlsh.exe" echo \sql | mysql://user#IP:PORT?schema=SCHEMANAME&password=PASSWORD
and then execute this to run the select query and save it to a tmpfile.txt
"C:\mysql-shell-1.0.11-windows-x86-64bit\bin\mysqlsh.exe" --file "$EDIR"/data/tmp.sql > "$EDIR"/data/tmpfile.txt 2>&1
I am not sure what am missing. I tried different alternatives,
I am getting the below error on the first line
"data_extractss.sh: line 53: mysql://user#IP:PORT?schema=SCHEMANAME&password=PASSWORD: No such file or directory"
Any suggestion is appreciated.

First, please upgrade to the latest version of the shell 8.0.12
Second, start the shell like 'mysqlsh --sql' or 'mysqlsh --sqlc' to have it start in SQL mode

Related

Error executing task write() argument must be str, not bytes | MySQL Workbench

After a clean install of Windows 10 when I started to use MySQL workbench I have this error when I try to dump a database.
I could not find anything about it anywhere, the only thing I find are problems with phyton, but nothing related with Workbench.
Output after dumping a database:
08:29:00 Dumping foo(all tables)
Error executing task write() argument must be str, not bytes
08:29:01 Export of b'C:\\Users\\erick\\Documents\\dumps\\Dump20210121 (2).sql' has finished
And the output file is in blank, some ideas?
You can disable (uncheck) the option create schema. It is annoying but it works.
If you have the latest MySQL Workbench (8.0.23), just uninstall and install MySQL Workbench 8.0.20. That worked for me.
Another workaround to this issue is to edit wb_admin_export.py.
In dump_to_folder (line 1679) replace
self.out_pipe = open(path,"w")
with
self.out_pipe = open(path,"wb")
In dump_to_file (line 1957) replace
self.out_pipe = open(self.path,"w")
with
self.out_pipe = open(self.path,"wb")
If you a using version 8.0.23, update to 8.0.25 or above.... it will fix this issue

Syntax error received when trying to source a file from mysql command line

I have a file that is filled with statements that appear like the following:
update table set column1=NULL where column2 in (select id from users where user_id in ('user1','user2'));
As you can see this is correct syntax. When I run this command manually on the mysql command line it works as intended.
However when I attempt to source the file I get syntax errors. Has anyone seen this issue before? I'm looking for a resolution...
mysql> \! source /tmp/update_users.sql
/tmp/update_users.sql: line 2: syntax error near unexpected token `('
/tmp/update_users.sql: line 2: `(select id from users where user_id in ('user1','user2'));'
You have to use the MySQL source command:
mysql> source /tmp/update_users.sql
When you use \!, you instead invoke a shell command. The shell source command expects a shell script, not a sql file.

Laravel 5.1 - `php artisan migrate` is giving 'PDOException'

I know this is a duplicate question however whatever I tried, unfortunately no luck. (To add, I am using MAMP)
I believe I successfully setup the database connection because, using the registration form (Auth/register), after submitting the form, I was getting the error PDO Exception: Driver not found before, but after changing to my MAMP to PHP 5.5.17 from the default(PHP 5.6.1), I started getting error: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel.users' doesn't exist, which I believe shows that the database connection is working and it's just the 'users table' is not created.
Here starts my problem. When I try to use php artisan migrate or migrate:{anything} in the terminal, it's throwing the error:
[PDOException]
could not find driver
But what I want to do is to use php artisan migrate to be able to create tables and migrate them.
Things I have tried:
I added <?php echo phpinfo(); ?> to if pdo files are successfully installed.
I get the result:
so I think it seems okay.
People were talking about opening /MAMP/bin/php/php5.6.1/conf/php.ini and adding extension=pdo_mysql.so in the code, but I have mine already placed there.
When I try php -i | grep PDO in the project root (/MAMP/htdocs/proj), I got:
PDO
PDO support => enabled
PDO drivers => sqlite
PDO Driver for SQLite 3.x => enabled
php -i | grep Conf and `php --ini also outputs:
Configuration File (php.ini) Path: /Applications/MAMP/bin/php/php5.6.1/conf
Loaded Configuration File: /Applications/MAMP/bin/php/php5.6.1/conf/php.ini but php artisan migrate still throwing the same error
SOLUTION:
I chose MAMP to use version 5.5.17 instead of 5.6.1 in the browser (from MAMP Preferences), however this time terminal was running the 5.6.1. What I did was first checking which PHP command and then, running the nano ~/.bash_profile command; and editing the version document. Now everything is up and running :)
What you see in the browser is that you have PDO enabled for PHP running through apache. But this does not mean you have it enabled for PHP running through CLI (in fact they are using two separate ini files). To confirm that you can try:
php -i | grep PDO
And you will see it is missing or not enabled. So what you need to do is to find which php.ini is using the PHP running through CLI and add the PDO module there:
php -i | grep Conf
It will output something like:
Configuration File (php.ini) Path => /etc/php5/cli
Loaded Configuration File => /etc/php5/cli/php.ini

can not run a sql file windows command line using SOURCE command

hi i am using windows 7, xampp
. i am trying to run a sql file using CMD ut it gives me an error
ERROR:
Unknown command '\x'.
this is what i am doing
this command is not working
mysql> SOURCE C:\xampp\htdocs\elephanti2\db\mas_gis_city_ip.sql;
*** one other problem with this works fine when i tried by go to phpmyadmin and import the file .
why this happening , i have no idea , please help ..................
My original answer:
mysql> SOURCE C:\\xampp\\htdocs\\elephanti2\\db\\mas_gis_city_ip.sql;
You could also try executing the command like this:
mysql> SOURCE C:/xampp/htdocs/elephanti2/db/mas_gis_city_ip.sql;
(Source: a comment in the reference manual suggests using forward slashes on Windows machines)
Try quoting your file name. Notice your command:
mysql> SOURCE C:\xampp\htdocs...
is probably being interpreted as:
mysql SOURCE C: \xampp \htdocs...
(see the \x there?)
If you quote the file name, I bet it'll work. (not sure if mysql requires ' or " for quoting filenames in this context, try both)
Actually this worked. I think it is dwuff's previous answer.
mysql> SOURCE C:\\xampp\\htdocs\\elephanti2\\db\\mas_gis_city_ip.sql;
I had the same problem recently trying to install FossTrak, and this was the syntax that worked on windows 10, with Mysql 5.7
\. C:/share/epcis-repository-0.5.0/epcis_schema.sql
using \source did not, and neither did \\, and by accident i removed the ; and it worked.

How can I check (My)SQL statements for syntactical correctness

we're currently setting up out integration server and during that process we've set up pre-commit hooks on the SVN so that our developers can't check in files that are syntactically invalid (primarily PHP and XML).
We also have a bunch of .sql files (for MySQL) which I'd like to lint as well. Unfortunately, Google didn't turn up anything useful for this task.
Any ideas?
After searching for a CLI tool for syntax linting in Mysql to use in Jenkins and didn't find anything quickly (this Stackoverflow question is one of the first results - LOL) I came up with the following solution (OS: Linux, but should be feasible with Windows too):
Something like the follwoing:
lint_result=`mysql mysql_test -B -f -e 'select asdf s where x;' 2>&1`; if [ `echo $lint_result | sed -r "s/ERROR ([0-9]*).*/\1/g"` -eq 1064 ]; then echo -e "Syntax error:\n${lint_result}"; fi
Syntax error:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where x' at line 1
(To check sql files you can use "< filename.sql" instead of -b -e 'statement')
If the syntax of the query can not be parsed by mysql it claims:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Only if the syntax is correct it tries to execute the query and realize that the table don't exists but this isn't interesting anymore:
ERROR 1146 (42S02) at line 1: Table 'mysql_test.s' doesn't exist
Therefor Error 1064 is the invalid syntax. You only need to create an empty test database because otherwise only errors with a wrong FROM part would appear (here for example the database is needed in order to get a valid syntax check result: 'select asdf from s where x and if;).
As far as i tested it works fine (Version Mysql 5.5).
Here a complete bash script vesion:
#!/bin/bash
source_dir=${1};
database="mysql_test";
mysql_args="-h127.0.0.1";
mysql $mysql_args -B -e "DROP DATABASE IF EXISTS $database; CREATE DATABASE $database;";
for file in `find $source_dir -name "*.sql"`; do
lint_result=`mysql $mysql_args $database -f -b < $file 2>&1`;
if [ "`echo $lint_result | sed -r \"s/ERROR ([0-9]*).*/\1/g\"`" = "1064" ]; then
echo -e "Syntax error in file ${file}:\n${lint_result}" && exit 1;
fi;
done
The commercial version of MySQL Workbench has a syntax checker for MySQL statements, but of course that would only cover the database aspects. See http://mysql.com/products/workbench/ (though I found the factoid in the help index for the free app).
Here is a validator, which validates against SQL 92/99/2003 standards however the fact you mention MySQL leads me to believe you are using MySQL specific syntax in your SQL queries.
One option is move to an agnostic approach in dealing with your database layer, writing agnostic SQL code. You will obviously need to get in contact with Mimer to see if you can take it offline and integrate it within your CI environment.
Couple of "ifs" in the overall approach and this is all contingent on the fact you are willing/able in the current state to write agnostic SQL code.
I've written an CLI tool to syntax check SQL files using the phpMyAdmin SQL parser: php-sqllint.
Disclaimer: I have not yet tried out any of these tools! YMMV!
PHPMyAdmin's validating SQL parser is an actively developed open source project and would be my best bet right now in 2021.
The other more or less current project I've found doesn't look as mature, but here it is:
Persist SQL Query "The best and most complete query builder/parser for MySQL (PHP)" according to its Github Tagline, but it's not explicitly validating, but it from the source you can see it does throw Exceptions if a query can't be parsed properly.
Just to mention it: PHP-SQL-Parser is non-validating, so it does not qualify for linting.