How do I stop DBI connect from adding #localhost to my username? - perl

This is a newbie question - don't overthink it!
I'm trying to make a connection from perl to a remote server's MySQL database that I can connect to just fine from PHP or from TOAD, so I know clearly what my host, dbname, username and password are.
But when I try to do this from my new installation of Strawberry Perl using the Padre IDE, something somewhere ( probably some config file ?) keeps insisting on adding '#localhost' to my username.
Oh, I'm using perl, v5.10.1 (*) built for MSWin32-x86-multi-thread.
I'm trying:
my $DBHOST = "50.88.64.41";
my $DBNAME = "keystrokes";
my $DBUSER = "wade\#'50.88.64.41'";
...
# MySQL database configuration
my $dsn = "DBI:mysql:database=$DBNAME,host=$DBHOST,port=3306";
So I get this output ( slightly sanitized for privacy):
DBI connect('database=keystrokes,host=50.88.64.41,port=3306','wade#50.88.64.41',...) failed: Access denied for user 'wade#50.88.64.41'#'localhost' (using password: YES) at C:\Bitnami\wampstack-8.0.8-0\apache2\htdocs\perl\firstscript.pl line 2.
Whether I put in my username as "wade" or as "wade#50.88.64.41" or "wade#'50.88.64.41'" SOMETHING then adds "#'localhost' to it, then tries to use that as the username, which of course fails, because I'm not on that machine and it is not my localhost.
Any ideas how to fix this? I couldn't find any explicit answers in this forum.
Thank you!

I think your problem is the exact opposite of what you think it is. MySQL is doing the right thing by adding '#localhost' to your username. What you don't need is the `#50.88.64.41' that you add. I've been connecting Perl programs to MySQL databases for fifteen years and I've never needed to add something like that to a username.
The '#host' that you'll see in MySQL connection messages is added by the MySQL client library when making the connection - by looking at the IP address that the connection request is coming from.
So just change your code to:
my $DBUSER = 'wade';
and it should work.
Traditionally, you'll see two rows for each user in the mysql.user table. One with a host of localhost and the other with a host of % (which means "any other host"). That makes it easier to control access to the database - it's common that you would only allow access to users who are on the same server as database.

Related

Having Trouble with a First Time Installation of PostgreSQL 14.1 on Ubuntu 18.04

I've been having a bit of trouble trying to install PostgreSQL 14 for the first time.
I would like to apologize in advance if this question has been asked in the manner that I am about to ask it, but I do not think it has. If it has been, please direct me to the appropriate location!
I've done a fair amount of Googling on the matter, and all the information that I find seems to be rather fragmented, or I end up following a spaghetti trail of hyperlinks (a la-do-this-and-follow-this-other-link-with-more-information-than-you-need-to-understand-this-other-required-portion).
Personally, I don't want to jump around to 50 different locations on the web to try and conjure up a piecemeal solution that I believe works, only to be proven wrong later. I want to know what to do and why it works. I've tried reading the documentation, and have given up on it, because to me, it seems to assume that the server has already been set up by a database administrator.
Instead of articulating my problem directly (as I seem to be having more trouble than I would like by trying to do so), I believe it would be easier to articulate my problem indirectly by stating what my expectations would be after installing PostgreSQL for the first time.
So to start, I will mention that I'm running Ubuntu 18.04.6 LTS, and am installing PostgreSQL 14.1 with the following command:
sudo apt install postgresql-14
Before continuing, I would like to add a side note in advance, that I do not want suggestions for an alternative OS or install method. I just want to be able to get "up and running" in a common-sense fashion from this exact point.
Moving on, I know that the aforementioned command creates a *nix user called postgres.
From here, I can now indirectly state my problem using an outline of what my goals and expectations are immediately after installing the software via that command.
After installing PostgreSQL via apt, these are my expected goals:
I want any client to be able to connect to the database server from any computer where a route exists from the client to the server.
For the sake of simplicity with these stated goals, when it is directly or implicitly stated that I am trying to connect a client to the database server, I am making the assumption that the client is able to, at a minimum, ping the machine that the server is running on, and vice versa.
For now, I'm not completely worried about the database being accessible from the public Internet.
I expect to be able to access the database from any computer on my LAN, whether it is an actual LAN, or some sort of logical LAN (like a WAN or a VPN).
If I change the PostgreSQL password of the postgres user, I expect that any client logging into the database  server via the postgres user will require the password.
This means if I want to change the password to some_password via \password postgres or ALTER USER postgres WITH PASSWORD 'some_password'; (I am assuming this is how you change the login password of a PostgreSQL user), then...
I expect running psql [-h host] -U postgres -W from any host...
That when I am prompted to enter the password... 
I can only log in by entering the exact password of some_password.
Entering any other arbitrary text for the password should not allow me to log in.
I am adding this as a requirement because previous install attempts have shown me that this is NOT the case.
I expect to be able to create a PostgreSQL user account other than postgres (e.g. db_user) with a password and have it be subject to the same requirements as the postgres user.
i.e. once the new account is given permission to log in, the same common-sense login requirements to log in must be imposed, i.e. you can't get in if you don't have the correct username/password combination. 
If the process to achieve the aforementioned can be explained in such a way that it can be understood with minimal mental friction, I would be extremely grateful.
Feel free to assume that my knowledge is on par with that of a undergraduate CS student who just completed their first year of university, who also understands Linux filesystems and basic computer networking. I just want the answer to be as accessible to as many people as possible, as I am sure I'm not the only person who has struggled with installing PostgreSQL, in spite of having a power user's level of computer literacy.
sudo apt install postgresql
sudo -u postgres psql
Set a password for this user with \password or the other method you mention
sudo vi /etc/postgresql/10/main/pg_hba.conf
Make the only uncommented nonblank line in this file be host all all all md5
sudo vi /etc/postgresql/10/main/postgresql.conf
uncomment listen_addresses line and set it to '*'
sudo service postgresql restart
When you make a new user, you should also make a new database which has the same spelling as the user does. Otherwise you will need to specify the database name when you try to log in with psql -U, such as psql -U newname -d postgres -h[hhh]. Should you actually be running 14 not 10, then you will need to change the paths of the config files you need to edit accordingly.

