DBD::mysql - Problem with dropping a database - mysql

Why doesn't the line "$rc = $dbh->func( 'dropdb', $dbname, 'admin' );" remove the database db_test_2?
#!/usr/bin/perl
use warnings;
use strict;
use 5.010;
use DBI;
my $host = 'localhost';
my $user = 'user';
my $password = 'password';
my( $rc, $dbname, #databases );
my $drh = DBI->install_driver( 'mysql' );
$dbname = 'db_test_1';
# use a driver handle (drh)
$rc = $drh->func( 'createdb', $dbname, $host, $user, $password, 'admin' );
say for DBI->data_sources( $driver, { host => $host, user => $user, password => $password });
# DBI:mysql:information_schema
# DBI:mysql:db_test_1
# DBI:mysql:mysql
$rc = $drh->func( 'dropdb', $dbname, $host, $user, $password, 'admin' );
say for DBI->data_sources( $driver, { host => $host, user => $user, password => $password });
# DBI:mysql:information_schema
# DBI:mysql:mysql
my $dbh = DBI->connect( "DBI:mysql:", $user, $password, { RaiseError=>1, AutoCommit=>1 } );
$dbname = 'db_test_2';
# reuse the existing connection of a database handle (dbh)
$rc = $dbh->func( 'createdb', $dbname, 'admin' );
say for DBI->data_sources( $driver, { host => $host, user => $user, password => $password });
# DBI:mysql:information_schema
# DBI:mysql:db_test_2
# DBI:mysql:mysql
$rc = $dbh->func( 'dropdb', $dbname, 'admin' );
say for DBI->data_sources( $driver, { host => $host, user => $user, password => $password });
# DBI:mysql:information_schema
# DBI:mysql:db_test_2
# DBI:mysql:mysql

It is interesting, in the DBI func() it is defined differently, but DBD::MySQL has a convince method to it for what is really an _admin_internal method, which is defined in Mysql.xs and looks to have quite a few calls to do_error(), which leads me to believe that you should check the errors. Try connecting with RaiseError => 1 and see what it says.
To do this, connect with
$dbh = DBI->connect("DBI:mysql:database=$db;host=$host",
$user, $password, {RaiseError => 1});
then use $dbh->func('dropdb' ...), and see what happens.

Related

SQLSTATE[42000]: Syntax error or access violation: 1044 Access denied for user 'ion'#'localhost' to database 'a901ce392dad4b90b1c6e175b07196a9'

I have created a multi-tenant database and problem is that when I create a new database with tenant I receive the following error:
SQLSTATE[42000]: Syntax error or access violation: 1044 Access denied for user 'ion'#'localhost' to database 'a901ce392dad4b90b1c6e175b07196a9' (SQL: GRANT ALL ONa901ce392dad4b90b1c6e175b07196a9.* TOa901ce392dad4b90b1c6e175b07196a9#'127.0.0.1')
I have the following controller to create a new tenant:
public function store(RegisterAuthRequest $request)
{
$name = $request->name;
$email = $request->email;
$password = $request->password;
if (Tenant::tenantExists($name)) {
return response()->json([
'success' => false,
'message' => "A tenant with name " . $name . " already exists."
]);
}
$tenant = Tenant::registerTenant($name, $email, $password);
$tenancy = app(Environment::class);
$tenancy->hostname(); // resolves $hostname as currently active hostname
$tenancy->website(); // resolves $website
$tenancy->tenant(); // resolves $website
//
$tenancy->identifyHostname(); // resets resolving $hostname by using the Request
// invite admin
$tenant->admin->notify(new TenantCreated($request->email, $request->password));
return response()->json([
'success' => true,
'message' => 'tenant with name ' . $tenant->hostname->fqdn . " created and Admin {$email} can log in using password {$password}"
],200);
}
And here is my Tenant model:
public static function registerTenant($name, $email, $password): Tenant
{
// Convert all to lowercase
$connection = app(Connection::class)->systemName();
$name = strtolower($name);
$email = strtolower($email);
$website = new Website;
if($connection != 'default') $website->managed_by_database_connection = $connection;
app(WebsiteRepository::class)->create($website);
// associate the website with a hostname
$hostname = new Hostname;
$baseUrl = config('tenancy.hostname.default');
$hostname->fqdn = "{$name}.{$baseUrl}";
app(HostnameRepository::class)->attach($hostname, $website);
// make hostname current
app(Environment::class)->tenant($hostname->website);
// Make the registered user the default Admin of the site.
$admin = static::makeAdmin($name, $email, $password, $hostname);
return new Tenant($website, $hostname, $admin);
}
private static function makeAdmin($name, $email, $password, $hostname): User
{
$admin = new User;
$admin->name = $name;
$admin->email = $email;
$admin->password = Hash::make($password);
$admin->hostname()->associate($hostname);
$admin->job_title = 'admin';
$admin->birth_date = '1000-01-01';
$roleAdmin = Role::where('name', 'admin')->firstOrFail();
$admin->assignRole($roleAdmin);
$admin->save();
return $admin;
}
public static function tenantExists($name)
{
$name = $name . '.' . config('tenancy.hostname.default');
return Hostname::where('fqdn', $name)->exists();
}
I have all privileges for ion#localhost in MySQL:
+--------------------------------------------------+
| Grants for ion#localhost |
+--------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'ion'#'localhost' |
and there is the thing, in error, it's said SQL: GRANT ALL ONa901ce392dad4b90b1c6e175b07196a9.* TOa901ce392dad4b90b1c6e175b07196a9#'127.0.0.1' and I don't understand why is giving all Grant to this user?
I tried dd() to find out where the error occurs and here it is:
app(WebsiteRepository::class)->create($website);

Perl Database connect not working in .pm file

In Perl I have connected the database using DBI concept. The database connection and select table query is working fine in .pl file. But I have run the DBI database connection code in .pm file. It's not working.
Please review code.
Sample.pl (It's working fine)
use DBI;
my $driver = "mysql";
my $database = "marketplace_perl";
my $dsn = "DBI:$driver:database=$database";
my $userid = "root";
my $password = "root";
my $dbh = DBI->connect($dsn, $userid, $password );
my $dbh=connect_db();
my $sth = $dbh->prepare("SELECT UserEmail,UserPassword FROM pj_user where UserEmail=? and UserPassword=?");
$sth->execute($UserEmail,$UserPassword);
Marketplace.pm (It's not working)
package Marketplace;
use DBI;
sub connect_db {
my $driver = "mysql";
my $database = "marketplace_perl";
my $dsn = "DBI:$driver:database=$database";
my $userid = "root";
my $password = "root";
my $dbh = DBI->connect($dsn, $userid, $password );
return $dbh;
}
sub login_marketplace {
my $dbh=connect_db();
my $sth = $dbh->prepare("SELECT UserEmail,UserPassword FROM pj_user where UserEmail=? and UserPassword=?");
$sth->execute($UserEmail,$UserPassword);
my $User_count=$sth->rows;
return $User_count
}
It returns the error message of "Failed to access class (Marketplace): Can\'t locate DBI.pm in #INC (you may need to install the DBI module)"
Please let me know how to fix the DB issue.
Try this below code in marketplace.pm file.
use ENV;
my $PERL5LIB= $ENV{'PERL5LIB'};
package Marketplace;
BEGIN {
push(#INC, $PERL5LIB);
};
use DBI;
sub connect_db {
my $driver = "mysql";
my $database = "marketplace_perl";
my $dsn = "DBI:$driver:database=$database";
my $userid = "root";
my $password = "root";
my $dbh = DBI->connect($dsn, $userid, $password );
return $dbh;
}
sub login_marketplace {
my $dbh=connect_db();
my $sth = $dbh->prepare("SELECT UserEmail,UserPassword FROM pj_user where UserEmail=? and UserPassword=?");
$sth->execute($UserEmail,$UserPassword);
my $User_count=$sth->rows;
return $User_count
}

Yii2-user Dektrium - how to hash password

i want to do hash password and check that with database ( password_hash )
How can I do it????
$username = $auth['username'];
my password is
$password = $auth['password'];
i want hash that :
$find = \dektrium\user\models\User::findOne(['username' => $username, 'password_hash' => $password]);
You could generate the $hash using
$hash = Yii::$app->getSecurity()->generatePasswordHash($password);
$find = \dektrium\user\models\User::findOne(['username' => $username,
'password_hash' => $hash]);
Th code belowe is from dektrium/yii2-user/helpers/password.php ( the code for hash function ..of dektrium adn as you see the extensions use the generatePasswordHash and a cost
public static function hash($password)
{
return \Yii::$app->security->generatePasswordHash($password,
\Yii::$app->getModule('user')->cost);
}
default cost = 8
I know quite late to answer this, but for those who are still looking.. I recently encountered this issue and after lots of testing below code worked for me:
$behaviors['authenticator'] = [
'class' => HttpBasicAuth::className(),
'auth' => function ($username, $password) {
$user = \dektrium\user\models\User::findOne(['username' => $username]);
if ($user->validate($password)) {
return $user;
}
return null;
}
];

Yii2 - encrypt password for db connection

How to encrypt the database password ?
return [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=localhost;port=5432;dbname=database',
'username' => 'user',
'password' => 'abcPassword',
'charset' => 'utf8',
];
Yii2 comes with user module in advanced setup. See how it store user passwords in encrypted way.
You can use setPassword() method in User Model to get hashed passwords.
public function setPassword($password)
{
$this->password_hash = Yii::$app->security->generatePasswordHash($password);
}
and call this method before saving model data.
public function signup()
{
if ($this->validate()) {
$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
if ($user->save()) {
return $user;
}
}
return null;
}
Also look at the Yii2 doc for passwords =>http://www.yiiframework.com/doc-2.0/guide-security-passwords.html and
authentication.=> http://www.yiiframework.com/doc-2.0/guide-security-authentication.html
Try Via This way.. I didn't tasted it.. but might be helpful to you.
$database = 'mydb';
$username = 'abc';
$password = '1234';
$config['components'] = [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=192.168.*.*;dbname=' . $database,
'username' => $username,
'password' => md5($password); // Try this
'password' => Yii::$app->security->generatePasswordHash($password); // Or Else try this
'charset' => 'utf8',
]
return config;

MySQL General Error 2031

I keep getting:
Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2031 ' in XXXXX
The code I'm using is:
$CR = $this->db->query('SELECT COUNT(*) FROM entities WHERE (entities.typeId = 2 OR entities.typeId = 1)');
$count = $CR->fetchColumn();
I have tried using prepare instead but I get the same result.
Any idea what's causing this and how I can fix it?
class Database extends PDO {
function __construct($user = 'xxx', $pass = 'xxx', $database = 'abc', $host = 'xxx', $port = 'xxx')
{
parent::__construct('mysql:host=' . $host . ';port=' . $port . ';dbname=' . $database, $user, $pass);
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->setAttribute(PDO::ATTR_PERSISTENT, FALSE);
$this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$this->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
$this->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, TRUE);
$this->exec("SET NAMES utf8");
}
}
$this->db = new Database();
try to remove:
$this->setAttribute(PDO::ATTR_PERSISTENT, FALSE);