Error passing large text with special characters in a variable to MYSQL - mysql

I have an script that connects with SSH to a remote Host, and executes another script there which inserts data into a local MySQL database. I pass all the variables that need to be inserted within the SSH. All works fine excepts for the $textBody variable.I think the problem is that there ara a lot of text in this variable (included special characters that may be causing the error).
ssh -p 22 $user#$IP_ADDRESS "$SCRIPTMYSQL \"$taulaName\" \"$valueType\" \"$valueTitle\" \"$textBODY\" exit"
This is the error I get:
jailshell: -u: command not found
ERROR at line 1: Unknown command '\S'.
If I pass another variable different than $textBODY in his place, no error occurs.

I'm passing double quotes inside the variable.
If $textBODY doesn't contain single quotes ', you can use them:
ssh -p 22 $user#$IP_ADDRESS "$SCRIPTMYSQL \"$taulaName\" \"$valueType\" \"$valueTitle\" '$textBODY' exit"
(that also applies to the other variables, of course).

Related

Value for 'configPath' when running checkForServerUpgrade on AWS RDS

To prepare my upgrade from mysql 5.7 to mysql 8, I want to run the upgrade utility checker. Here's what I did so far:
installed mysqlsh on my machine
started mysqlsh
executed util.checkForServerUpgrade targeting the server that I want to upgrade
Here's the exact command that I used in step 3:
util.checkForServerUpgrade('root#my-remote-host:3306', { "password":"my-password" })
This runs fine but some checks are not executed because I don't provide the configPath parameter. For example, here's a warning that I get:
14) Removed system variables for error logging to the system log configuration
To run this check requires full path to MySQL server configuration file to be specified at 'configPath' key of options dictionary
More information:
https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-13.html#mysqld-8-0-13-logging
Anybody knows the value that I should provide for the configPath parameter?
I've tried to do the same using the command util.checkForServerUpgrade defining the configPath, without success. I then tried to run the same command directly from outside the mysqlsh shell, with success:
mysqlsh -- util check-for-server-upgrade root#localhost --target-version=8.0.13 --output-format=JSON --config-path=/etc/mysql/my.cnf
and it worked. To be noted that when I've tried to run from mysqlsh in the session root#localhost the command:
util.checkForServerUpgrade({"configPath":"/etc/mysql/my.cnf"})
mysqlsh replied with:
"Util.checkForServerUpgrade: Argument #1: Invalid values in connection options: configPath (ArgumentError)"
Try putting in the connection string, for example,
util.checkForServerUpgrade('root#localhost',{'configPath': '/etc/my.cnf'})
This worked for me, but without the connection string it doesn't.

How to import/load/run mysql file using golang?

I’m trying to run/load sql file into mysql database using this golang statement but this is not working:
exec.Command("mysql", "-u", "{username}", "-p{db password}", "{db name}", "<", file abs path )
But when i use following command in windows command prompt it’s working perfect.
mysql -u {username} -p{db password} {db name} < {file abs path}
So what is the problem?
As others have answered, you can't use the < redirection operator because exec doesn't use the shell.
But you don't have to redirect input to read an SQL file. You can pass arguments to the MySQL client to use its source command.
exec.Command("mysql", "-u", "{username}", "-p{db password}", "{db name}",
"-e", "source {file abs path}" )
The source command is a builtin of the MySQL client. See https://dev.mysql.com/doc/refman/5.7/en/mysql-commands.html
Go's exec.Command runs the first argument as a program with the rest of the arguments as parameters. The '<' is interpreted as a literal argument.
e.g. exec.Command("cat", "<", "abc") is the following command in bash: cat \< abc.
To do what you want you have got two options.
Run (ba)sh and the command as argument: exec.Command("bash", "-c", "mysql ... < full/path")
Pipe the content of the file in manually. See https://stackoverflow.com/a/36383984/8751302 for details.
The problem with the bash version is that is not portable between different operating systems. It won't work on Windows.
Go's os.exec package does not use the shell and does not support redirection:
Unlike the "system" library call from C and other languages, the os/exec package intentionally does not invoke the system shell and does not expand any glob patterns or handle other expansions, pipelines, or redirections typically done by shells.
You can call the shell explicitly to pass arguments to it:
cmd := exec.Command("/bin/sh", yourBashCommand)
Depending on what you're doing, it may be helpful to write a short bash script and call it from Go.

Attempting to get rid of binary operator expected error when opening a terminal session

