perl script to connect to mysql server port 3307 - mysql

I am trying to connect to a mysql server which is running at port 3307. How do I connect to the server? I do not see any other way to specify port. I am using like this:
#!/usr/bin/perl
use Mysql;
$host = "localhost";
$database = "abc";
$tablename = "def";
$user = "uuu";
$pw = "ppp";
$connect = Mysql->connect($host, $database, $user, $pw) or die "Cannot connect to MySQL server\n";
I want to use MySQL package and not DBI.
Thank you.

You are mistaken. You want to use DBI and not Mysql. The Mysql module became obsolete 12 years ago, when it was replaced with a compatibility module that's just a wrapper around DBI. Even the compatibility module has been removed from the current distribution; you have to install an old DBD::mysql just to get it (it last shipped in DBD-mysql 3.0008, released back in 2006).
#!/usr/bin/perl
use strict;
use DBI;
my $host = "localhost";
my $database = "abc";
my $port = 3307;
my $tablename = "def";
my $user = "uuu";
my $pw = "ppp";
my $dbh = DBI->connect("DBI:mysql:database=$database;host=$host;port=$port",
$user, $pw)
or die "Cannot connect to MySQL server\n";

Try specifying host like localhost:3307

For me...Following seems to be working ...
#!/usr/bin/perl
use strict;
use DBI;
my $host = "rajeshk-W7";
my $database = "rajesh";
my $port = 3307;
my $tablename = "def";
my $user = "rajesh";
my $pw = "rajesh123";
#my $dbh = DBI->connect("DBI:mysql:rajesh:rajeshk-W7","rajesh","rajesh123") or die "Cannot connect to MySQL server\n";
my $dbh = DBI->connect("DBI:mysql:database=$database;host=$host",$user, $pw) or die "Cannot connect to MySQL server\n";
where i dint mentioned port. When i add port, its unable to connect.

Related

Cannot connect to my Mysql DB with PHP7.0 and Apache2 (2 different VM)

I'm trying to connect to my MySQL DB located on 192.168.23.140 from my Web Frontend on .23.139
Here is my code :
$servername = "192.168.23.140";
$username = "web";
$password = "rootnetwork";
$dbname = "test";
try {
$conn = new PDO('mysql:host=$servername; dbname=$dbname', $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
Here is what i get :
Connection failed: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
I've been looking for some answers on the internet but none of them work.
Thank you for your help
$variables are not expanded in a single quotes string literal, they are only expanded in a double quoted string literals.
So
$conn = new PDO("mysql:host=$servername; dbname=$dbname", $username, $password);
You will also have to make sure that this MYSQL user account web is setup to allow connections from ip addresses that are not the ip of the machine running MySQL

Remote db connection from another remote ip perl

I am trying to connect one remote server where I have installed MySQL but it is taking localhost
Remote server where mySQL is installed: 131.116.220.228
Remote server from where db needs to be connected: 131.116.220.220 ('ov046hanpgate01.ddc.teliasonera.net')
use DBI;
print "Testing 1\n";
$DSN="DATABASE=MySQL80;port=3306;host=131.116.220.228";
print "Testing 2\n";
my $dbh = DBI->connect("dbi:mysql:$DSN", "ShrutiTest", "Shruti#92", {PrintError => 0})
or die "Couldn't connect to database: " . DBI->errstr;
print "Testing 3\n";
$dbh->disconnect;
error: Couldn't connect to database: Host 'ov046hanpgate01.ddc.teliasonera.net' is not allowed to connect to this MySQL server at D:\Integrations\OWF\Scripts\test27july.pl line 6.
Looks like you need to allow the user ShrutiTest to connect from the ip where you are running the script, viz. 131.116.220.220
It can be done at the MySQL server by any admin:
GRANT ALL PRIVILEGES ON *.* TO 'ShrutiTest'#'131.116.220.220'
This syntax "DBI:mysql:database=$db;host=$host;port=$port" worked for me in the perl script to connect to a remote MySQL DB from an Unix server.
Solved the same. Code used is:
#!/usr/bin/perl -w
use DBI;
## mysql user database name
$db ="Buffering";
## mysql database user name
$user = "ShrutiBuffer";
## mysql database password
$pass = "Shruti1234";
## user hostname : This should be "localhost" but it can be diffrent too
$host="131.116.220.228";
$port="3306";
## SQL query
$query = "select * from buffering.insert_data";
my $dbh = DBI->connect("DBI:mysql:database=$db;host=$host;port=$port",$user, $pass);
$sqlQuery = $dbh->prepare($query)
or die "Can't prepare $query: $dbh->errstr\n";
$sqlQuery->execute
or die "can't execute the query: $sqlQuery->errstr";
print "values are:: ";
while (#row= $sqlQuery->fetchrow_array()) {
print join(", ", #row), "\n";
}
$sqlQuery->finish;
exit(0);

Why won't this Perl DBI/DBD MySQL (MariaDB) connect to locahost work?

use DBI;
my $dbh = DBI->connect ('DBI:mysql:host=localhost;database=test', 'user', 'password')
or die "failed to connect\n";
Results in an error message:
DBI connect('host=localhost;database=test','user',...) failed: Can't connect to MySQL server on 'localhost' (10061) at connect.pl line 3.
using: DBI 1.641, perl v5.26.2 on Windows 10 and running MariaDB 10.2.14
mysqld is running on the computer, and the server can be connected to with the standard "mysql test -u user -p" command
On another PC running Windows 7 with a very similar setup - but with DBI 1.636 - the connect() succeeds with the same perl code. Is is possible that DBI:mysql and Windows 10 aren't compatible?
It seems you have a space after the word "connect", anyway...try this:
my $driver = "mysql";
my $database = "DBname";
my $ip = "localhost";
my $db = "DBI:$driver:DBNAME:$ip:database=$database";
my $username = "mysqluser";
my $password = "mysqlpass";
my $cn = DBI->connect($db, $username, $password)
or print "Couldn't connect to database: " . DBI->errstr . "\n\n";

MySQL connection not working on Windows

Any clue as to why this would not work in a Windows environment?
It works under Linux
function dbLink(){
$host = 'localhost';
$user = '';
$pass = '';
$base = '';
mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db($base) or die(mysql_error());
}
dbLink();
Thanks
Try 127.0.0.1 instead. Your localhost might be set to something different.
Make sure PHP for windows has php-mysql installed and enabled.

Problems with Godaddy Database connections

I am having problem with connecting to Database in godaddy. I do not know is it my code that is giving the problem or godaddy that is giving the problem.
I tried 2 different ways of coding to connect to database;
Below is the working code:
$hostname = "xxx.db.xxx.hostedresource.com";
$username = "xxx";
$dbname = "xxx";
$password = "xxx";
mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($dbname);
Below is the non-working code:
define("DBHOST","xxx.db.xxx.hostedresource.com");
define("DBNAME","xxx");
define("DBUSER","xxx");
define("DBPASS","xxx");
mysql_connect(DB_HOST,DB_USER,DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME) or die(mysql_error());
Or is it because godaddy can't use define function? Thanks guys.
You're defining DBHOST but using DB_HOST. The same problem with all other defined values as well. define is NOT banned on GoDaddy.