MySQL Call script from command line, but don't exit the session - mysql

I have 15+ Mysql database on different machines and call each of them manually through the command line. I have replaced those commands with an alias and created a windows batch file using DOSKEY
example of running the alias:
c:\Projects>DOSKEY mysql_db1=mysql -u staffing -p staffing -e "select database(), user();"
c:\Projects>mysql_db1
Enter password: ********
+------------+--------------------+
| database() | user() |
+------------+--------------------+
| staffing | staffing#localhost |
+------------+--------------------+
c:\Projects>
This works fine, but is there a way to not exit the MySQL prompt?
Basically, for each database that I log in, I should see that its the correct database and run other commands without exit.
Is it possible with MySQL?

Related

mysql command line can no longer see any databases

I am running a local mysql server to work with some old data from a dump. I am able to normally log into the database using:
/mysql -uroot -p
and in fact the show databases command correctly outputs the following, I am also able to execute queries and use the data normally:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| servicenow2 |
| sys |
+--------------------+
now the problem is I was using command line to import some files from my old dump:
/mysql -uroot -p servicenow2</filetoimport.sql
however this command no longer works always resulting in (for all dbs in the server):
ERROR 1049 (42000): Unknown database 'servicenow2'
I assume some schema got corrupted somewhere is there any way to fix this so I do not have to dump the db and reimport it the db I am working with is over 150gb and would take a very long time to dump and reimport.
update: I performed a flush privileges as described here : https://superuser.com/questions/603026/mysql-how-to-fix-access-denied-for-user-rootlocalhost
now the command : mysql -uroot -p servicenow2 correctly starts a client in the servicenow2 db but the import mysql -uroot -p servicenow2</filetoimport.sql still gives unknown db error.
Work around in case anyone is interested:
create a new db and import into the new db then move from table in new db to desired db

Supply mysql command in shell script

I am trying to automate some tasks for mysql docker container for that I am using a shell script. But I not able to pass/supply mysql commands after starting mysql server.
Below is my shell script.
#!/bin/bash
mysql.server start
mysql -u root
show databases;
All steps are works as expected else last one 'show databases;' am not getting how to pass 'show databases;' command after mysql server starts. I am on a MAC machine, same behaviour happens on my ubuntu 14.04 container.
below is output on my console.
Please try:
mysql -uUSER -pPASSWORD DBNAME -e 'show databases;'
USER is your user
PASSWORD is your password
-e switch is used to fire commands to mysql via shell
A sample output:
User#Host:~> mysql -uUSER -pPASSWORD DBNAME -e 'show databases ;'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mdpdb |
| mdpdb6 |
| mysql |
+--------------------+

How to get mysql to use the current effective linux user as the default user in mysql?

If I login to a linux system as user alex, then change to user bob with su - bob, then as bob run mysql with no username arg specified, mysql will report (select user();) the user is alex.
I know I can do mysql -u bob to change the user, however I'm wondering if there's any way to configure mysql to use the current effective user (user reported by whoami) who ran the command mysql, bob in this example, to be used as the default when no username arg is specified to mysql? Thanks for any input.
Example:
Login as user alex
[alex#gc-instance-1 ~]$ mysql -e 'select user()'
+----------------+
| user() |
+----------------+
| alex#localhost |
+----------------+
[alex#gc-instance-1 ~]$ su - bob
Password:
[bob#gc-instance-1 ~]$ mysql -e 'select user()'
+----------------+
| user() |
+----------------+
| alex#localhost |
+----------------+
[bob#gc-instance-1 ~]$
Doesn't it work to create a .my.cnf file in the home directory of bob and put
[client]
user=bob
Alternatively you can also put password. Eg.
[client]
user=bob
password=1234
Then change the file permissions to read/write by owner (600)
Also explained at MySQL Manual
This way, you can just write 'mysql' and login without entering the password also.
PS. strangely in my system it uses the username of the whoever I became using 'su -' I am not sure why it is different in your machine. Manual says it defaults to unix username...

MySQL User Permission Errors using Sqitch on Ubtunu

I am running into what I think is a very easy issue to fix, I am just out of possible ideas.
I have a brand new Ubuntu 14.04 x64 server. I just installed MySQL. Not Apache, php or phpMyAdmin, just plain MySQL.
I have run through mysql_secure_installation and created a password for my root user.
I then put my root password in the /etc/mysql/my.cnf file under the [client] section.
I can run mysql -u root and get to the MySQL console just fine.
However, if I run sqitch deploy I get:
Access denied for user 'root'#'localhost' (using password: NO)
Sqitch is pointing to:
[target "database name_v1"]
uri = db:mysql://root#/databasename_v1
[engine "mysql"]
target = db:mysql://root#/databasename_v1
EDIT
It turns out the problem was with Sqitch and my configuration. Sqitch is a Perl application and needed the perl module MySQL Config in order to read the my.cnf file and access the database.
It turns out the problem was with Sqitch and my configuration. Sqitch is a Perl application and needed the perl module MySQL::Config in order to read the my.cnf file and access the database.
If you are using -v to get MySQL version, here's your answer : mysql -V
-v is for verbose output.
See this thread for more details.
"mysql -v" command line error(linux/ubuntu)
There are multiple root users in mysql. They are in the format user#remote.
You can check if the user password is set for all of those by running the query select Host, User, Password from mysql.user where User="root";
The output should show something like:
+--------------+----------+-------------------------------------------+
| Host | User | Password |
+--------------+----------+-------------------------------------------+
| localhost | root | *XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| testvm | root | *XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 127.0.0.1 | root | *XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
If the password is empty, set it via mysqladmin.
From here on, your connection to mysql, whether coming from localhost, or coming from the hostname will be allowed to login.

Copy MySQL user to create a new one

I have a ton of users on many different MySQL servers with this type
myuser#localhost
myotheruser#localhost
now, I want to create new users, that should have the same password as the user above, and have access to the same databases, but from a different host like this:
myuser#127.0.0.1
myotheruser#127.0.0.1
does anyone have a quick and easy way to do this?
I have tried a different approach to this problem, but running these two commands:
mysqldump --skip-extended-insert mysql user | grep 'localhost' | egrep '^INSERT INTO' | sed 's/localhost/127.0.0.1/g' > add-local-ip-user.sql
mysqldump --skip-extended-insert mysql db | grep 'localhost' | egrep '^INSERT INTO' | sed 's/localhost/127.0.0.1/g' > add-local-ip-db.sql
Then, I just output the content:
cat add-local-ip*
then I open mysql:
mysql mysql
and finally just paste the output from above, and run flush privileges does anyone have any objection to doing it this way? e.g. is this a stupid solution?