$db_host = "";
$db_user = "";
$db_pass = "";
$db_name = "";
I have to fill out these, so I wanted to check but I used 'ampps' to install mysql.
so I couldn't find info in my database.
where should I check in below pic?
mysql wrokbench
wrokbench2
Detault mysql login credentials in AMPPS are :
Username : root
Pass : mysql
You can create a new database using phpMyAdmin :
http://localhost/phpmyadmin/
I have this script to access my mysql database and do some maintenance.
I am on windows platform.
Mysqldump is installed at C:\Program Files\MySQL\MySQL Server 5.7\bin
and PHP.exe is installed at C:\TCAFiles\xampp\php
I have a database.php with
<?php
// Database Connection Setup
// -------------------------------------------------------
$dbhost = '127.0.0.1';
$dbname = 'dbname';
$dbuser = 'username';
$dbpass = 'password';
// -------------------------------------------------------
$db_local = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
?>
I have a cleanup.php
<?php
// Set the protection period
$ProtectionPeriod = 97;
echo "================================\nDatabase Cleanup Started:\n================================\n";
include 'database.php';
// Do db backup
// -------------------------------------------------------
// if mysqldump is on the system path you do not need to specify the full path
// simply use "mysqldump --add-drop-table ..." in this case
$date = date("Y-m-d_H-i-s");
$dumpfname = 'D:\\Tinboye\\backups\\'.$dbname.'_'.$date.'.sql';
$command = "C:\\Program%20Files\\MySQL\MySQL%20Server%205.7\\bin\\mysqldump --add-drop-table --host=$dbhost --user=$dbuser --password=$dbpass exile > $dumpfname";
system($command);
echo "backup $dumpfname made\n\n";
// delete push bikes
$sql = "DELETE FROM vehicle WHERE class = 'Exile_Bike_OldBike' OR class = 'Exile_Bike_MountainBike'";
$result = mysqli_query($db_local, $sql);
// Delete players not logged in for $ProtectionPeriod days with less than 10 total_connections
$sql = "DELETE FROM player WHERE account_uid IN (SELECT uid FROM account WHERE last_connect_at < NOW() - INTERVAL $ProtectionPeriod DAY)";
$result = mysqli_query($db_local, $sql);
// Remove empty containers not used in 48 hours
$sql = "DELETE FROM container WHERE last_updated_at <= NOW() - INTERVAL 48 HOUR AND cargo_items = '[[],[]]' AND cargo_magazines = '[]' AND cargo_weapons = '[]' AND cargo_container = '[]'";
$result = mysqli_query($db_local, $sql);
?>
and a bat file to run the php script
#echo off
::=====================================================================================================================================
:: Database Housekeeping backup & clean
::=====================================================================================================================================
C:\TCAFiles\xampp\php\php.exe D:\Tinboye\servers\jnjexile\ExileCleanup\exile_cleanup.php
pause
but when i execute the script I get an error
================================
Database Cleanup Started:
The system cannot find the path specified.
backup D:\Tinboye\backups\exile_dayz_2016-04-09_23-30-31.sql made
Press any key to continue . . .
Terminate batch job (Y/N)?
when i go to the folder D:\Tinboye\backups there is the file created, but no data, which leads me to believe that the mysqldump is not found.
anyone see the problem here?
I am using a php script to backup my database. Below is the given script:
$datestamp = date("Y-m-d_h-i-s"); // Current date to append to filename of backup file in format of YYYY-MM-DD
/* CONFIGURE THE FOLLOWING FOUR VARIABLES TO MATCH YOUR SETUP */
$dbuser = "xyz"; // Database username
$dbpwd = "*****"; // Database password
$dbname = "mydatabase"; // Database name. Use --all-databases if you have more than one
$filename = "backup-$datestamp.sql.gz"; // The name (and optionally path) of the dump file
$command = "mysqldump -u $dbuser --password=$dbpwd $dbname | gzip > $filename";
$result = system($command);
Below is the command line
php5 /var/www/vhosts/cc.astra1641.startdedicated.de/httpdocs/cron/backup.php
I have output the $result variable using var_dump to check it returns empty string. also I have tried using other methods like exec($command) or passthru($command), it doesn't work.
Am trying to get a auto backup of our mysql database via a cron job that runs daily.
We have:
$database_user = 'VALUE';
$database_pass = 'VALUE';
$database_server = 'localhost';
// Name of the database to backup
$database_target = 'VALUE';
// Get Database dump
$sql_backup_file = $temp_dir . '/sql_backup.sql';
$backup_command = "mysqldump -u" . $database_user . " -p" . $database_pass . " -h " . $database_server . " " . $database_target . " > " . $sql_backup_file;
system($backup_command);
// End Database dump
Problem is we get a message back from the Cron Daemon with:
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help
sh: -h: command not found
So it looks like it has something to do with the -h
~~~
Anyone have any thoughts on how to fix?
First, I recommend you upgrade to mysql 5.6+ so that you can keep your database passwords more secure. First, you would follow the instructions from this stackoverflow answer to set up the more-secure mysql login method for command line scripts.
You should probably write a bash script instead of using PHP. Here's a full backup script, very simple. db_name is the name of your database, and /path/to/backup_folder/ is obviously where you want to store backups. The --login-path=local switch will look in the home directory of whoever is running this bash script and see if there is a login file there (must be readable by the current user and accessible by no one else).
#!/bin/bash
#$NOW will provide you with a timestamp in the filename for the backup
NOW=$(date +"%Y%m%d-%H%M%S")
DB=/path/to/backup_folder/"$NOW"_db_name.sql.gz
mysqldump --login-path=local db_name | gzip > "$DB"
#You could change permissions on the created file if you want
chmod 640 "$DB"
I save that file as db_backup.sh inside of the /usr/local/bin/ folder and make sure it is readable/executable by the user who is going to be doing the db backups. Now I can run # db_backup.sh from anywhere on the system and it will work.
To make it a cron, I put a file called 'db_backup' (the name doesn't really matter) inside my /etc/crond.d/ folder that looks like this (user_name is the user who is supposed to run the backup script)
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:usr/local/bin
MAILTO=root
HOME=/
# "user_name" will try to run "db-backup.sh" every day at 3AM
0 3 * * * user_name db-backup.sh
I think that you have some strange char on your password string. Maybe a "`" character and this why you are getting both errors.
The first from mysqldump because you don't have specified any database (because the broken string) and the second from shell saying that cannot find -h command (next string after password)
Try to escape $backup_command before system_call (http://www.php.net/escapeshellcmd)
system(escapeshellcmd($backup_command));
You should not use -p option anyway, as this can be read out by anybody on the system via ps aux.
It is a much better way to specify your credentials for mysqldump in an option file:
[client]
host = localhost
user = root
password = "password here"
Then you use call your script like this:
mysqldump --defaults-file=/etc/my-option-file.cnf ...other-args
This way, there is no password exposure on your system to other users. It might even also fix your proglem with unescaped complex passwords.
I would also recommend you to look at this project to get some more ideas:
mysqldump-secure
Do not keep space between -p and $database_pass. You need to write the password
immediately after the -p, without a space.
$backup_command = "mysqldump -u" . $database_user . " -p" . $database_pass . " -h " .
$database_server . " " . $database_target . " > " . $sql_backup_file;
hope will work for you
You need to supply database name. If you want to ahve backup for all please add --all-databases and Also remove spacing between -p and password
"mysqldump -u" . $database_user . " -p" . $database_pass . " -h " .
$database_server . " --all-databases" . $database_target . " > " .
$sql_backup_file;
Try this by adding the space between -u and username
$backup_command = "mysqldump -u " . $database_user . " -p" . $database_pass . " -h " . $database_server . " " . $database_target . " > " . $sql_backup_file;
^
If the database server located at the localhost remove the host param
$backup_command = "mysqldump -u " . $database_user . " -p" . $database_pass . " " . $database_target . " > " . $sql_backup_file;
i cannot seem to create a connection to Mysql database from the following code. Can you please let me know if there is anything missing from this code. Since i have not used php code much i might have missed out something.
on submitting code im getting the following error
Warning: mysql_connect() [function.mysql-connect]: Host '31.170.160.80' is not allowed to connect to this MySQL server in /home/a5847996/public_html/check_db.php on line 15
here is the code im using
<body>
<?php
$server = "-";
$database = "-";
$username = "-";
$password = "**************";
$mysqlConnection = mysql_connect($server, $username, $password);
if (!$mysqlConnection)
{
echo "Please try later.";
}
else
{
mysql_select_db($database, $mysqlConnection);
}
?>
</body>
Go to your MySQL Prompt and issue this command.
$ mysql -u root -p
Enter password:
mysql> use mysql
mysql> GRANT ALL ON *.* to root#'31.170.160.80' IDENTIFIED BY 'your-root-password';
mysql> FLUSH PRIVILEGES;
Don't forget to assign your parameters !