Cannot connect to MySQL from Local host using Perl script












0














I have a perl script on a webserver that connects to a database on a remote server using this code:



   my $dbusername='myname';
my $dbpassword='mypassword';
my $dbdatabasename='mydatabase';
my $driver = "mysql";

my $host='x.x.x.x'; # the actual ip of the mysql server
my $port='1010'; # the port it listens on
my $dsn = "DBI:$driver:database=$dbdatabasename;host=$host;port=$port";
$dbh = DBI->connect($dsn,$dbusername,$dbpassword, {
PrintError => 0, RaiseError => 1, });


This all works fine. Now I want to move the Perl script to the same machine as the database. So I replaced the $host ip above with
my $host='localhost';



I also created a new user (called "webuser") on the database that matches hosts "localhost" instead of "%" and I use the user name and password for that connection in my new script. (I can connect to the database using that user from MySQL Workbench).



But when I run the scrpt I always get
"Access denied for user 'webuser'@'localhost' "



What else do I need to do?



It does look like a permissions issue (thanks ikegami), it's the same error I would expect if the password was incorrect. But I have carefully checked it, and I can successfully connect from the command line using exactly the same parameters:
mysql -u webuser -pMyPassword -h localhost -P 1010 MyDBname



Could there be a subtle difference between the mysql.exe program and the mysql driver used by the DBI module?



later:
One difference I have found is that when I connect from the command line I have to specify the port number that the server is listening on. If I don't then I get the error "Can't connect to MySQL Server on 'localhost'".



But in the Perl program whatever I use for the port number,or if I don't specify a port at all, I get "Access denied for user 'webuser'@'localhost' ".
In fact I get the same if I specify a non-existent database name.



I wonder if the sql server is denying access because the request is coming from a program in the webserver area (public_html). Is that a possibility?










share|improve this question




















  • 2




    localhost uses Unix Socket on Linux and shared memory on MSWin. If you want to use TCP/IP, use 127.0.0.1.
    – choroba
    Nov 12 '18 at 17:01












  • Substituting "127.0.0.1" for "localhost" still gives the error: "Access denied for user 'webuser'@'localhost' "
    – kaj66
    Nov 12 '18 at 17:30






  • 1




    Sounds like it did connect, but it's a permission issue.
    – ikegami
    Nov 12 '18 at 19:57










  • Does it work if you leave the script unchanged and use the actual IP address of the server?
    – Chris Turner
    Nov 13 '18 at 10:15










  • Chris Turner - I get a different error: Can't connect to MySQL server on 'myipaddress' (111)
    – kaj66
    Nov 13 '18 at 10:27
















0














I have a perl script on a webserver that connects to a database on a remote server using this code:



   my $dbusername='myname';
my $dbpassword='mypassword';
my $dbdatabasename='mydatabase';
my $driver = "mysql";

my $host='x.x.x.x'; # the actual ip of the mysql server
my $port='1010'; # the port it listens on
my $dsn = "DBI:$driver:database=$dbdatabasename;host=$host;port=$port";
$dbh = DBI->connect($dsn,$dbusername,$dbpassword, {
PrintError => 0, RaiseError => 1, });


This all works fine. Now I want to move the Perl script to the same machine as the database. So I replaced the $host ip above with
my $host='localhost';



I also created a new user (called "webuser") on the database that matches hosts "localhost" instead of "%" and I use the user name and password for that connection in my new script. (I can connect to the database using that user from MySQL Workbench).



But when I run the scrpt I always get
"Access denied for user 'webuser'@'localhost' "



What else do I need to do?



It does look like a permissions issue (thanks ikegami), it's the same error I would expect if the password was incorrect. But I have carefully checked it, and I can successfully connect from the command line using exactly the same parameters:
mysql -u webuser -pMyPassword -h localhost -P 1010 MyDBname



Could there be a subtle difference between the mysql.exe program and the mysql driver used by the DBI module?



later:
One difference I have found is that when I connect from the command line I have to specify the port number that the server is listening on. If I don't then I get the error "Can't connect to MySQL Server on 'localhost'".



