I am writing a Perl script to connect to Sybase machine having particular database name called PPDB but it is not working.
my $dbh = DBI->connect( "dbi:Sybase:server=sybase IP address; database=PP_DB;port=5000","sa", "password" );
This is the error I'm getting
Can't connect to data source 'dbi:Sybase ASE:server=server
name;database=PP_DB;port=5000' because I can't work out what driver to
use (it doesn't seem to contain a 'dbi:driver:' prefix and the
DBI_DRIVER env var is not set) at ./update_database.pl line 6
Don't know what's wrong in this. Your suggestions are welcome.
The server parameter in the data source name expects a Sybase server name. If you want to specify an IP address then you need to use host instead:
my $dbh = DBI->connect( 'dbi:Sybase:host=<sybase IP address>;database=PP_DB;port=5000', qw/ sa password /);
Related
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.
Since I'm programming a Perl script to monitor the SAP HANA database, I'm experiencing problems with the connection timeout. I don't know how to set the timeout. This is my connection string:
use DBI;
use DBD::ODBC;
$dbh = DBI->connect("dbi:ODBC:dsn=$dsn");
Meanwhile in another script to monitor a Sybase database it works properly:
use DBI;
dbh = DBI->connect("dbi:Sybase:server=$dbsvr;database=$dbname;".
timeout=$timeout;"."loginTimeout=$timeout", $user, $pass);
I think the problem comes with the ODBC. Which is the way to implement a timeout using ODBC connection?
I don't believe there is a way to set SQL_ATTR_LOGIN_TIMEOUT on the connection handle via DBD::ODBC. In any case, few ODBC drivers do anything with it. However, if you are on UNIX and using the unixODBC driver manager - see Setting ODBC driver environment variables automatically
You basically add "DMConnAttr = CONNECTION_ATTRIBUTE=value" to you odbc.ini file where CONNECTION_ATTRIBUTE is an ODBC connection attribute (e.g., SQL_ATTR_CONNECTION_TIMEOUT) and value is what you want to set it to.
It is commom to confused with connection and query timeout:
DMStmtAttr = SQL_ATTR_QUERY_TIMEOUT=2
DMConnAttr = SQL_ATTR_CONNECTION_TIMEOUT=2
Connection may be ok but query response take more time than normal
I have a problem that I have never seen before with Mysql replication (Master/Slave):
It works excellent when I execute on my PHP scripts, on the mysql console queries like "INSERT INTO". They are perfectly replicated on the mysql slave machine.
But: Not if I do this same operations on a Perl script, using Perl DBI. Then its only stored on the Mysql master server. In the mysql.log file on the slave server, nothing arrives. I have this problem since I migrated on the two servers from Mysql to MariaDB.
Is there something that a Perl DBI client should absolutely do that replicating would work?
I'm using this simple code to INSERT data:
#!/usr/bin/perl
use DBI;
$dsn = 'dbi:mysql:database=mysql;host=myhostname';
$dbh = DBI->connect($dsn, 'mouser', 'password',
{ RaiseError => 1, AutoCommit => 0 }) || exit(1);
...
$sth = $dbh->prepare("INSERT INTO mydatabase.mytable (user, domain) VALUES('$account_name', '$domain')");
$sth->execute();
$dbh->commit();
Is it possible that it does not replicate because DBI should use a mariadb driver? Like $dsn = 'dbi:mariadb:database=mysql;host=myhostname'; if that exists?
To be clear: Data inserted with your PHP script is still being replicated after switching to MariaDB, but data inserted with your Perl script isn't.
I found the reason why the replication failed with this simple Perl script:
There is written:
$dsn = 'dbi:mysql:database=mysql;host=myhostname';
...
$sth = $dbh->prepare("INSERT INTO mydatabase.mytable (user, domain) VALUES('$account_name', '$domain')");
This is not working, because DBI connects to the database "mysql" and not "mydatabase" (see "database=mysql"). Even if the INSERT INTO inserts the row directly in the database "mydatabase", MariaDB master server ignores it for the replication. Its strange that it worked perfectly with MySQL and not for MariaDB (version 5.5.46). Maybe its a bug in MariaDB, I don't know.
I am new to Perl Programming and database connectivity. Can anyone please let me know step by step procedure to write Perl Script for Oracle Database connectivity.
My Perl Verion is:
This is perl 5, version 22, subversion 0 (v5.22.0) built for MSWin32-x64-multi-thread
Copyright 1987-2015, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
I have tried the below:
my $db = DBI->connect("dbi:Oracle:Local","SYSTEM","SYSTEM") or die print ("could not connect! $DBI::errstr \n");
Since I don't know what is this "dbi:Oracle:Local" I could not able to connect to Database.
Can you please let me know what is dbi, Oracle, local. if it is Hostname and oracle database name, how can I find the same in my computer.
Do I need to set any ENV variable in Perl? If so, where and what I need to set?
dbi:Oracle lets DBI know which driver to use. If you're connecting to an Oracle database, you'll never change these.
Local is either an actual database name on the local system, or a name listed in TNSNAMES.ORA. Substitute the name of the local database you'd like to connect to.
The next two parameters are username and password.
If you're connecting remotely, or need to do something more elaborate, consult the docs, or one of the many available guides.
my $db = DBI->connect("dbi:Oracle:Local","SYSTEM","SYSTEM");
"dbi" is a string,
"Oracle" is the driver type,
"local" is the database name,
First "SYSTEM" is the username and second one is password.
use DBI;
my $dbh = DBI->connect( "dbi:Oracle:databaseName", 'username', 'passwd' ) or die($DBI::errstr, "\n");
First read the basic concept of Simple Database access using Perl DBI and SQL.
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