How to connect to database through JDBC in pgAdmin?

I have database connection settings and pgAdmin (it is all that i have). Can I connect to the database through the pgAdmin to make the necessary changes in the database using these settings? If so, how?
I have not found how I can do this. Other answers here (and in google too) suggest writing Java-code - this is not what i need. I want to use pgAdmin interface for it. Can I make changes without using Java-code?
Here example of settings that i have:
jdbc.driverClassName - org.postgresql.Driver
jdbc.url - jdbc:postgresql://localhost:4444/
jdbc.username - username
jdbc.password - password
I don't understand why people don't understand what he's trying to do. He just wants to connect to db using pgAdmin. He just doesn't understand how to use the connection string in pgAdmin.
Basically, just remove all prefixes and suffixes (eg. jdbc:postgresql://). So your hostname is localhost. Type localhost in host name, 4444 in port name as well as your username and password. Also give it a name in General tab Then you'll see the databases on the left. Click Tools > Query Tool and write your select/insert etc. scripts.

Unable to create connection in Oracle SQL developer . IO error

I cannot add a connection in oracle SQL developer installed.
The error I am getting is:
IO Error :The network adapter could not establish connection.
Also can anyone help me as to what user name and password it is asking?
Is there anything else I need to install.
SQL Developer is a tool which enables you to connect to the database - let's presume an Oracle one. Did you install it? If not, is there any available on the network? If not, you'll have to do that first (i.e. install a database - 11g Express Edition might be your choice, download it here), and then let SQL Developer connect to it.
As of username and password you'd use: database owner is SYS, but - you shouldn't use it for coding training - you'd rather create a new user, or unlock one of pre-installed; unless I'm wrong, 11gXE contains the HR schema (Human Resources). In order to unlock it, establish a connection to the previously mentioned SYS user (remember which password you choose for it during the installation process) and choose the SYSDBA role. Then unlock the HR user and modify its password by issuing the following statements:
alter user hr account unlock;
alter user hr identified by hr;
Now create a new connection to HR user (this time choose the "default" role); you should be able to see its tables, run queries, etc.
Make sure your oracle database is up and running.. if you are using docker you need to restart the oracle docker image in this case.
I had similar issue then i tried to restart the oracle using
docker-compose -f docker-image-oracle.yml -d up (in this case you might have a diff name).
or you can start docker image directly from cmd .
Make sure that your hostname, listener port, sid/service name, username/password are correct.
You can use lsnrctl status to get the port number.
There are four things that come to my mind:
Is your Oracle Database installed and running? If not, install it and make sure it works.
Have you entered the correct username and password? Have you entered the correct hostname and/ or port number? If hostname and port number are wrong, check the listener.ora file (if you have the permission). Alternatively, you can look into the tnsnames.ora file.
Maybe your listener is not working after all. With the command "C:> lsnrctl status" (on cmd), you can check, if the listener works. If it doesn't, run the command "C:> lsnrctl start".
Are you using the right URL?
The following link may help you:
https://community.oracle.com/tech/welcome/discussion/2547624/io-error-the-network-adapter-could-not-establish-the-connection

FreeTDS - TSQL fails when password given in option, but succeeds when typed?

I'm trying to connect to a MS SQL Server trough PHP 5.6 with an Ubuntu 16.04 server. I'm forced to use this version of PHP, in order to assure compatibility with an 'ancient' application.
I installed PHP 5.6 and its modules (pdo, pdo_mysql, readline, etc.) trough ondrej's PPA without any trouble, but I wasn't able to find and install the 'mssql.so' module package needed for the application I'm trying to make work.
That's why I decided I would use ODBC (and PDO_ODBC) drivers in order to connect to the database.
In order to do so, I installed freetds with unixodbc and configured my files like this :
'odbcinst.ini' file :
'odbc.ini' file :
and finally the 'freetds.conf' file :
Ok, I guess all my files are well configured, but the next part starts to be a bit weird, let me explain you : when I try to connect to the MS SQL database using tsql and not giving the password in option but typing it when asked, the connexion works :
But when I try to give the same password as an option (-P), it doesn't work (I tried it at least 10 times with the correct password) !!!
--> tsql failed to open a session for the user 'WIPSOS-PHP'
The same problem happens when I try to use isql with one of my connectors I configured in the 'odbc.ini' file :
It seems to be related to the password, but I can't find the problem, can you please help me ?
I found a partial answer, and it was effectively related to the password: it seems like special characters are not well interpreted when present in the password given for the -P option of the tsql command.
Therefore, depending on you shell, you have to escape these characters in the string which completes an option. In my case, using bash, I had to use the '\' to escape the special character :
tsql -S hostname -D Database -U User -P pa\$\$word
And it now works!
But when I try to escape the special characters with '\' in the 'odbc.ini' file, it still doesn't seem to work :
My next question is : 'How to escape a special character in a '.ini' file ?' or 'Is there something wrong in my configuration ?'
EDIT: I know it was a long time ago, but I had a configuration problem related to the hostname in the "freetds.conf" file. In fact, I was using the server's local network DNS name instead of its IP adress(which works).
I hope my issue will at least help some people, it isn't easy to deal with TSQL and FreeTDS...

Informix dbserver connections in sqlhosts via perl

I want to add a new Informix sever entry into sqlhosts, but I'm not quite sure how it will impact the existing connection.
Currently sqlhosts contains only one server entry...
dbserver onsoctcp 111.111.111.20 7101
The database handle is created within an existing perl module (db is a database on the server)...
my $dsn = "DBI:Informix:db";
my $dbh = DBI->connect($dsn,"user","password");
Notice that "dbserver" is never referenced.
I want to add a test server to sqlhosts. Something like this...
dbserver onsoctcp 111.111.111.20 7101
dbserver_test onsoctcp 111.111.111.21 7101
With only one entry in sqlhosts, everything has been working fine. But my connection never references the server name in sqlhosts.
So, my question(s)...
Does Informix just try to use the only one available?
Will adding a second server entry in sqlhosts force me to include the server name in the connection string?
Thanks!
Informix client uses environment variables to resolve hosts and other configuration; check that INFORMIXDIR is set to the path where Informix CSDK is installed (I assume it is), and set INFORMIXSERVER to point to the new entry in sqlhosts. See this article in IBM knowledge base.
Alternatively, use db#server data source format:
my $dbh = DBI->connect("DBI:Informix:db#server", "user", "password");
Maybe it is a permissions issue? From the documentation:
Note that you might also be able to connect to other databases not
listed by DBI->data_sources using other notations to identify the
database. For example, you can connect to "dbase#server" if "server"
appears in the sqlhosts file and the database "dbase" exists on the
server and the server is up and you have permission to use both the
server and the database on the server and so on. Also, you might not
be able to connect to every one of the databases listed if you have
not been given at least connect permission on the database. However,
the databases listed by the DBI->data_sources method certainly exist,
and it is legitimate to try connecting to those sources.
http://search.cpan.org/~johnl/DBD-Informix-2013.0521/Informix.pm