But in the Perl program whatever I use for the port number,or if I don't specify a port at all, I get "Access denied for user 'webuser'@'localhost' ".
In fact I get the same if I specify a non-existent database name.



I wonder if the sql server is denying access because the request is coming from a program in the webserver area (public_html). Is that a possibility?










share|improve this question




















  • 2




    localhost uses Unix Socket on Linux and shared memory on MSWin. If you want to use TCP/IP, use 127.0.0.1.
    – choroba
    Nov 12 '18 at 17:01












  • Substituting "127.0.0.1" for "localhost" still gives the error: "Access denied for user 'webuser'@'localhost' "
    – kaj66
    Nov 12 '18 at 17:30






  • 1




    Sounds like it did connect, but it's a permission issue.
    – ikegami
    Nov 12 '18 at 19:57










  • Does it work if you leave the script unchanged and use the actual IP address of the server?
    – Chris Turner
    Nov 13 '18 at 10:15










  • Chris Turner - I get a different error: Can't connect to MySQL server on 'myipaddress' (111)
    – kaj66
    Nov 13 '18 at 10:27














0












0








0







I have a perl script on a webserver that connects to a database on a remote server using this code:



   my $dbusername='myname';
my $dbpassword='mypassword';
my $dbdatabasename='mydatabase';
my $driver = "mysql";

my $host='x.x.x.x'; # the actual ip of the mysql server
my $port='1010'; # the port it listens on
my $dsn = "DBI:$driver:database=$dbdatabasename;host=$host;port=$port";
$dbh = DBI->connect($dsn,$dbusername,$dbpassword, {
PrintError => 0, RaiseError => 1, });


This all works fine. Now I want to move the Perl script to the same machine as the database. So I replaced the $host ip above with
my $host='localhost';



I also created a new user (called "webuser") on the database that matches hosts "localhost" instead of "%" and I use the user name and password for that connection in my new script. (I can connect to the database using that user from MySQL Workbench).



But when I run the scrpt I always get
"Access denied for user 'webuser'@'localhost' "



What else do I need to do?



It does look like a permissions issue (thanks ikegami), it's the same error I would expect if the password was incorrect. But I have carefully checked it, and I can successfully connect from the command line using exactly the same parameters:
mysql -u webuser -pMyPassword -h localhost -P 1010 MyDBname



Could there be a subtle difference between the mysql.exe program and the mysql driver used by the DBI module?



later:
One difference I have found is that when I connect from the command line I have to specify the port number that the server is listening on. If I don't then I get the error "Can't connect to MySQL Server on 'localhost'".



But in the Perl program whatever I use for the port number,or if I don't specify a port at all, I get "Access denied for user 'webuser'@'localhost' ".
In fact I get the same if I specify a non-existent database name.



I wonder if the sql server is denying access because the request is coming from a program in the webserver area (public_html). Is that a possibility?










share|improve this question















I have a perl script on a webserver that connects to a database on a remote server using this code:



   my $dbusername='myname';
my $dbpassword='mypassword';
my $dbdatabasename='mydatabase';
my $driver = "mysql";

my $host='x.x.x.x'; # the actual ip of the mysql server
my $port='1010'; # the port it listens on
my $dsn = "DBI:$driver:database=$dbdatabasename;host=$host;port=$port";
$dbh = DBI->connect($dsn,$dbusername,$dbpassword, {
PrintError => 0, RaiseError => 1, });


This all works fine. Now I want to move the Perl script to the same machine as the database. So I replaced the $host ip above with
my $host='localhost';



I also created a new user (called "webuser") on the database that matches hosts "localhost" instead of "%" and I use the user name and password for that connection in my new script. (I can connect to the database using that user from MySQL Workbench).



But when I run the scrpt I always get
"Access denied for user 'webuser'@'localhost' "



What else do I need to do?



