mysqldump cannot connect using socket - bash

This issue has been racking my brain for a few hours. I have been trying to use mysqldump to dump a database, using:
mysqldump --protocol=socket -S /var/run/mysqld/mysqld.sock database`
However, I keep getting:
1045: Access denied for user 'root'#'localhost' (using password: NO) when trying to connect
I am on localhost and running under root (sudo su).
Root#localhost is allowed in the mysql user table.
I can use > mysql to view all of the databases, but mysqldump will not work.
I do not know the root password (system generated).
I have tried adding the socket to the my.conf like so and restarting the mysql server:
[mysqldump]
socket = /var/run/mysqld/mysqld.sock
Any help would be appreciated!

Even though you are connecting via the socket, you must still give the user root
If root#localhost has no password then do this:
mysqldump -uroot --protocol=socket -S /var/run/mysqld/mysqld.sock database
If root#localhost has a password then do this:
mysqldump -uroot -p --protocol=socket -S /var/run/mysqld/mysqld.sock database
If running
mysql
lets you login with specifying -uroot, try not specifying the socket either
mysqldump database
I just noticed that the socket you specified for mysqldump is
[mysqldump]
socket = /var/run/mysqld/mysqld.sock
You need to make sure the socket is defined under the [mysqld] section of my.cnf as well
If this does not exist
[mysqld]
socket = /var/run/mysqld/mysqld.sock
then run this query
SHOW VARIABLES LIKE 'socket';
and make sure of the socket file's name and path.
You could have you System DBA add a custom user for you
GRANT ALL PRIVILEGES ON *.* TO tyler#localhost;
Then, you can run
mysqldump -utyler --protocol=socket -S /var/run/mysqld/mysqld.sock database
This is not secure. tyler should have a password. So, run this:
SET SQL_LOG_BIN=0;
GRANT ALL PRIVILEGES ON *.* TO tyler#localhost IDENTIFIED BY 'tylerspasssword';
then you can do
mysqldump -utyler -p --protocol=socket -S /var/run/mysqld/mysqld.sock database
Give it a Try !!!

I found the solution! The socket does not hold the credentials itself. They are stored in the /root/.my.cnf configuration file instead. Mine only had the username and password for the mysql command. I needed to add [mysqldump] to it as well. Here is what my /root/.my.cnf file looks like now:
[mysql]
user=root
pass=myawesomepass
[mysqldump]
user=root
pass=myawesomepass

Related

pg_restore ignores .pgpass and PGPASSWORD environment variable

I want to import a backup using pg_restore without a password prompt.
I tried several options but after I run the script it will always ask for a password. pg_dump is working but not pg_restore. I can run the pg_restore command if I enter my password but I want a passwordless command or at least I don't want to enter my password because the script has to work without user interaction.
What is working for me:
PGPASSWORD=xyz pg_dump -h localhost -U user -Fc database > ~/dump_prod.pgsql
What is NOT working
1.)
PGPASSWORD=xyz pg_restore -h localhost -d database -U user -W --clean --no-owner ~/dump_prod.pgsql
2.)
pg_restore --dbname=postgresql://user:pass#localhost:5432/db -W --clean --no-owner ~/dump_prod.pgsql
3.)
touch ~/.pgpass
echo "*:*:*:*:password > ~/.pgpass
chmod 0600 ~/.pgpass
pg_restore -h localhost -d db -U user -W --clean --no-owner ~/dump_prod.pgsql
any ideas?
Regards
As per the doc, -W will prompt for a password. -w will not
-w
--no-password
Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as
a .pgpass file, the connection attempt will fail. This option can be
useful in batch jobs and scripts where no user is present to enter a
password.
-W
--password
Force pg_restore to prompt for a password before connecting to a database.
The .pgpass file worked for me.
My setup:
I am restoring DB into the postgres instance running as Docker container.
The postgres instance is run using command:
docker run --name postgres_db -p 5432:5432 -e POSTGRES_PASSWORD=admin -d postgres:9.6
pg_restore is present but it is available as separate tool, outside docker setup.
The ~/.pgpass file entry looks like this:
localhost:5432:db_name:user:password
Where:
db_name is the target db name that one is going to restore.
user is the name of the user that is going to perform the restore - in my case an admin user. i.e. postgres user in the postgres instance.
password - admin user's password. i.e. admin

Run postgres query from another server, in bash

I have a Ubuntu server that processes documents and another that have the database (postgresql 9.3).
I ran psql -? to understand how to connect to another DB and the final command would be:
psql -U postgres -W -h 1.2.3.4 -d testdb -p 5432
It works, but I must type the password after the command is issued.
I was trying to adapt this command in a bash script:
#!/bin/bash
psql -U postgres -W mypassword -h 1.2.3.4 -d testdb -p 5432 << EOF
select * from mytable;
\q
EOF
Needless to say this is not the right command.
Also, the password does not get recognized as a password, reporting the error:
psql: warning: extra command-line argument "mypassword" ignored
Password for user postgres:
psql: FATAL: password authentication failed for user "postgres"
FATAL: password authentication failed for user "postgres"
In another server, where the script runs on the local DB, my working script is:
su - postgres -c "psql myDatabase" << EOF
select * from "myOtherTable";
\q
EOF
The question is simple, how can I write the right command for bash, to connect to another database with user/password and issue commands?
A link I tried, but password seems to not be set:
run psql query in bash
Thanks!
Try
PGPASSWORD=yourpass psql -U postgres -W -h 1.2.3.4 -d testdb -p 5432
See: https://www.postgresql.org/docs/9.3/static/libpq-envars.html
or ~/.pgpass file https://www.postgresql.org/docs/9.3/static/libpq-pgpass.html

Access Denied for User ODBC # localhost

i have installed Mysql on Windows7, i used mysql with mysql command line client & i also use Mysqldump in windows cmd, & it was working without any problem. But today, i tried to export database using mysqldump with this command in cmd
mysqldump –u root -p mypassword db_name > f:\mydb.sql
i tried many other commands and i always see error
Access Denied for User 'ODBC'#'localhost' (using password: yes) when trying to connect
as you can see, in mysqldump command i am using root as user then why i get user ODBC error ? one more thing, using mysql command line client i am still using mysql normally without any problem using root as user. i also tried to login in cmd with this command
mysql –u root -p mypassword
but still same error. and my password is 100% correct. kindly tell me how to solve this problem. Thanks
Have you tried storing pass in the cfg file? http://dev.mysql.com/doc/refman/5.7/en/password-security-user.html
try from command line:
mysql -u root -p
and then, when you are being asked for password, just type it.
Try the same with mysqldump command without typing your password after -p.

cannot connect to template1: FATAL: role "_postgres" does not exist

Working on OS X 10.10, installed postgreSQL and PostGIS from
here, psql vers 9.3.5. I am having a hard time getting postgreSQL running.
I try to follow the OSM tutorial on psql and PostGis. But that seems rather hard... I am stuck here.
They suggest to create the user like:
sudo -u postgres createuser christoph
That did not work for me. I created the user like this:
$ sudo -u _postgres createuser -U postgres christoph
That worked without any problems. Then I tried this:
$ sudo -u _postgres createdb --encoding=UTF8 --owner=christoph gis
> cannot connect to template1: FATAL: role "_postgres" does not exist
How can _postgres not exist if I just created a role with it?!
I also do not understand why
$ createdb mydb
> -bash: createdb: command not found
does not work although
I added the path like:
$ export PSQL_DIR=/usr/local/pgsql/bin/
$ export PATH="$PSQL_DIR:$PATH"
before.
Executing the command like:
$ /usr/local/pgsql/bin/createdb mydb
> createdb: cannot create database: ERROR: permission denied to create database
I am looged in with the user who installed psql...
Is that connected?!
It is very easy to confuse system users (stored in /etc/passwd) with Postgres roles (stored inside the database cluster).
In your first command, you mention both types of user:
sudo -u _postgres means "use the system user _postgres to execute the following command"
the -U postgres in the command means "connect to the Postgres cluster with the role postgres"
However, in the next command:
sudo -u _postgres createdb --encoding=UTF8 --owner=christoph gis
You specify the same sudo -u _postgres, but are missing the -U argument to the actual Postgres command (you say you want christoph to own the new DB, but you don't say "I am christoph").
The Postgres command-line tools have a default behaviour of guessing that the Postgres role name is the same as the system user name. Since the system user is named _postgres (with leading underscore) and the Postgres role is named postgres (no leading underscore), this assumption fails, and you can't connect.

Postgresql: cannot access psql after changing user password

After a few weeks of not using my postgres db, I need to use it again. To my knowledge I haven't changed anything in the configuration since then, but I cannot login as my normal superuser.
psql -d %dbname% -U %username%
I get: psql: FATAL: password authentication failed for user "%username%"
Here's what I've done so far:
Sudo into the db: sudo -u %OS_username% psql
Test create a user to verify commands work: CREATE USER test WITH PASSWORD 'test'; (it worked)
Change my password: ALTER USER %username% PASSWORD 'test'
Reboot the server: pg_ctl restart -D ~/Library/Application\ Support/Postgres/var-9.3
Then I try psql -d %dbname% -U %username% again, but it fails with the same message.
Below are the active connection configurations from my pg_hba.conf:
local all all trust
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
local all %username% trust
I have only added the last line today, and made no other changes. PG Commander/PGAdmin refused to login as normal (as they always have done). Why and where could the credentials have been changed?
Edit: I should also note that I cannot login with the test user I created.

Resources