We have a simple script to connect to the database (mysql -u root -p -h localhost). I don't want to create 2 scripts (.bat, .sh), how do I just create a single script to address this issue.
You might want to give MSYS a try: http://www.mingw.org/wiki/MSYS
you can use perl scripts. Perl is powerful scripting medium which is platform independent.
use can insert batch command in a variable in perl.
for eg
#!C:\Perl64\bin\perl
my $cmd = 'mysql - u root -p -h localhost';
system $cmd;
This should work fine.
Related
I want to use a script that will get my database from a remote server and automatically deploy locally.
Desired Algorithm:
Get base
Remove the previous drop database localDbName
Create a new create database localDbName
Expand my dump use localDbName; source / var / www / html / TEST $ currentDate.sql
I tried to implement this with bash but it didn't work. At the last step, the script stops working, the term stops waiting for input.
Tell me how to fix it. Or advise a working script in bash or another language
##!/bin/sh
remoteDbUser="remoteDbUser"
remoteDbName="remoteDbName"
localDbName="localDbName"
localPort="3306"
currentDate=$(date +%Y-%m-%d_%H-%M)
ssh test#123.123.123.123 -p 123 "mysqldump -v --insert-ignore --skip-lock-tables --single-transaction=TRUE -u $remoteDbUser -p $remoteDbName" > /var/www/html/TEST$currentDate.sql
mysql -uroot -proot -h127.0.0.1 --port="$localPort" -e"source /var/www/html/TEST$currentDate.sql" --show-warnings $localDbName;
I am using Ubuntu-server. Can I setup / configure 'mysql' to auto-connect to another host? I mean if I type mysql on my terminal, it will connect automatically to specific host.
Thanks.
There are two parts for the answer.
First - how to make an alias, a word that if you type it, a specific command will execute (taken from here:
Create ~/.bash_aliases if not exists
touch ~/.bash_aliases
Open ~/.bash_aliases in a editor and append alias MY_COMMAND_ALIAS="THE COMMAND YOU WANT"
or use command echo 'alias MY_COMMAND_ALIAS="THE COMMAND YOU WANT"' >> ~/.bash_aliases
Save it
Use reload source ~/.bash_profile command to reload profile or reopen the terminal
Second part - the command you want to run mysql command, it will be something like:
mysql -u $user -p$passsword -Bse "command1;command2;....;commandn"
You can write a simple wrapper like this:
#!/bin/sh
/usr/bin/mysql --user $MYSQL_USER -p $MYSQL_PASSWORD --host $MYSQL_HOST $*
Where that should work almost identically to the default mysql command with a few tiny exceptions, like how LOAD DATA INFILE will read files only on the server, not your local machine.
You may need to do the same for mysqldump and other related commands like mysqladmin if you use those.
Be sure to specify the actual path to the mysql binary you want to run. I'm using /usr/bin/mysql here but it could be something else.
I try to dump my Cloud SQL instance database from my local computer.
I know I should use gcloud commands but in the project I will use it would be a real pain to rewrite all the mysqldump instructions.
I can connect to Cloud SQL via the MySQL client, but when I try to use mysqldump I get the following:
mysqldump --databases testdb -h 130.211.xxx.xxx -u root -p > testdump.sql
mysqldump: Got error: 1227: Access denied; you need (at least one of) the SUPER privilege(s) for this operation when using LOCK TABLES
And of course CloudSQL doesn't support SUPER privileges... :/
Does anyone know if there's a way around?
Yes it accepts it, but you must first, use the cloud_sql_proxy, and have the right permissions. Also this is not in the documentation as of the moment, neither as a warning nor as an official method. Still using an intermediary bucket for dumps is not of my liking.
In Mac OS with latest mysqldump as of moment (posted of an example of the problems I encountered might vary with os and mysqldump version)
mysqldump --column_statistics=0 -h 127.0.0.1 -u <user> -p <db> --set-gtid-purged=OFF> <dumpFile>
// this is because I use the tcp connection sample for the cloud sql proxy
mysql -h 127.0.0.1 -u <user> -p -D <database> < DBs/mysqldump100519.sql
According to their documentation, seems you have 2 options.
The first, which you do not like is to use a gcloud command.
The second, use RESTful API to access the service which is, under the hood, used by gcloud commands. You may use the same request from inside you code. Take a look here.
I need the script because a DB schema has changed and I have to fill a new table with some values (these have to be calculated using a php class). That was just a short summary, for a small overview).
Here's the first thing I can't figure out. I'm new to scripts so I was watching a few basic/beginner guids. In the shell (windows) I can run:
mysql -B -D www -h xxx -u yyy -pzzz -e "show tables"
this prints me all tables. Fine. If I try something similar in a batch script just to test if a can use variables
#!/bin/bash
V=10 // or V="10" // or V=$10, i saw all of it
mysql -B -D www -h xxx -u yyy -pzzz -e "select * from session_content_score where score_id<$V"
I'm always getting the error The command V cannot be found. I tried every declaration I could find. Whats the problem? How can I make usage of variables, because I need them for fulfilling my task?
The correct way to set a variable in a windows batch script is:
SET foo=bar
echo %foo%
A good windows batch tutorial is here:
Windows batch tutorial
You should question whether you are doing the right thing when you want to populate your database from windows batch scripts :)
I have a user on my machine that is only supposed to run mysql. Is there any way that I can set the shell of that user to mysql and login using password and username?
I know how to change the shell to the mysql binary
usermod -s /usr/bin/mysql
That is working indeed, only I can't provide a username/password in the program. Usually user/pw are given as
mysql -u $USER -p
I can not provide parameters for a shell as in
usermod -s "/usr/bin/mysql -u $USER -p" # Does not work!
Also using a simple shell-script as shell does not work:
#!/bin/sh # mysqlShell
/usr/bin/mysql -u $USER -p
----
usermod -s mysqlShell # does not work
So how can I provide parameters to a program I use as a shell for a user?
Thanks to Tom Regner I could figure out a solution using .my.cnf containing
[client]
host=localhost
user=$user
password=$pass
disable-auto-rehash
where mysql is set to the shell. I still would like give the password manually but this is the best I found.
Setup a $HOME/.my.cnf file for the user
[client]
host=localhost
user=mysqluser
password=mysqlpass
then set a bash as login shell and put the following in $HOME/.bashrc
exec mysql --host=localhost dbname
that should do what you want, while the user in question just has to give one password (the system account password on login).
exec replaces the shell process with the mysql process.
If this does not work as expected, you may need to adjust $HOME/.bash_profile to source .bashrc:
[[ -f ~/.bashrc ]] && . ~/.bashrc
It might be enough to provide an appropriate .my.cnf and setting /usr/bin/mysql as shell, but this way you can pass arbitrary commandline options/flags to the mysql client.
You can do that by editing the user's account details in the /etc/passwd and change the default shell.
You need a login password (unless you set up ssh appropriately). Use the following command: sudo passwd username to change that login password.
You also need a mysql password. Use SET PASSWORD Mysql request.
If you want the user to be connected to some fixed database with some fixed password, code a small C wrapper (then, make the executable only executable by your Unix user) doing mysql_real_connect, or calling some exec function for mysql --user=username --password=password databasename but I don't recommend doiing the later (because ps aux will show the password, and that is a security risk).
Perhaps, since MySQL is free software, you could customize the source code of mysql for your particular needs.
Perhaps using a restricted shell and carefully configuring it is better.