Yii2 migration cant create DataBase - yii2

I create a table with migration and all works. But when i try yii migrate i catch error "database doesn exist". When i try to create DB in php admin, and after it use yii migrate, all works, tables was add to my db.
P.S db connection file work.
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=splynx',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
];
I Ask, how migrate can create DB?

If you want to use migrations on a table you HAVE TO create the database before that and then put the database with credentials in your DB file. then you will be able to use migrations.
You cannot migrate on a database which does not actually exist, as the error clearly says.

You can use migration to create rename tables but not create DB. Migration need to configure access to existing DB.

Firstly, create a database in localhost named splynx and then run the migrations

Related

How can I make a separate database in django and not use the default database?

Okay, so I am a complete newbie and this might be a silly question but I couldn't find any answer related to this. I have created a login page using google authentication using django-allauth and it works perfectly fine but the problem it saves the user data directly into default database but I want it to be stored in my own mysql database.
I changed the default database info in the settings.py file but when I open the site to login I get an error "Social query does not exist". Is there any other changes that I need to do other than changing the default database in settings.py?
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysql',
'USER' : '****',
'PASSWORD' : '****',
'HOST' : 'localhost',
'PORT' : 3306
}
}
Maybe you need to run py manage.py migrate for to make the new tables on the data

Laravel 5.7 problems migrating split read-write database

I am experiencing the following scenario:
I want to split my database into separate read and write databases. For that reason i've setup the config/database.php
'mysql' => [
'driver' => 'mysql',
'write' => ['host' => env('DB_HOST_WRITE', 'localhost'),],
'read' => ['host' => env('DB_HOST_READ', 'localhost'),],
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
For DB_HOST_WRITE and DB_HOST_READ I've set the IP addresses for the servers where the databases are running on.
For DB_DATABASE, DB_USERNAME, DB_PASSWORD I've setup the database name and the login credentials.
Both servers are reachable, when I try to access them using mysql console commands like this: mysql -u USERNAME -h IPADDRESS -p
Here is the problem: When I attempt to run my migrations from a third server like this
php artisan migrate
But only the write database gets set up.
Is this the expected behaviour?
Do I have to migrate each database manually?
Is there a flag for the migration command, that allows me to specify the connection i want to migrate on?
I cant find anything about read write database migrations when I google for it.
There was no further database setup done. My information source is the following guide: https://laravel.com/docs/5.7/database#read-and-write-connections
Links to useful guides or solution suggestions are very welcome.
Thanks for your attention.
to #Travis Britz: Thanks for the hint. I was missing the term "MySQL Replication", what is not commented in the laravel documentations. I was mistakenly thinking laravel algorithms are handling the synchronisation on their own, what is in fact not happening.
Synchronization between databaseservers is handled by MySQL internal mechanisms
With these MySQL algorithms I was able to achieve a communication structure like that, what might be a common task in the deployment step of web applications:
I followed this guide by digital ocean to setup a mysql replication manually: https://www.digitalocean.com/community/tutorials/how-to-set-up-master-slave-replication-in-mysql
Digital Ocean is the server provider i'm hosting my applications at.
Images are taken from:
https://www.toptal.com/mysql/mysql-master-slave-replication-tutorial
Best regards.

Cakephp Shell can't connect to mysql

I'm trying to get a crontab working for Cakephp (running on my shared Dreamhost server for now).
I have a shell set up as per the book, which works fine via either a cron run or manually executing (both via a shell file, e.g:
cron.sh
#########
cd /pathToApp/app/Console
/usr/local/bin/php cake.php updater list_reports
where updater is my shell and list_reports a method thereof.
The problem occurs when I try to access the database in the shell ("Using models in your shell", same link).
Error Message:
Error: Database connection "SQLSTATE[HY000] [2002] Can't connect to
local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)"
is missing, or could not be created.
My DB Set-up (site works fine for http, do I need to add unix_socket or something?
array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'mysql.mydomain.net',
'port' => '3306',
'login' => '*******',
'password' => '*******',
'database' => '*******',
'prefix' => '',
);
Thing I've tried:
Creating a symlink to /var/run/mysqld/mysqld.sock - permission denied
Can I even do this with shared hosting?
Tried various changes to the db settings above, no luck.
I note similar problems have been solved by people replacing localhost with 127.0.0.1, but I'm using a shared host, not local.
It's official! I'm a db!
I can confirm this problem was not caused by either Cakephp or Mysql, but rather for something I'd done myself and forgot about....
I had a little switch in database.php to automatically detect whether I was in the dev, test, staging environment etc. I had the default set to local, and since the bash script didn't pick up $_SERVER['SERVER_NAME'] which I was using for the switch, it defaulted to my local db.
Thanks to ifunk for spotting the clue which enabled me to unravel this and get back on track!
:)

