mysql errors to authenticate and create a table - mysql

I have not been introduced to mysql, but had to work with it. I installed it and tried to run this:
mysql> CREATE TABLE h7vsk1200001 (quid VARCHAR(15), suid VARCHAR(15), iden FLOAT, alen INT, mism INT, gapo INT, qsta INT, qend INT, ssta INT, send INT, eval FLOAT, bits INT);
Which I understand is the basis for a table, with its names for columns and its datatypes. Whenever I run it, I get this error message:
bash: error sintáctico cerca del elemento inesperado `('
By the way, I'm using Ubuntu 14.04 if that matters. Also, if I try to run the CREATE TABLE command without arguments I get:
$ mysql> CREATE TABLE
ERROR 1045 (28000): Access denied for user 'carlos'#'localhost' (using password: NO)
Consider that I have little experience with bash and no experience with mysql (as you may have denoted)

You first need to launch the mysql shell. You can't run queries like this in bash: you need to use a MySQL client. You're getting the first error because bash is trying to parse the parentheses, and the second error is the result of you running the mysql command while piping (>) the output to a new file called CREATE.
Note you're getting the ACCESS DENIED because you apparently don't have access as the user carlos without a password. Use mysql -u myusername -p to log in with username myusername and to have mysql prompt for a password.
thom#lethe-arch:~$ # bash shell
thom#lethe-arch:~$ mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 443
Server version: 10.0.14-MariaDB-log MariaDB Server
Copyright (c) 2000, 2014, Oracle, SkySQL Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
+--------------------+
2 rows in set (0.01 sec)
mysql> -- type queries here
(Note: MariaDB is a fork of MySQL and behaves exactly the same)

Seems you have to create a user 'carlos' and allow necessary permissions like:
mysql> use mysql;
mysql> CREATE USER 'carlos'#'localhost' IDENTIFIED BY 'yourpassword';
mysql> GRANT ALL PRIVILEGES ON . TO 'carlos'#'localhost' WITH GRANT OPTION;

Related

MariaDB --init-command when invoking mysql from a BASH script

I am a new user on Stack Overflow, so I apologize in advance for any potential breaches of site etiquette.
I am attempting to create a BASH script that will generate a command to invoke the MariaDB monitor, i.e. mysql. I want this mysql command to include the --init-command option. I want the --init-command option to set a list of user variables to a their values, as specified in a configuration file.
The script builds a string that appears to be correct for my purpose but, when it invokes mysql, an error is generated. If I print out the generated string from the script, it appears to be exactly what I was attempting to create.
I have boiled it down to the following code example:
#!/bin/sh
declare foo="name"
declare bar="value"
declare invoke="mysql -p -D information_schema"
declare opts=" --init-command='SET #$foo:=\"$bar\"'"
invoke+=$opts
echo $invoke
$invoke
When I execute this script, the result looks like:
$ example.sh
mysql -p -D information_schema --init-command='SET #name:="value"'
Enter password:
ERROR 1044 (42000): Access denied for user 'Bill'#'%' to database '#name:="value"''
This error message doesn't even seem to make sense.
However, if I copy the generated command, and paste it back into the command prompt, it requests my password, and proceeds as I would expect, as follows:
$ mysql -p -D information_schema --init-command='SET #name:="value"'
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 171
Server version: 10.3.11-MariaDB Source distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [information_schema]> SELECT #name;
+-------+
| #name |
+-------+
| value |
+-------+
1 row in set (0.000 sec)
MariaDB [information_schema]>
Demonstrating that the SET command in the --init-command option was successfully passed to the MariaDB monitor, and executed.
I do not know whether this is a Linux issue, a BASH issue, or a MariaDB issue. So, while I have spent a good amount of time trying to find the answer, I really don't know where the problem originates, and therefore, where to focus my research.
Please note: I only used the information_schema database in my example because I expect that anyone attempting to recreate this problem would have that database available to them.
Thank you in advance for your assistance.
Some options:
Option 1:
#!/bin/sh
# USING BASH
# FILE: bash_mariadb.sh
foo="\`name\`"
bar="value"
mysql -p -D information_schema --init-command="SET #$foo:='$bar';"
Option 2:
#!/bin/sh
# USING BASH
# FILE: bash_mariadb.sh
foo="\`name\`"
bar="value"
opts=(--init-command="SET #$foo:='$bar';")
invoke=(mysql -p -D information_schema "$opts")
"${invoke[#]}"
Option 3:
#!/bin/sh
# USING BASH
# FILE: bash_mariadb.sh
foo="\`name\`"
bar="value"
#define
invoke() {
opts=(--init-command="SET #$foo:='$bar'")
if [[ -v opts ]]; then
invoke=(mysql -p -D information_schema "$opts")
else
invoke=(mysql -p -D information_schema)
fi
"${invoke[#]}"
}
#call
invoke
Option 4: DANGER, option not recommended for safety reasons.
#!/bin/sh
# USING BASH
# FILE: bash_mariadb.sh
foo="\`name\`"
bar="value"
invoke="mysql -p -D information_schema"
opts=" --init-command='SET #$foo:=\"$bar\"'"
invoke+=$opts
eval $invoke
In all cases:
$ ./bash_mariadb.sh
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 10.3.11-MariaDB Source distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [information_schema]> SELECT #`name`;
+---------+
| #`name` |
+---------+
| value |
+---------+
1 row in set (0.000 sec)
Welcome to SO.
From a script you have to indicate the password you want to use.
The option -p force it for a interactive introduction of the password, wich don't works from a script.
If you instead use -ppassword (notice that you still write the "p", only that you write it next to the password, without spaces), your connection will work.
So, you just have to modify the line:
declare invoke="mysql -ppassword -D information_schema"
(Don't forget to write your password where I wrote "password", of course :) )

Using MySQL instance on AWS EMR cluster

I created an AWS EMR instance and logged in "Master" console via putty. I'm able to get in to MySQL command line on the console. I login as "hadoop#localhost"
mysql> SELECT user();
+------------------+
| user() |
+------------------+
| hadoop#localhost |
+------------------+
Now I want to create some users and databases for other projects. When I try to create a new user on MYSQL I get the below error.
How can I create user on EMR-MySQL instance, and give them permission ?
mysql> CREATE USER 'dbadmin'#'localhost' IDENTIFIED BY 'dbadmin';
ERROR 1227 (42000): Access denied; you need (at least one of) the CREATE USER privilege(s) for this operation
BTW when I login to MySQL console, it automatically logs me in as "hadoop#localhost", as that is the user I used to login to machine via putty.
Please advise. Thanks.
Hive uses MySQL for its metadata store on EMR. You will need to stop the MySQL service, restart in safe mode and do a standard root password recovery. This will give you root access. However, STOP and don't do this. Create your MySQL database somewhere else.
you could do sudo mysql and it should work.
sudo mysql
CREATE USER 'dbadmin'#'localhost' IDENTIFIED BY 'dbadmin';
Query OK, 0 rows affected (0.00 sec)

WSO2 GREG: My SQL setup issues

Following (https://docs.wso2.com/display/Governance450/Setting+up+with+MySQL) instructions I get an error:
mysql> -u regadmin -p -Dregdb < 'D:\Programs\wso2greg-5.1.0\dbscripts\mysql.sql';
ERROR 1064 (42000): 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 '-u regadmin -p -Dregdb < 'D:\Programs\wso2greg-5.1.0\dbscripts\mysql.sql'' at line 1
the steps are:
create database regdb character set latin1;
GRANT ALL ON regdb.* TO regadmin#localhost IDENTIFIED BY "regadmin";
FLUSH PRIVILEGES;
use regdb
show tables;
quit;
Iv'e confirmed drivers copied across - mysql_connector_java_5.1.38_bin_1.0.0.jar.
Iv'e confirmed default datasource updated - master-datasources.xml.
I didn't config any new datasources - I dont think I need them.
I then attempted to create database tables but get error above. Running "wso2server.bat -Dsetup" just generates following exception, which I assume is because I have no tables.
[2015-12-10 18:00:41,251] ERROR {org.wso2.carbon.user.core.util.DatabaseUtil} - Database Error - Incorrect string value: '\xE2\x80\x91200...' for column 'UM_DESCRIPTION' at row 1 java.sql.SQLException: Incorrect string value: '\xE2\x80\x91200...' for column 'UM_DESCRIPTION' at row 1
I'm guessing its going to be something trivial - I just don't see it. Ive tried playing around with the mysql syntax but to no avail. I note Governance450 docs say the tables are auto created. I assume the 460 is a valid correction?
-- update
part solved: not sure exactly what was wrong above (if anything). But following did create tables: (> from dos prompt, mysql> from mysql prompt)
> mysql -u root -p
--mysql> drop database if exists regdb;
mysql> create database regdb character set latin1;
--mysql> DROP USER ‘regadmin’#‘localhost’;
mysql> CREATE USER 'regadmin'#'localhost' IDENTIFIED BY 'regadmin';
mysql> GRANT ALL PRIVILEGES ON regdb.* TO 'regadmin'#'localhost';
> mysql -u regadmin -p
mysql> use regdb
mysql> source D:\Programs\wso2greg-5.1.0\dbscripts\mysql.sql;
mysql> show tables;
The steps I followed that eventually worked for me were:-
> mysql -u root -p
--mysql> drop database if exists regdb;
mysql> create database regdb;
--mysql> DROP USER ‘regadmin’#‘localhost’;
mysql> CREATE USER 'regadmin'#'localhost' IDENTIFIED BY 'regadmin';
mysql> GRANT ALL PRIVILEGES ON regdb.* TO 'regadmin'#'localhost';
> mysql -u regadmin -p
mysql> use regdb
mysql> source D:\Programs\wso2greg-5.1.0\dbscripts\mysql.sql;
mysql> show tables;
where
Dos prompt >
mysqlprompt mysql>

mysqlimport: Error: 1045, Access denied

Does anyone know why I get this error when running mysqlimport?
mysqlimport -u someone -pwhatever --columns=a,b,c,d,e bar /var/tmp/baz.sql
mysqlimport: Error: 1045, Access denied for user 'someone'#'%' (using password: YES), when using table: baz
However...
mysql -u someone -pwhatever
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 199
Server version: 5.1.41-3ubuntu12.10 (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show grants;
+------------------------------------------------------------------------------------------------------------+
| Grants for someone#% |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'someone'#'%' IDENTIFIED BY PASSWORD '*BLAHBLAHBLAH' |
| GRANT ALL PRIVILEGES ON `bar`.* TO 'someone'#'%' |
+------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql>
You can avoid the need for the extra privileges by using the --local parameter to mysqlimport:
--local, -L
Read input files locally from the client host.
OK, it turns out that the FILE privilege is a "global" privilege, which apparently means you can't selectively enable it on certain databases, tables. etc. That's why my previous grant statement on bar.* had no effect:
GRANT ALL PRIVILEGES ON `bar`.* TO 'someone'#'%'
You need to grant FILE privileges on *.*:
GRANT FILE ON *.* to 'someone'#'%';
Hope this helps someone.
Some would instead opt for this command, skipping the extra FILE grant.
mysql -u username -p <yourdbname> < yourfile.sql
mysqlimport is a command-line interface to the LOAD DATA INFILE statement, for which you need the 'FILE' privilege (server level).
From LOAD DATA INFILE syntax:
Also, to use LOAD DATA INFILE on server files, you must have the FILE privilege.
TLDR: Use the `--set-gtid-purged=OFF` Arg in MySQLDump
When doing mysqldump -u username -p to create the file you're going to import elsewhere, throw in the argument of --set-gtid-purged=OFF.
GTIDs are needed for replication, and probably don't apply to what you're doing if you just want to copy/paste DB 1 to DB 2.
General Debugging Help
My debugging process here was a little bit different than what others have done. I suggest this to debug: Change your .sql to the simplest possible thing, maybe just one single CREATE TABLE statement, and see if it runs.
If it runs, then these are things that you want to remove from your SQL import file:
Any line setting ##GLOBAL.GTID.
SET #MYSQLDUMP_TEMP_LOG_BIN = ##SESSION.SQL_LOG_BIN;
SET ##SESSION.SQL_LOG_BIN= 0;
SET ##SESSION.SQL_LOG_BIN = #MYSQLDUMP_TEMP_LOG_BIN;
As you can see, it's a lot of GTID stuff, which is transaction ID info used for doing replication. So, these are important when doing server replication, but not when doing basically a copy-paste of one DB to another DB, and in that case we can drop them.

MySQL Grant Problem

Why might the following grant statement fail to work?
grant all on kylie.* to 'kylie'#'localhost' identified by 'foo';
Here's the complete output.
$ mysql -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 63
Server version: 5.1.37 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases like 'kylie%';
+-------------------+
| Database (kylie%) |
+-------------------+
| kylie |
+-------------------+
1 row in set (0.00 sec)
mysql> grant all on kylie.* to 'kylie'#'localhost' identified by 'foo';
Query OK, 0 rows affected (0.02 sec)
mysql> exit
Bye
$ mysql -u kylie
ERROR 1045 (28000): Access denied for user 'kylie'#'localhost' (using password: YES)
It seems that these grant options get me every time. I think I have them memorized and the docs seem to check out too, but they often fail to work. What am I missing?
Try something like:
mysql -ukylie -pfoo kylie
The last kylie should tell it to use kylie as the default database (i.e. the one you have permission for). It may not be required, but I'm wondering if it'll work or not for you.
Updated thanks to the comments.