We are using MYSQL in that we have 10 databases as single project.
my problem is to auto-merge 10 database tables into single database using replication.
for example :
MasterDatabases
database1
....table1
....table2
database2
....table21
....table22
database3
....table31
....table33
Replication Database
slavedatabase
....table1
....table2
....table21
....table22
....table31
....table33
You can use --replicate-rewrite-db for that.
Tells the slave to create a replication filter that translates the
default database (that is, the one selected by USE) to to_name if it
was from_name on the master. Only statements involving tables are
affected (not statements such as CREATE DATABASE, DROP DATABASE, and
ALTER DATABASE), and only if from_name is the default database on the
master. To specify multiple rewrites, use this option multiple times.
The server uses the first one with a from_name value that matches. The
database name translation is done before the --replicate-* rules are
tested. You can also create such a filter by issuing a CHANGE
REPLICATION FILTER REPLICATE_REWRITE_DB statement.
Read more about it here.
Related
I have table which has a few data.
name score
1 AAA 100
2 BBB 98
3 CCC 85
Now I want to make the insert sentence such as
insert into pepolescore(name,score) VALUE("CCC",85)
automatically.
Is there any command to do this or any function ? by mysql commandline or phpmyadmin.
MySQL queries can address another schema on the same MySQL Server instance by using qualified table names. See https://dev.mysql.com/doc/refman/8.0/en/identifier-qualifiers.html
But this does not work if the tables are on separate MySQL Servers. A given SQL query can only address schemas on the same server.
Here are a few workarounds:
Use mysqldump to export data from one table and then use mysql to import it to the other table on the other instance. You need to be careful not to let mysqldump output the DROP TABLE command, so read about the options here: https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html
MySQL supports a table engine called FEDERATED, where a table can function as a sort of proxy to a table on another MySQL Server. Then you can use INSERT ... SELECT syntax as if the tables were co-located on the same MySQL Server. The Federated engine has limitations, so read https://dev.mysql.com/doc/refman/8.0/en/federated-storage-engine.html and its subsections to learn more.
Use a community tool such as pt-archiver to copy data from one MySQL instance to the other. Read the manual to learn more: https://docs.percona.com/percona-toolkit/pt-archiver.html
Write your own custom code in a client application. Create two connections, one for each MySQL Server. Fetch query results from the first server, and store the resulting rows in variables in your application. Then use these rows as the tuples to insert using the second connection to the other MySQL Server. This involves writing more code, but you get a lot of flexibility.
Mysql 8.X
Database name: db1.
In this database, I create a view:
CREATE VIEW vw1 AS
SELECT * FROM sometable;
When I check the source code of the view, instead of the code above, I can see:
SELECT * FROM db1.sometable;
I.e. MySQL engine automatically adds a database name prefix to every table I refer to in the view.
Now, I need to rename my database from db1 to db2. There is no built-in database rename functionality in MySQL. I have to take a backup, then drop the original database, then restore the backup under a new name.
Result: the vw1 view in my new db2 database tries to select rows from (now non-existing) db1 database, causing errors.
Now imagine 100s of databases, with 100s or 1000s of tables each. This issue makes things absolutely non-manageable.
Is there any way of stopping MySQL from adding database prefixes in view definitions?
I have two database, each contain different tables
leave.sql and crm.sql
I want to transfer all my leave tables into the crm database
How should I do it?
If the other database has different tables then the leaves database then all you need to do is import the data into the crm database. Finally, make sure your leaves script is connected to the new crm database.
Two other methods:
On the mysql prompt, you can rename your tables - and effectively move them from one database to the other (if both are on the same filesystem):
From http://dev.mysql.com/doc/refman/5.0/en/rename-table.html:
As long as two databases are on the same file system, you can use
RENAME TABLE to move a table from one database to another:
RENAME TABLE current_db.tbl_name TO other_db.tbl_name;
If you are using MyISAM tables, and you have root privileges, you can just do:
stop your mysql server
move your tables from the old database directory to the new one
start your mysql server
All those assuming the tables in your old database (the ones you want to move) have different names than the ones in your new database (the ones you want to keep). If that's not true, you'll also have to change the names.
I've setup mysql replication only for a specific database on the master.
If I connect to the master and don't specify a database (e.g. in the connection string or with the 'use database' command) the statement is not sent to the slave. Is this a bug?? Why does this happen?
Example 1
with no db specified up till now: won't replicate
insert into exampledb.mytable values(1,2,3);
Example 2
replicates
use exampeldb;
insert into mytable values(1,2,3);
Not a bug. This behavior is defined in the MySql docs:
The main reason for this “check just
the default database” behavior is that
it is difficult from the statement
alone to know whether it should be
replicated (for example, if you are
using multiple-table DELETE or
multiple-table UPDATE statements that
go across multiple databases). It is
also faster to check only the default
database rather than all databases if
there is no need.
I got a case where I have several databases running on the same server. There's one database for each client (company1, company2 etc). The structure of each of these databases should be identical with the same tables etc, but the data contained in each db will be different.
What I want to do is keep a master db that will contain no data, but manage the structure of all the other databases, meaning if I add, remove or alter any tables in the master db the changes will also be mirrored out to the other databases.
Example: If a table named Table1 is created in the master DB, the other databases (company1, company2 etc) will also get a table1.
Currently it is done by a script that monitors the database logs for changes made to the master database and running the same queries on each of the other databases. Looked into database replication, but from what I understand this will also bring along the data from the master database, which is not an option in this case.
Can I use some kind of logic against database schemas to do it?
So basicly what I'm asking here is:
How do I make this sync happen in the best possible way? Should I use a script monitoring the logs for changes or some other method?
How do I avoid existing data getting corrupted if a table is altered? (data getting removed if a table is dropped is okay)
Is syncing from a master database considered a good way to do what I wish (having an easy maintainable structure across several datbases)?
How will making updates like this affect the performance of the databases?
Hope my question was clear and that this is not a duplicate of some other thread. If more information and/or a better explantion of my problem is needed, let me know:)
You can get the list of tables for a given schema using:
select TABLE_NAME from information_schema.tables where TABLE_SCHEMA='<master table name>';
Use this list for a script or stored procedure ala:
create database if not exists <name>;
use <name>;
for each ( table_name in list )
create table if not exists <name>.table_name like <master_table>.table_name;
Now that Im thinking about it you might be able to put a trigger on the 'information_schema.tables' db that would call the 'create/maintain' script. Look for inserts and react accordingly.