I am writing a script that will run on a Ubuntu 16.04.1 server. The purpose of the script is to install a wordpress site on the server. I took most of my scripting from this site.
I am trying to create and grant permissions on the database, however my script stops when I enter a mysql environment using mysql -u root -p. As well, variables created in the bash environment are not carried over to the mysql environment. Here is my script for this section.
echo "Please enter the Name of the Database(please make name relevant)"
read -p DBNAME
echo "Please enter the Name of the Database User(Please document username)"
read -p DBUSER
echo "Please enter a Database password(Please document password)"
read -p DBPASSWORD
a=\'
b=$a$DBPASSWORD$a
f='CREATE DATABASE '$DBNAME' CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci'
g=$a$f$a
c='GRANT ALL ON '$DBNAME.*'TO '$DBUSER'#localhost'' IDENTIFIED BY '$b
d=$a$c$a
mysql -u root -p -e $g
mysql -u root -p -e $c
mysql -u root -p -e "FLUSH PRIVILEGES"
Any help integrating the environment changes together would be great
When defining f and c use double quotes. Single quotes don't allow your variables to expand.
You log the user in as root to mysql, but don't supply a password, so it will prompt. Perhaps ask for the root password as well.
Perhaps something like the following would do the trick:
echo "Please enter the Name of the Database(please make name relevant)"
read -p DBNAME
echo "Please enter the Name of the Database User(Please document username)"
read -p DBUSER
echo "Please enter a Database password(Please document password)"
read -p DBPASSWORD
echo "Please enter your database's root password"
read -p DBROOTPASSWORD
a=\'
b=$a$DBPASSWORD$a
f="CREATE DATABASE '$DBNAME' CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"
g=$a$f$a
c="GRANT ALL ON $DBNAME.* TO $DBUSER#localhost IDENTIFIED BY "$b
d=$a$c$a
mysql -u root -p"$DBROOTPASSWORD" -e $g
mysql -u root -p"$DBROOTPASSWORD" -e $c
mysql -u root -p"$DBROOTPASSWORD" -e "FLUSH PRIVILEGES"
Related
I have a database that is quite large so I want to export it using Command Prompt but I don't know how to.
I am using WAMP.
First check if your command line recognizes mysql command. If not go to command & type in:
set path=c:\wamp\bin\mysql\mysql5.1.36\bin
Then use this command to export your database:
mysqldump -u YourUser -p YourDatabaseName > wantedsqlfile.sql
You will then be prompted for the database password.
This exports the database to the path you are currently in, while executing this command
Note: Here are some detailed instructions regarding both import and export
Simply use the following command,
For Export:
mysqldump -u [user] -p [db_name] | gzip > [filename_to_compress.sql.gz]
For Import:
gunzip < [compressed_filename.sql.gz] | mysql -u [user] -p[password] [databasename]
Note: There is no space between the keyword '-p' and your password.
Well you can use below command,
mysqldump --databases --user=root --password your_db_name > export_into_db.sql
and the generated file will be available in the same directory where you had ran this command.
You could find more on the official reference for mysqldump: Import Export MySQL DB
Note: use --databases instead of --database since the last one is no more supported.
Enjoy :)
First of all
open command prompt then open bin directory in cmd (i hope you're aware with cmd commands)
go to bin directory of your MySql folder in WAMP program files.
run command
mysqldump -u db_username -p database_name > path_where_to_save_sql_file
press enter system will export particular database and create sql file to the given location.
i hope you got it :)
if you have any question please let me know.
Go to command prompt at this path,
C:\Program Files (x86)\MySQL\MySQL Server 5.0\bin>
Then give this command to export your database (no space after -p)
mysqldump -u[username] -p[userpassword] yourdatabase > [filepath]wantedsqlfile.sql
Locate your mysql instance with:
which mysql
If this is correct then export with the following (else navigate to the mysql instance in your mamp folder in bin):
mysqldump -u [username] -p [password] [dbname] > filename.sql
And if you wish to zip it at the sametime:
mysqldump -u [username] -p [password] [db] | gzip > filename.sql.gz
You can then move this file between servers with:
scp user#xxx.xxx.xxx.xxx:/path_to_your_dump/filename.sql.gz your_detination_path/
(where xxx.xxx.xxx.xxx is the server IP address)
And then import it with:
gunzip filename.sql.gz | mysql -u [user] -p [password] [database]
To export PROCEDUREs, FUNCTIONs & TRIGGERs too, add --routines parameter:
mysqldump -u YourUser -p YourDatabaseName --routines > wantedsqlfile.sql
The problem with all these solutions (using the > redirector character) is that you write your dump from stdout which may break the encoding of some characters of your database.
If you have a character encoding issue. Such as :
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 ...
then, you MUST use -r option to write the file.
MySQL
mysqldump -u user -pyour-password-without-space-between-letter-p-and-your-password --default-character-set=utf8 --host $HOST database-name -r dump.sql
Using Docker
docker exec --rm -v $pwd:dump -it mysql:5:7 mysqldump -u user -pyour-password-without-space-between-letter-p-and-your-password --default-character-set=utf8 --host $HOST database-name -r dump/dump.sql
Note: this mounts the current path as dump inside the instance.
We found the answer here
Conversely, don't use < to import your dump into your database, again, your non-utf8 characters may not be passed; but prefer source option.
mysql -u user -pYourPasswordYouNowKnowHow --default-character-set=utf8 your-database
mysql> SET names 'utf8'
mysql> SOURCE dump.sql
Give this command to export your database, this will include date as well
mysqldump -u[username] -p[userpassword] --databases yourdatabase | gzip > /home/pi/database_backup/database_`date '+%m-%d-%Y'`.sql.gz
(no space after -p)
I have installed my wamp server in D: drive so u have to go to the following path from ur command line->(and if u have installed ur wamp in c: drive then just replace the d: wtih c: here)
D:\>cd wamp
D:\wamp>cd bin
D:\wamp\bin>cd mysql
D:\wamp\bin\mysql>cd mysql5.5.8 (whatever ur verserion will be displayed here use keyboard Tab button and select the currently working mysql version on your server if you have more than one mysql versions)
D:\wamp\bin\mysql\mysql5.5.8>cd bin
D:\wamp\bin\mysql\mysql5.5.8\bin>mysqldump -u root -p password db_name > "d:\backupfile.sql"
here root is user of my phpmyadmin
password is the password for phpmyadmin so if u haven't set any password for root just nothing type at that place,
db_name is the database (for which database u r taking the backup)
,backupfile.sql is the file in which u want ur backup of ur database and u can also change the backup file location(d:\backupfile.sql) from to any other place on your computer
mysqldump -h [host] -p -u [user] [database name] > filename.sql
Example in localhost
mysqldump -h localhost -p -u root cookbook > cookbook.sql
mysqldump --no-tablespaces -u username -p pass database_name > db_backup_file.sql
Syntax
(mysqldump.exe full path) -u (user name) -p (password) (database name) > (export database file full path)
Example
c:>d:\wamp\bin\mysql\mysql5.6.12\bin\mysqldump.exe -u root -p mydbname > d:\mydb.sql
where d:\wamp\bin\mysql\mysql5.6.12\bin\mysqldump.exe will be your actual mysqldump.exe path, mydbname is the name of database which you want to export and d:\mydb.sql is the path where you want to store the exported database.
For import:
mysql -u db_username -p newFileName < databasName.sql
For export:
mysqldump -u db_username -p databasName > newFileName.sql
I have used wamp server. I tried on
c:\wamp\bin\mysql\mysql5.5.8\bin\mysqldump -uroot -p db_name > c:\somefolder\filename.sql
root is my username for mysql, and if you have any password specify it with:
-p[yourpassword]
Hope it works.
For windows OS :
When you get error 1064 mysql (42000) while trying to execute mysqldump command, exit from current terminal. And execute mysqldump command.
mysql>exit
c:\xampp\mysql\bin>mysqldump -uroot -p --databases [database_name] > name_for_export_db.sql
I was trying to take the dump of the db which was running on the docker and came up with the below command to achieve the same:
docker exec <container_id/name> /usr/bin/mysqldump -u <db_username> --password=<db_password> db_name > .sql
Hope this helps!
mysql -u -p databaseName>fileToPutDatabase
Login in your databse server and then hit the below command:-
mysql -u username -p databasename > exportfilename.sql
Then it will ask for password Enter the password and hit enter,it will take some time your database will be exported.
You can use this script to export or import any database from terminal
given at this link: https://github.com/Ridhwanluthra/mysql_import_export_script/blob/master/mysql_import_export_script.sh
echo -e "Welcome to the import/export database utility\n"
echo -e "the default location of mysqldump file is: /opt/lampp/bin/mysqldump\n"
echo -e "the default location of mysql file is: /opt/lampp/bin/mysql\n"
read -p 'Would like you like to change the default location [y/n]: ' location_change
read -p "Please enter your username: " u_name
read -p 'Would you like to import or export a database: [import/export]: ' action
echo
mysqldump_location=/opt/lampp/bin/mysqldump
mysql_location=/opt/lampp/bin/mysql
if [ "$action" == "export" ]; then
if [ "$location_change" == "y" ]; then
read -p 'Give the location of mysqldump that you want to use: ' mysqldump_location
echo
else
echo -e "Using default location of mysqldump\n"
fi
read -p 'Give the name of database in which you would like to export: ' db_name
read -p 'Give the complete path of the .sql file in which you would like to export the database: ' sql_file
$mysqldump_location -u $u_name -p $db_name > $sql_file
elif [ "$action" == "import" ]; then
if [ "$location_change" == "y" ]; then
read -p 'Give the location of mysql that you want to use: ' mysql_location
echo
else
echo -e "Using default location of mysql\n"
fi
read -p 'Give the complete path of the .sql file you would like to import: ' sql_file
read -p 'Give the name of database in which to import this file: ' db_name
$mysql_location -u $u_name -p $db_name < $sql_file
else
echo "please select a valid command"
fi
I need to change the temporary root password that is created when the MySQL daemon is started. The problem is that the temporary password has some weird characters (e.g. like a left/right parenthesis) that needs to be escaped. Now, there are several posts (here, here, here) on how to escape characters, in general, but this post is in the context of using a bash script to change the temporary MySQL root password that may have special characters.
Currently, my script looks like the following.
function startMysql {
sudo service mysqld start
echo "started mysql"
export PW=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $11}')
# export PASS=\'$PW\'
echo "temporary password is $PW"
mysqladmin -u root -p$PW password aaBB##cc1122
# the following doesn't work either
# mysqladmin -u root -p$PASS password aaBB##cc1122
echo "changed mysql password"
}
Note that the temporary password may look like the following.
BYkc*),ZM3-_
If I type in the following on the terminal, it works.
mysqladmin -u root -p'BYkc*),ZM3-_' password aaBB##cc1122
But inside the script, it fails. Here are some ways that I have tried to put single quotes around $PW without success.
mysqladmin -u root -p"'$PW'" password aaBB##cc1122
mysqladmin -u root -p\''$PW'\' password aaBB##cc1122
mysqladmin -u root -p"$PW" password aaBB##cc1122
mysqladmin -u root -p"\"$PW\"" password aaBB##cc1122
Any ideas on what I am doing wrong?
It does work for me using double-quotes:
password=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $11}')
mysqladmin --user=root --password="$password" password aaBB##cc1122
In a simple script like this one:
set -x
# Check if db exists, if not we make it, make user, give privileges
if ! mysql -u root -p -e "use $db" 2>/dev/null; then
c1="CREATE DATABASE $db"
c2="GRANT ALL PRIVILEGES ON ${db}.* to '$username'#'localhost' IDENTIFIED BY '$password'"
c3="FLUSH PRIVILEGES"
mysql -u root -p -e "$c1; $c2; $c3"
else
echo 'DATABASE ExISTS, ABORTING'; exit $DB_EXISTS
fi
I am asked each time, bash sees mysql command, for my root credentials.
Is there a way to avoid that, so that once entered the root password, all
additional mysql commands execute seamlessly?
Try looking into adding password to ~/.my.cnf
[client]
user = root
password = XXXXXXXX
Check out :
How to execute a MySQL command from a shell script?
Specifying the --password argument
mysql -u root --password=my_mysql_pass db_name
Safer using a bash variable
mysql -u root --password=$MYSQL_PASS db_name
So I want to pipe my netstat output into a mysql database
The plan is to make it a continuous crontab event. That way I can store that data and use it elsewhere easily.
I figured before I could figure that part out though I needed to figure out how to run a SQL Command from terminal.
It seems pretty straight forward
sudo mysql -u username -pMYPassword -e "SQL COMMAND"
This however doesn't work...
When I run this it prints the MYSQL Help
When I run this though
sudo mysql -u username -p -e "SQL COMMAND"
everything works perfect, it just prompts me for a password
Now I don't know if it makes a difference or not but my DB password does have exclamation points in it. It is also over 15 chars long
Could either of these be an issue?
mysql -u user -p -e 'SQL Query' database
Where,
-u : Specify mysql database user name
-p : Prompt for password
-e : Execute sql query
database : Specify database name
So the option -p is a PROMPT, the password need not to be linked, but to be separated from it:
sudo mysql -u username -p'PassWord' -e 'SELECT COUNT(*) FROM my_table'
EDIT: If your real password contains special characters, you need to escape them.
Maybe try : sudo mysql -u username -p'MYPassword' -e "SQL COMMAND"
Or : sudo mysql -u username --password=MyPassword -e "SQL COMMAND"
I have a database that is quite large so I want to export it using Command Prompt but I don't know how to.
I am using WAMP.
First check if your command line recognizes mysql command. If not go to command & type in:
set path=c:\wamp\bin\mysql\mysql5.1.36\bin
Then use this command to export your database:
mysqldump -u YourUser -p YourDatabaseName > wantedsqlfile.sql
You will then be prompted for the database password.
This exports the database to the path you are currently in, while executing this command
Note: Here are some detailed instructions regarding both import and export
Simply use the following command,
For Export:
mysqldump -u [user] -p [db_name] | gzip > [filename_to_compress.sql.gz]
For Import:
gunzip < [compressed_filename.sql.gz] | mysql -u [user] -p[password] [databasename]
Note: There is no space between the keyword '-p' and your password.
Well you can use below command,
mysqldump --databases --user=root --password your_db_name > export_into_db.sql
and the generated file will be available in the same directory where you had ran this command.
You could find more on the official reference for mysqldump: Import Export MySQL DB
Note: use --databases instead of --database since the last one is no more supported.
Enjoy :)
First of all
open command prompt then open bin directory in cmd (i hope you're aware with cmd commands)
go to bin directory of your MySql folder in WAMP program files.
run command
mysqldump -u db_username -p database_name > path_where_to_save_sql_file
press enter system will export particular database and create sql file to the given location.
i hope you got it :)
if you have any question please let me know.
Go to command prompt at this path,
C:\Program Files (x86)\MySQL\MySQL Server 5.0\bin>
Then give this command to export your database (no space after -p)
mysqldump -u[username] -p[userpassword] yourdatabase > [filepath]wantedsqlfile.sql
Locate your mysql instance with:
which mysql
If this is correct then export with the following (else navigate to the mysql instance in your mamp folder in bin):
mysqldump -u [username] -p [password] [dbname] > filename.sql
And if you wish to zip it at the sametime:
mysqldump -u [username] -p [password] [db] | gzip > filename.sql.gz
You can then move this file between servers with:
scp user#xxx.xxx.xxx.xxx:/path_to_your_dump/filename.sql.gz your_detination_path/
(where xxx.xxx.xxx.xxx is the server IP address)
And then import it with:
gunzip filename.sql.gz | mysql -u [user] -p [password] [database]
To export PROCEDUREs, FUNCTIONs & TRIGGERs too, add --routines parameter:
mysqldump -u YourUser -p YourDatabaseName --routines > wantedsqlfile.sql
The problem with all these solutions (using the > redirector character) is that you write your dump from stdout which may break the encoding of some characters of your database.
If you have a character encoding issue. Such as :
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 ...
then, you MUST use -r option to write the file.
MySQL
mysqldump -u user -pyour-password-without-space-between-letter-p-and-your-password --default-character-set=utf8 --host $HOST database-name -r dump.sql
Using Docker
docker exec --rm -v $pwd:dump -it mysql:5:7 mysqldump -u user -pyour-password-without-space-between-letter-p-and-your-password --default-character-set=utf8 --host $HOST database-name -r dump/dump.sql
Note: this mounts the current path as dump inside the instance.
We found the answer here
Conversely, don't use < to import your dump into your database, again, your non-utf8 characters may not be passed; but prefer source option.
mysql -u user -pYourPasswordYouNowKnowHow --default-character-set=utf8 your-database
mysql> SET names 'utf8'
mysql> SOURCE dump.sql
Give this command to export your database, this will include date as well
mysqldump -u[username] -p[userpassword] --databases yourdatabase | gzip > /home/pi/database_backup/database_`date '+%m-%d-%Y'`.sql.gz
(no space after -p)
I have installed my wamp server in D: drive so u have to go to the following path from ur command line->(and if u have installed ur wamp in c: drive then just replace the d: wtih c: here)
D:\>cd wamp
D:\wamp>cd bin
D:\wamp\bin>cd mysql
D:\wamp\bin\mysql>cd mysql5.5.8 (whatever ur verserion will be displayed here use keyboard Tab button and select the currently working mysql version on your server if you have more than one mysql versions)
D:\wamp\bin\mysql\mysql5.5.8>cd bin
D:\wamp\bin\mysql\mysql5.5.8\bin>mysqldump -u root -p password db_name > "d:\backupfile.sql"
here root is user of my phpmyadmin
password is the password for phpmyadmin so if u haven't set any password for root just nothing type at that place,
db_name is the database (for which database u r taking the backup)
,backupfile.sql is the file in which u want ur backup of ur database and u can also change the backup file location(d:\backupfile.sql) from to any other place on your computer
mysqldump -h [host] -p -u [user] [database name] > filename.sql
Example in localhost
mysqldump -h localhost -p -u root cookbook > cookbook.sql
mysqldump --no-tablespaces -u username -p pass database_name > db_backup_file.sql
Syntax
(mysqldump.exe full path) -u (user name) -p (password) (database name) > (export database file full path)
Example
c:>d:\wamp\bin\mysql\mysql5.6.12\bin\mysqldump.exe -u root -p mydbname > d:\mydb.sql
where d:\wamp\bin\mysql\mysql5.6.12\bin\mysqldump.exe will be your actual mysqldump.exe path, mydbname is the name of database which you want to export and d:\mydb.sql is the path where you want to store the exported database.
For import:
mysql -u db_username -p newFileName < databasName.sql
For export:
mysqldump -u db_username -p databasName > newFileName.sql
I have used wamp server. I tried on
c:\wamp\bin\mysql\mysql5.5.8\bin\mysqldump -uroot -p db_name > c:\somefolder\filename.sql
root is my username for mysql, and if you have any password specify it with:
-p[yourpassword]
Hope it works.
For windows OS :
When you get error 1064 mysql (42000) while trying to execute mysqldump command, exit from current terminal. And execute mysqldump command.
mysql>exit
c:\xampp\mysql\bin>mysqldump -uroot -p --databases [database_name] > name_for_export_db.sql
I was trying to take the dump of the db which was running on the docker and came up with the below command to achieve the same:
docker exec <container_id/name> /usr/bin/mysqldump -u <db_username> --password=<db_password> db_name > .sql
Hope this helps!
mysql -u -p databaseName>fileToPutDatabase
Login in your databse server and then hit the below command:-
mysql -u username -p databasename > exportfilename.sql
Then it will ask for password Enter the password and hit enter,it will take some time your database will be exported.
You can use this script to export or import any database from terminal
given at this link: https://github.com/Ridhwanluthra/mysql_import_export_script/blob/master/mysql_import_export_script.sh
echo -e "Welcome to the import/export database utility\n"
echo -e "the default location of mysqldump file is: /opt/lampp/bin/mysqldump\n"
echo -e "the default location of mysql file is: /opt/lampp/bin/mysql\n"
read -p 'Would like you like to change the default location [y/n]: ' location_change
read -p "Please enter your username: " u_name
read -p 'Would you like to import or export a database: [import/export]: ' action
echo
mysqldump_location=/opt/lampp/bin/mysqldump
mysql_location=/opt/lampp/bin/mysql
if [ "$action" == "export" ]; then
if [ "$location_change" == "y" ]; then
read -p 'Give the location of mysqldump that you want to use: ' mysqldump_location
echo
else
echo -e "Using default location of mysqldump\n"
fi
read -p 'Give the name of database in which you would like to export: ' db_name
read -p 'Give the complete path of the .sql file in which you would like to export the database: ' sql_file
$mysqldump_location -u $u_name -p $db_name > $sql_file
elif [ "$action" == "import" ]; then
if [ "$location_change" == "y" ]; then
read -p 'Give the location of mysql that you want to use: ' mysql_location
echo
else
echo -e "Using default location of mysql\n"
fi
read -p 'Give the complete path of the .sql file you would like to import: ' sql_file
read -p 'Give the name of database in which to import this file: ' db_name
$mysql_location -u $u_name -p $db_name < $sql_file
else
echo "please select a valid command"
fi