Every time I begin a new terminal session on my Mac, I receive the following error messages:
-bash: [: /Users/angelobrown/wdi/exercises/test_ossicle/google-cloud-sdk: binary operator expected
-bash: [: /Users/angelobrown/wdi/exercises/test_ossicle/google-cloud-sdk: binary operator expected
Can anyone tell me how to fix this so that I no longer experience these messages when beginning a new terminal session? Thanks.
I had this issue and the problem is something unquoted inside the file ~/.bashrc.
This file is executed when bash is opened.
So you should open nano ~/.bashrc and search for directories with folder name spaced.
In my case, my Windows username is name lastname, so I justed quoted it in the file.
Ex:
/mnt/c/users/Foo Bar/something change to '/mnt/c/users/Foo Bar/something'

DatabaseError: 1 (HY000): Can't create/write to file '2015-04-06 20:48:33.418000'.csv (Errcode: 13 - Permission denied)

I am designing an application in Python and trying to write to a CSV file, but I am getting this error:
DatabaseError: 1 (HY000): Can't create/write to file '2015-04-06 20:48:33.418000'.csv (Errcode: 13 - Permission denied)
The Code:
def generate_report(self):
conn=mysql.connector.connect(user='root',password='',host='localhost',database='mydatabase')
exe2 = conn.cursor()
exe2.execute("""SELECT tbl_site.Site_name, State_Code, Country_Code,Street_Address, instrum_start_date, instrum_end_date, Comment INTO OUTFILE %s FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n'FROM tbl_site JOIN tbl_site_monit_invent ON site_id = tbl_Site_site_id """, (str(datetime.datetime.now()),))
I can run this code without any errors on a Mac, but I need it to work on Windows.
How can I resolve this error?
Simple really. A colon character is not a valid character in a filename on Windows. It's not allowed.
Reference: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx
The colon character is in the list of "reserved characters", along with several others. (NOTE: One use of the colon character is as a separator for an Alternate Data Stream on NTFS. Ref: http://blogs.technet.com/b/askcore/archive/2013/03/24/alternate-data-streams-in-ntfs.aspx
Followup
The question has been significantly edited since my previous answer was provided. Some notes:
I'm not very familiar with running MySQL on Windows OS. Most of my work with MySQL server is on Linux.
The SELECT ... INTO OUTFILE statement will cause the MySQL server to attempt to write a file on the server host.
The MySQL user (the user logged in to MySQL) must have the FILE privilege in order to use the SELECT ... INTO OUTFILE statement.
Also, the OS account that is running MySQL server must have OS permissions to write a file to the specified directory, and the file to be written must not already exist. Also, the filename must conform to the naming rules for filenames on OS filesystem.
Ref: https://dev.mysql.com/doc/refman/5.5/en/select-into.html
For debugging this type of issue, I strongly recommend you echo out the actual SQL text that is going to be sent to the MySQL server. And then take that SQL text and run it from a different client, like the mysql command line client.
For debugging a privileges issues, you can use a much simpler statement. Test writing a file to a directory that is known to exist, that is known the mysql server has permissions to write files to, and with a filename that does not exist and that conforms to the rules for the OS and filesystem.
For example, on a normal Linux box, we could test with something like this:
mysql> SELECT 'bar' AS foo INTO OUTFILE '/tmp/mysql_foo.csv'
Before we run that, we can easily verify that the /tmp directory exists, that it is writable by the OS account that is running the mysql server, and that the filename conforms to the rules for the filesystem, and that the filename doesn't exist, e.g.
$ su - mysql
$ ls -l /tmp/mysql_foo.csv
$ echo "foo" >/tmp/mysql_foo.csv
$ cat /tmp/mysql_foo.csv
$ rm /tmp/mysql_foo.csv
$ ls -l /tmp/mysql_foo.csv
Once we get over that hurdle, we can move on to testing writing a file to a different directory, a file with a more more complex filename. Once we get that plumbing working, we can work on getting actual data, into a usable csv format.
The original question seems to indicate that the MySQL server is running on Windows OS, and it seems to indicate that the filename attempting to be written contains semicolon characters. Windows does not allow semicolon as part a filename.
It was simply permission error.

What is the equivalent of the spool command in MySQL?

I know you use the spool command when you are trying to write a report to a file in Oracle SQLplus.
What is the equivalent command in MySQL?
This is my code:
set termout off
spool ${DB_ADMIN_HOME}/data/Datareport.log # ${DB_ADMIN_HOME}/Scripts.Datavalidation/Datareportscript.sql
spool off
exit
How can I write it in MySQL?
In MySQL you need to use the commands tee & notee:
tee data.txt;
//SQL sentences...
notee;
teedata.txt == spooldata.txt
notee == spool off
For the Oracle SQLPlus spool command, there is no equivalent in the mysql command line client.
To get output from the mysql command line client saved to a file, you can have the operating system redirect the output to a file, rather than to the display.
In Unix, you use the > symbol on the command line. (It seems a bit redundant here to give an example of how to redirect output.)
date > /tmp/foo.txt
That > symbol is basically telling the shell to take what is written to the STDOUT handle and redirect that to the named file (overwriting the file if it exists) if you have privileges.
Q: is set pagesize and set linesize used in mysql when you are trying to generate a report?
A: No. Those are specific to Oracle SQLPlus. I don't know of any equivalent functionality in the mysql command line client. The mysql command line client has some powerful features when its run in interactive mode (e.g. pager and tee), but in non-interactive mode, it's an inadequate replacement for SQLPlus.
If I get what you are asking:
mysql dbname < ${DB_ADMIN_HOME}/Scripts.Datavalidation/Datareportscript.sql \
> ${DB_ADMIN_HOME}/data/Datareport.log
Use redirection.