It does look like a permissions issue (thanks ikegami), it's the same error I would expect if the password was incorrect. But I have carefully checked it, and I can successfully connect from the command line using exactly the same parameters:
mysql -u webuser -pMyPassword -h localhost -P 1010 MyDBname



Could there be a subtle difference between the mysql.exe program and the mysql driver used by the DBI module?



later:
One difference I have found is that when I connect from the command line I have to specify the port number that the server is listening on. If I don't then I get the error "Can't connect to MySQL Server on 'localhost'".



But in the Perl program whatever I use for the port number,or if I don't specify a port at all, I get "Access denied for user 'webuser'@'localhost' ".
In fact I get the same if I specify a non-existent database name.



I wonder if the sql server is denying access because the request is coming from a program in the webserver area (public_html). Is that a possibility?







mysql perl dbi






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 13:23

























asked Nov 12 '18 at 16:45









kaj66

315




315








  • 2




    localhost uses Unix Socket on Linux and shared memory on MSWin. If you want to use TCP/IP, use 127.0.0.1.
    – choroba
    Nov 12 '18 at 17:01












  • Substituting "127.0.0.1" for "localhost" still gives the error: "Access denied for user 'webuser'@'localhost' "
    – kaj66
    Nov 12 '18 at 17:30






  • 1




    Sounds like it did connect, but it's a permission issue.
    – ikegami
    Nov 12 '18 at 19:57










  • Does it work if you leave the script unchanged and use the actual IP address of the server?
    – Chris Turner
    Nov 13 '18 at 10:15










  • Chris Turner - I get a different error: Can't connect to MySQL server on 'myipaddress' (111)
    – kaj66
    Nov 13 '18 at 10:27














  • 2




    localhost uses Unix Socket on Linux and shared memory on MSWin. If you want to use TCP/IP, use 127.0.0.1.
    – choroba
    Nov 12 '18 at 17:01












  • Substituting "127.0.0.1" for "localhost" still gives the error: "Access denied for user 'webuser'@'localhost' "
    – kaj66
    Nov 12 '18 at 17:30






  • 1




    Sounds like it did connect, but it's a permission issue.
    – ikegami
    Nov 12 '18 at 19:57










  • Does it work if you leave the script unchanged and use the actual IP address of the server?
    – Chris Turner
    Nov 13 '18 at 10:15










  • Chris Turner - I get a different error: Can't connect to MySQL server on 'myipaddress' (111)
    – kaj66
    Nov 13 '18 at 10:27








2




2




localhost uses Unix Socket on Linux and shared memory on MSWin. If you want to use TCP/IP, use 127.0.0.1.
– choroba
Nov 12 '18 at 17:01






localhost uses Unix Socket on Linux and shared memory on MSWin. If you want to use TCP/IP, use 127.0.0.1.
– choroba
Nov 12 '18 at 17:01














Substituting "127.0.0.1" for "localhost" still gives the error: "Access denied for user 'webuser'@'localhost' "
– kaj66
Nov 12 '18 at 17:30




Substituting "127.0.0.1" for "localhost" still gives the error: "Access denied for user 'webuser'@'localhost' "
– kaj66
Nov 12 '18 at 17:30




1




1




Sounds like it did connect, but it's a permission issue.
– ikegami
Nov 12 '18 at 19:57




Sounds like it did connect, but it's a permission issue.
– ikegami
Nov 12 '18 at 19:57












Does it work if you leave the script unchanged and use the actual IP address of the server?
– Chris Turner
Nov 13 '18 at 10:15




Does it work if you leave the script unchanged and use the actual IP address of the server?
– Chris Turner
Nov 13 '18 at 10:15












Chris Turner - I get a different error: Can't connect to MySQL server on 'myipaddress' (111)
– kaj66
Nov 13 '18 at 10:27




Chris Turner - I get a different error: Can't connect to MySQL server on 'myipaddress' (111)
– kaj66
Nov 13 '18 at 10:27












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266584%2fcannot-connect-to-mysql-from-local-host-using-perl-script%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266584%2fcannot-connect-to-mysql-from-local-host-using-perl-script%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud