Could not connect to database: Access denied for user....
Le
Maminirina Hasina RAKOTONINDRAINY
Bonjour,
Je suis bloqué sur une erreur depuis quelques jour.
Je me connecte à une base de données mysql. J'ai le message d'erreur
suivant :
"DBI connect('articom;ns366702.ovh.net','articom',) failed: Access
denied for user ''@'ns366702.ovh.net' to database 'articom' at cgi-bin/
perl.pl line 207
Could not connect to database: Access denied for user
''@'ns366702.ovh.net' to database 'articom' at cgi-bin/perl.pl line
207."
En faisant un diagnostic du programme, la ligne 30 ne retourne pas de
résultat.
Quelqu'un pourrait-il me filer un coup de main?
Merci.
Voici le code source :
{{
1 #!/usr/bin/perl -w
2
3 use strict;
4 use warnings;
5 use DBI;
6 use DBD::mysql;
7 use Text::CSV;
8 use CGI;
9 use Text::Iconv;
10 use Env;
11 use LWP::Simple;
12 use LWP::UserAgent;
13 use HTTP::Request;
14 use HTTP::Response;
15 use HTML::LinkExtor;
16
17 my $url= new CGI;
18 my $filename = $url->param("le_csv");
19 my $upload_filehandle = $url->upload("le_csv");
20 my $hostname=$ENV{SERVER_NAME};
21
22
23 #my $repertoire="/home/articom-web/public_html/";
24
25 print $url->header();
26
27 $hostname =~ s/^www\.//g;
28
29 #IP du SERVEUR
30 my $ip = $ENV{'SERVER_ADDR'};
31 my $host = "ns366702.ovh.net";
32 my $database = "articom";
33 my $user = "articom";
34 my $password = "";
35
36 if($ip eq "192.168.0.7") # adresse IP phenom
37 {
38 $host = "localhost";
39 $database = "articom";
40 $user = "articom";
41 $password = "";
42 }
43 elsif($ip eq "91.121.99.97") # Serveur 1 qui se connecte au serveur
2
44 {
45 $host = "ns366702.ovh.net";
46 $database = "articom";
47 $user = "articom";
48 $password = "";
49 }
50
51
52 elsif($ip eq "94.23.19.58") # Serveur 2 IP 1
53 {
54 $host = "localhost";
55 $database = "articom_un";
56 $user = "articom";
57 $password = "";
58 }
59 elsif($ip eq "91.121.62.106") # Serveur 2 IP 2
60 {
61 $host = "localhost";
62 $database = "articom_deux";
63 $user = "articom";
64 $password = "";
65 }
66 elsif($ip eq "87.98.221.158") # Serveur 2 IP 3
67 {
68 $host = "localhost";
69 $database = "articom_trois";
70 $user = "articom";
71 $password = "";
72 }
73 elsif($ip eq "87.98.131.220") # Serveur 2 IP 4
74 {
75 $host = "localhost";
76 $database = "articom_quatre";
77 $user = "articom";
78 $password = "";
79 }
80 if($filename ne '')
81 {
82 my $table_gratuits = "fiche_champs_gratuits";
83 my $table_payants = "fiche_champs_payants";
84 my $table_portail = "portails";
85
86 my $connect = DBI->connect ("DBI:mysql:$database;$host",$user,
$password);
87 my $changed = Text::CSV->new({sep_char => ';', binary => 1});
88 my $converter = Text::Iconv->new("iso-8859-1", "utf-8");
89
90 my $sql_portail="SELECT id FROM $table_portail WHERE
nom_domaine = '$hostname'";
91 my $query_portail = $connect->prepare($sql_portail);
92 $query_portail->execute;
93 my $portail;
94
95 $query_portail->bind_columns(\$portail);
96 $query_portail->fetch;
97 $query_portail->finish();
98
99
100 ## Supprimer les fiches de ce portail
101 my $sql_delete_fiv_payants="DELETE FROM fiche_champs_payants
102 WHERE fiche_champs_payants.id
103 IN (SELECT `fiche_champs_gratuits`.id FROM
`fiche_champs_gratuits`
104 WHERE `fiche_champs_gratuits`.id_portail =
$portail)";
105 my $delete_fiv_payants = $connect->prepare
($sql_delete_fiv_payants);
106 $delete_fiv_payants->execute;
107 $delete_fiv_payants->finish();
108
109 my $sql_delete_fiv_gratuits="DELETE FROM fiche_champs_gratuits
WHERE id_portail = $portail";
110 my $delete_fiv_gratuits = $connect->prepare
($sql_delete_fiv_gratuits);
111 $delete_fiv_gratuits->execute;
112 $delete_fiv_gratuits->finish();
113
114
115 my $tel_portable='';
116 my $tel_fixe= '';
117
118 my $zero=0;
119
120 #############################################
121 # fonction traitant les caractères spéciaux #
122 #############################################
123 sub treatspecialCaracter($){
124 my $x=shift;
125 $x =~ s/\'/ /g;
126 $x =~ s/\"/ /g;
127 $x =~ s/\',\)/\'\)/g;
128 return $x;
129 }
130 #############################################
131
132 while (<$upload_filehandle>)
133 {
134 if ($zero==0)
135 {$zero ++;}
136 else
137 {
138 my $converted=$converter->convert($_);
139 if ($changed->parse($converted))
140 {
141 my @columns = $changed->fields();
142
143 # Dans la colonne Telephone
144 # je remplace les espaces et les points
145 $columns[5]=~ s/ //g;
146 $columns[5]=~ s/\.//g; # le point doit etre echape
147
148 if($columns[5] =~ /^06.+/)
149 {
150 $tel_portable = $columns[5];
151 $tel_fixe = '';
152 }
153 else
154 {
155 $tel_fixe = $columns[5];
156 $tel_portable='';
157 }
158
159 ###############################################
160 # Insertion dans la table fiche champ gratuit #
161 ###############################################
162 my $tmp = "INSERT INTO $table_gratuits VALUES
(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
163 my $query = $connect->prepare($tmp);
164 my $res = $query->execute('',
165 &treatspecialCaracter
( $columns[0]),
166 &treatspecialCaracter
( $portail),
167 &treatspecialCaracter
( $tel_fixe),
168 '',
169 &treatspecialCaracter
( $tel_portable),
170 &treatspecialCaracter
( $columns[7]),
171 &treatspecialCaracter
( $columns[2]),
172 &treatspecialCaracter
( $columns[3]),
173 &treatspecialCaracter
( $columns[4]),
174 '',
175 '',
176 '',
177 '',
178 '',
179 ''
180 );
181 $query->finish();
182
183 ##############################################
184 # Insertion dans la table fiche champ payant #
185 ##############################################
186 if ($res==1) # si l'insertion dans table fiche
champ gratuit a été réalisé avec succés #
187 {
188 my $query_2 = $connect->prepare("INSERT INTO
$table_payants VALUES('', '', '', '', '')");
189 $query_2->execute;
190 $query_2->finish();
191 }
192 else # echec de l'insertion dasn table fiche champ
gratuit #
193 {
194 print "Erreur lors du charegement du fichier
CSV";
195 }
196 ##############################################
197
198 }
199 }
200 }
201 $connect->disconnect();
202 }
203
204 my $repertoire="../";
205 my $destination_table = "towns";
206 my %hash_town = ();
207 my $destination_connect = DBI->connect ("DBI:mysql:$database;
$host","articom","")
208 || die "Could not connect to database: "
209 . DBI-> errstr;
210
211 my $town_query = $destination_connect->prepare(" SELECT
code_postal, departement.nom_departement as nom_departement
212 FROM
fiche_champs_gratuits
213 JOIN departement
214 ON
departement.num_departement = ROUND((code_postal - (code_postal %
1000)) /1000)
215 ");
216
217
218 my($code_postal_0,$nom_departement);
219 $town_query->execute();
220 $town_query->bind_columns(undef, \$code_postal_0,\
$nom_departement);
221
222
223 while($town_query->fetch())
224 {
225 $hash_town{$code_postal_0}=$nom_departement;
226 }
227 $town_query->finish();
228
229 # purge les nom de fichiers FIV
230 my $purge_query = $destination_connect->prepare("UPDATE
fiche_champs_gratuits SET fichier = NULL");
231 $purge_query->execute;
232 $purge_query->finish();
233
234 my $destination_query = $destination_connect->prepare("SELECT
id,nom,ville,code_postal
235 FROM
fiche_champs_gratuits
236 ");
237 my($id,$nom, $ville,$code_postal);
238 $destination_query->execute();
239 $destination_query->bind_columns(undef, \$id,\$nom,\$ville,\
$code_postal);
240
241 my $update_query = $destination_connect->prepare("UPDATE
fiche_champs_gratuits SET fichier = ? WHERE id = ?");
242
243 my $contenu = "";
244
245 open(F, ">", "../map.txt");
246
247 while($destination_query->fetch())
248 {
249 my $nom_fichier = $hash_town{$code_postal}." ".$ville." ".
$nom;
250 $nom_fichier =~ s/[^\w]/-/g;
251 $nom_fichier =~ s/(-)+$//g;
252 $nom_fichier =~ s/(-)+/-/g;
253 $nom_fichier =~ tr/A-Z/a-z/;
254 print F $nom_fichier.".php php/fiches-
virtuelles.php?id=$id "."";
255 $update_query->execute($nom_fichier,$id);
256 $update_query->finish();
257 }
258 $destination_query->finish();
259
260 # purge noms fichiers ARTICLES
261 my $purge_articles=$destination_connect->prepare("UPDATE
article_libre SET fichier= NULL");
262 $purge_articles->execute;
263 $purge_articles->finish();
264
265 my $update_article=$destination_connect->prepare("UPDATE
article_libre SET fichier = ? WHERE id_article = ?");
266
267 my $articles_select=$destination_connect->prepare("SELECT
id_article, titre_gauche FROM article_libre");
268 my ($id_article, $titre_gauche);
269 $articles_select->execute();
270 $articles_select->bind_columns(undef, \$id_article, \
$titre_gauche);
271 while($articles_select->fetch())
272 {
273 my $nom_article=$titre_gauche;
274 $nom_article=~ s/[^\w]/-/g;
275 $nom_article=~ s/(-)+$//g;
276 $nom_article=~ s/(-)+/-/g;
277 $nom_article=~ tr/A-Z/a-z/;
278 print F $nom_article.".php php/article.php?id=
$id_article "."";
279 $update_article->execute($nom_article,$id_article);
280 $update_article->finish();
281 }
282
283 $articles_select->finish();
284
285 close(F);
286
287 $destination_connect->disconnect();
288
289 if($filename eq '')
290 {
291 print "<html><head></head><body><a href=\"http://$hostname/
\">Revenir</a></body></html>";
292 }
293 else
294 {
295 print "<html><head></head><body><a href=\"http://$hostname/
backoffice/php/ajouter_csv.php\">Revenir</a></body></html>";
296 }
}}
Je suis bloqué sur une erreur depuis quelques jour.
Je me connecte à une base de données mysql. J'ai le message d'erreur
suivant :
"DBI connect('articom;ns366702.ovh.net','articom',) failed: Access
denied for user ''@'ns366702.ovh.net' to database 'articom' at cgi-bin/
perl.pl line 207
Could not connect to database: Access denied for user
''@'ns366702.ovh.net' to database 'articom' at cgi-bin/perl.pl line
207."
En faisant un diagnostic du programme, la ligne 30 ne retourne pas de
résultat.
Quelqu'un pourrait-il me filer un coup de main?
Merci.
Voici le code source :
{{
1 #!/usr/bin/perl -w
2
3 use strict;
4 use warnings;
5 use DBI;
6 use DBD::mysql;
7 use Text::CSV;
8 use CGI;
9 use Text::Iconv;
10 use Env;
11 use LWP::Simple;
12 use LWP::UserAgent;
13 use HTTP::Request;
14 use HTTP::Response;
15 use HTML::LinkExtor;
16
17 my $url= new CGI;
18 my $filename = $url->param("le_csv");
19 my $upload_filehandle = $url->upload("le_csv");
20 my $hostname=$ENV{SERVER_NAME};
21
22
23 #my $repertoire="/home/articom-web/public_html/";
24
25 print $url->header();
26
27 $hostname =~ s/^www\.//g;
28
29 #IP du SERVEUR
30 my $ip = $ENV{'SERVER_ADDR'};
31 my $host = "ns366702.ovh.net";
32 my $database = "articom";
33 my $user = "articom";
34 my $password = "";
35
36 if($ip eq "192.168.0.7") # adresse IP phenom
37 {
38 $host = "localhost";
39 $database = "articom";
40 $user = "articom";
41 $password = "";
42 }
43 elsif($ip eq "91.121.99.97") # Serveur 1 qui se connecte au serveur
2
44 {
45 $host = "ns366702.ovh.net";
46 $database = "articom";
47 $user = "articom";
48 $password = "";
49 }
50
51
52 elsif($ip eq "94.23.19.58") # Serveur 2 IP 1
53 {
54 $host = "localhost";
55 $database = "articom_un";
56 $user = "articom";
57 $password = "";
58 }
59 elsif($ip eq "91.121.62.106") # Serveur 2 IP 2
60 {
61 $host = "localhost";
62 $database = "articom_deux";
63 $user = "articom";
64 $password = "";
65 }
66 elsif($ip eq "87.98.221.158") # Serveur 2 IP 3
67 {
68 $host = "localhost";
69 $database = "articom_trois";
70 $user = "articom";
71 $password = "";
72 }
73 elsif($ip eq "87.98.131.220") # Serveur 2 IP 4
74 {
75 $host = "localhost";
76 $database = "articom_quatre";
77 $user = "articom";
78 $password = "";
79 }
80 if($filename ne '')
81 {
82 my $table_gratuits = "fiche_champs_gratuits";
83 my $table_payants = "fiche_champs_payants";
84 my $table_portail = "portails";
85
86 my $connect = DBI->connect ("DBI:mysql:$database;$host",$user,
$password);
87 my $changed = Text::CSV->new({sep_char => ';', binary => 1});
88 my $converter = Text::Iconv->new("iso-8859-1", "utf-8");
89
90 my $sql_portail="SELECT id FROM $table_portail WHERE
nom_domaine = '$hostname'";
91 my $query_portail = $connect->prepare($sql_portail);
92 $query_portail->execute;
93 my $portail;
94
95 $query_portail->bind_columns(\$portail);
96 $query_portail->fetch;
97 $query_portail->finish();
98
99
100 ## Supprimer les fiches de ce portail
101 my $sql_delete_fiv_payants="DELETE FROM fiche_champs_payants
102 WHERE fiche_champs_payants.id
103 IN (SELECT `fiche_champs_gratuits`.id FROM
`fiche_champs_gratuits`
104 WHERE `fiche_champs_gratuits`.id_portail =
$portail)";
105 my $delete_fiv_payants = $connect->prepare
($sql_delete_fiv_payants);
106 $delete_fiv_payants->execute;
107 $delete_fiv_payants->finish();
108
109 my $sql_delete_fiv_gratuits="DELETE FROM fiche_champs_gratuits
WHERE id_portail = $portail";
110 my $delete_fiv_gratuits = $connect->prepare
($sql_delete_fiv_gratuits);
111 $delete_fiv_gratuits->execute;
112 $delete_fiv_gratuits->finish();
113
114
115 my $tel_portable='';
116 my $tel_fixe= '';
117
118 my $zero=0;
119
120 #############################################
121 # fonction traitant les caractères spéciaux #
122 #############################################
123 sub treatspecialCaracter($){
124 my $x=shift;
125 $x =~ s/\'/ /g;
126 $x =~ s/\"/ /g;
127 $x =~ s/\',\)/\'\)/g;
128 return $x;
129 }
130 #############################################
131
132 while (<$upload_filehandle>)
133 {
134 if ($zero==0)
135 {$zero ++;}
136 else
137 {
138 my $converted=$converter->convert($_);
139 if ($changed->parse($converted))
140 {
141 my @columns = $changed->fields();
142
143 # Dans la colonne Telephone
144 # je remplace les espaces et les points
145 $columns[5]=~ s/ //g;
146 $columns[5]=~ s/\.//g; # le point doit etre echape
147
148 if($columns[5] =~ /^06.+/)
149 {
150 $tel_portable = $columns[5];
151 $tel_fixe = '';
152 }
153 else
154 {
155 $tel_fixe = $columns[5];
156 $tel_portable='';
157 }
158
159 ###############################################
160 # Insertion dans la table fiche champ gratuit #
161 ###############################################
162 my $tmp = "INSERT INTO $table_gratuits VALUES
(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
163 my $query = $connect->prepare($tmp);
164 my $res = $query->execute('',
165 &treatspecialCaracter
( $columns[0]),
166 &treatspecialCaracter
( $portail),
167 &treatspecialCaracter
( $tel_fixe),
168 '',
169 &treatspecialCaracter
( $tel_portable),
170 &treatspecialCaracter
( $columns[7]),
171 &treatspecialCaracter
( $columns[2]),
172 &treatspecialCaracter
( $columns[3]),
173 &treatspecialCaracter
( $columns[4]),
174 '',
175 '',
176 '',
177 '',
178 '',
179 ''
180 );
181 $query->finish();
182
183 ##############################################
184 # Insertion dans la table fiche champ payant #
185 ##############################################
186 if ($res==1) # si l'insertion dans table fiche
champ gratuit a été réalisé avec succés #
187 {
188 my $query_2 = $connect->prepare("INSERT INTO
$table_payants VALUES('', '', '', '', '')");
189 $query_2->execute;
190 $query_2->finish();
191 }
192 else # echec de l'insertion dasn table fiche champ
gratuit #
193 {
194 print "Erreur lors du charegement du fichier
CSV";
195 }
196 ##############################################
197
198 }
199 }
200 }
201 $connect->disconnect();
202 }
203
204 my $repertoire="../";
205 my $destination_table = "towns";
206 my %hash_town = ();
207 my $destination_connect = DBI->connect ("DBI:mysql:$database;
$host","articom","")
208 || die "Could not connect to database: "
209 . DBI-> errstr;
210
211 my $town_query = $destination_connect->prepare(" SELECT
code_postal, departement.nom_departement as nom_departement
212 FROM
fiche_champs_gratuits
213 JOIN departement
214 ON
departement.num_departement = ROUND((code_postal - (code_postal %
1000)) /1000)
215 ");
216
217
218 my($code_postal_0,$nom_departement);
219 $town_query->execute();
220 $town_query->bind_columns(undef, \$code_postal_0,\
$nom_departement);
221
222
223 while($town_query->fetch())
224 {
225 $hash_town{$code_postal_0}=$nom_departement;
226 }
227 $town_query->finish();
228
229 # purge les nom de fichiers FIV
230 my $purge_query = $destination_connect->prepare("UPDATE
fiche_champs_gratuits SET fichier = NULL");
231 $purge_query->execute;
232 $purge_query->finish();
233
234 my $destination_query = $destination_connect->prepare("SELECT
id,nom,ville,code_postal
235 FROM
fiche_champs_gratuits
236 ");
237 my($id,$nom, $ville,$code_postal);
238 $destination_query->execute();
239 $destination_query->bind_columns(undef, \$id,\$nom,\$ville,\
$code_postal);
240
241 my $update_query = $destination_connect->prepare("UPDATE
fiche_champs_gratuits SET fichier = ? WHERE id = ?");
242
243 my $contenu = "";
244
245 open(F, ">", "../map.txt");
246
247 while($destination_query->fetch())
248 {
249 my $nom_fichier = $hash_town{$code_postal}." ".$ville." ".
$nom;
250 $nom_fichier =~ s/[^\w]/-/g;
251 $nom_fichier =~ s/(-)+$//g;
252 $nom_fichier =~ s/(-)+/-/g;
253 $nom_fichier =~ tr/A-Z/a-z/;
254 print F $nom_fichier.".php php/fiches-
virtuelles.php?id=$id "."";
255 $update_query->execute($nom_fichier,$id);
256 $update_query->finish();
257 }
258 $destination_query->finish();
259
260 # purge noms fichiers ARTICLES
261 my $purge_articles=$destination_connect->prepare("UPDATE
article_libre SET fichier= NULL");
262 $purge_articles->execute;
263 $purge_articles->finish();
264
265 my $update_article=$destination_connect->prepare("UPDATE
article_libre SET fichier = ? WHERE id_article = ?");
266
267 my $articles_select=$destination_connect->prepare("SELECT
id_article, titre_gauche FROM article_libre");
268 my ($id_article, $titre_gauche);
269 $articles_select->execute();
270 $articles_select->bind_columns(undef, \$id_article, \
$titre_gauche);
271 while($articles_select->fetch())
272 {
273 my $nom_article=$titre_gauche;
274 $nom_article=~ s/[^\w]/-/g;
275 $nom_article=~ s/(-)+$//g;
276 $nom_article=~ s/(-)+/-/g;
277 $nom_article=~ tr/A-Z/a-z/;
278 print F $nom_article.".php php/article.php?id=
$id_article "."";
279 $update_article->execute($nom_article,$id_article);
280 $update_article->finish();
281 }
282
283 $articles_select->finish();
284
285 close(F);
286
287 $destination_connect->disconnect();
288
289 if($filename eq '')
290 {
291 print "<html><head></head><body><a href=\"http://$hostname/
\">Revenir</a></body></html>";
292 }
293 else
294 {
295 print "<html><head></head><body><a href=\"http://$hostname/
backoffice/php/ajouter_csv.php\">Revenir</a></body></html>";
296 }
}}

Poser une question


Simplifie ton programme de manière à ce qu'il ne fasse plus que quelques
lignes mais fasse toujours l'erreur.
, a dit...
La variable d'environnement SERVER_ADDR n'est pas toujours définie; ça
dépend du serveur.
Essayez plutôt
my $ip = inet_ntoa scalar gethostbyname($ENV{SERVER_NAME});
en rajoutant un
use Socket;
en haut du script.
HTH
--
J-L.M.
http://www.bribes.org/perl
gethostbyname est obsolète. Il faut utiliser getaddrinfo dans le code
récent. L'API de perl est un peu à la traîne de ce point de vue.
C'est normal, comme le dit perldoc dbi :
If the connect fails (see below), it returns undef and sets both $DBI::err
and $DBI::errstr. (It does not explicitly set $!.) You should generally test
the return status of connect and print $DBI::errstr if it has failed.
Un petit "print $DBI::errstr;" devrait préciser l'erreur
--
Denis Dordoigne
Membre de l'April - promouvoir et défendre le logiciel libre - april.org
Rejoignez maintenant plus de 5 000 personnes, associations,
entreprises et collectivités qui soutiennent notre action
tu y vas un peu fort. c'est vrai que c'est mieux d'utiliser getaddrinfo,
mais dire que gethostbyname est obsolete, ca fait zelote Ipv6...