Drupal db connectivity

I am setting up Drupal 7.9 on my MAC OS X lion. I have it installed under MAMP/HTDOCS, copied default.settings.php to settings.php, changed the write permission for settings.php. I am trying to setup my database but keep running into the following error:
Failed to connect to your database server. The server reports the following message:
SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'localhost/phpMyAdmin' (61).
I am able to access localhost/phpmyadmin very easily, the username, password and database have been specified. The database has been created at phpMyAdmin as well and the correct name is specified. This is leaving me pretty clueless as to what mistake I am making out here.
I haven't made any changes in the settings.php file, I am not sure what to do.
Any of you have a solution to this or have experienced a similar problem, that would be great!
Thanks much, Happy coding!!!
I think it's because you're setting your database server address as localhost/phpMyAdmin...it should just be localhost
Check you settings.php (located at sites/default). Look at line 180 the database settings should look like
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => 'database_name',
'username' => 'user_name',
'password' => 'password',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
);

How can I copy a mySQL Database in ruby on rails?

We are making a Ruby On Rails webapp where every customer gets their own database.
The database needs to be created after they fill out a form on our website.
We have a template database that has all of the tables and columns that we need to copy. How can I do this in programatically from ruby on rails?
I'm not sure what you mean but you can use ruby's command line functionality to dump the template database, create a new database and re-import it using the mysqldump program:
> mysqldump -uroot -proot templateDB > dump.sql
> mysql -uroot -proot --execute="CREATE DATABASE newDB"
> mysql -uroot -proot newDB < dump.sql
Here is a good description of invoking command line options from Ruby.
From any controller, you can define the following method.
def copy_template_database
template_name = "customerdb1" # Database to copy from
new_name = "temp" #database to create & copy to
#connect to template database to copy. Note that this will override any previous
#connections for all Models that inherit from ActiveRecord::Base
ActiveRecord::Base.establish_connection({:adapter => "mysql", :database => template_name, :host => "olddev",
:username => "root", :password => "password" })
sql_connection = ActiveRecord::Base.connection
sql_connection.execute("CREATE DATABASE #{new_name} CHARACTER SET latin1 COLLATE latin1_general_ci")
tables = sql_connection.select_all("Show Tables")
#the results are an array of hashes, ie:
# [{"table_from_customerdb1" => "customers"},{"table_from_customerdb1" => "employees},...]
table_names = Array.new
tables.each { |hash| hash.each_value { |name| table_names << name }}
table_names.each { |name|
sql_connection.execute("CREATE TABLE #{new_name}.#{name} LIKE #{template_name}.#{name}")
sql_connection.execute("INSERT INTO #{new_name}.#{name} SELECT * FROM #{template_name}.#{name}")
}
#This statement is optional. It connects ActiveRecord to the new database
ActiveRecord::Base.establish_connection({:adapter => "mysql", :database => new_name, :host => "olddev",
:username => "root", :password => "password" })
end
Note that I do not know for sure if this will keep foriegn key integrity. I think it depends a lot on how the template Database is created.
By using yaml_db
You need to install plugin, dump any rails database (including mysql) into data.yml file using rake task, change connection string to point to new database and then finaly load data.yml into any new database (including mysql) using another rake task. Very straightforward.
You could put your template schema creation code into a script which contains all of the required table/index/view/procedure creation statements, call it "template_schema.sql" or whatever and then just run the script on the database of your choice (from Ruby, if that's what you're after) and you're done.
The best approach is probably to have each database object in a separate file under source control (to make it easy to track changes on individual objects) and then have them merged into a single file as part of the deployment.