Connecting to MySQL with Tcl - mysql

I had a question about where a MySql database has to sit for a Tcl application to read it. Currently, I have a Tcl application that reads off of a sqlite database. For the application to find the database, the Sqlite Db must be located in the same folder as the Tcl application. I am planning on upgrading the Sqlite Database to MySql.
Will the MySql database also have to sit in the same folder as the Tcl application? I noticed that the connection string for MySql in Tcl doesn't require a directory path, similar to connecting to Sqlite.
Thank you,
DFM

SQLite databases are actually only a single file (so, your application needs to know where that file is) ; on the other hand, MySQL is a daemon to which your application will access via a network connection (and you're application will need to know the address of the server to which it should connect, plus DB name and login/password).
So, there is no such notion as "in the same directory" for a MySQL DB : you can actually put your MySQL server on a different computer/server, it'll still work the same : you will access it via a network connection.

Related

How to access database while not connected to server?

Relevant info: Using Python3, mysql database and mysql connector package for connecting to the database.
I have a program that uses database to get info that it needs and to write to it. On my machine it works well because I'm connected to the local host but on another machine it won't work. Because program will use real server someday, I want to, if program doesn't have access to the server to use copy of a database that will be stored in his files. I'm new to databases and I can't seem to find any remotely relevant info on this. Help on how can I do this or point me in the right direction where to look, thanks.

Synchronize a file from MySQL database to MS SQL Server

Is there a way to transfer a file from a MySQL database to a MSSQL Server using Nodejs?
Say, I have two applications,
Application A uses MySQL database and nodejs for the backend.
Application B uses MSSQL Server database.
I uploaded an image/document from Application A. I want to transfer/synchronize that image/document to Application B from the source code of Application A.
I've tried using mssql client for Node.js. It works as the rows from MySQL database is transferred to MSSQL Server database.
The problem is when I download the image/document from Application B, the image/document is a plain text with a file size of 0 byte.
Any suggestions or solutions that can help?
Thank you.
Requirements
Before starting the database migration, you will need the following software on your Windows machine :
A running MS SQL Server instance.
A running MySQL Server instance (this depends on your environment, we work with the MySQL server available in Xampp ). The idea is basically to have a MySQL server instance accessible on port 3306.
SQL Server Management Studio installed.
MySQL Workbench . This tool will allow you to migrate the data at the end.
1. Identify the database you are trying to migrate.
As a first step, you must verify that the database you want to migrate is exposed in your SQL Server instance. The easiest way to do this is through the SSMS tool. SQL Server Management Studio (SSMS) is an integrated environment for managing any SQL infrastructure, from SQL Server to Azure SQL Database. SSMS provides tools to configure, monitor and manage SQL Server instances and databases. Use SSMS to deploy, monitor and update the data layer components used by your applications, as well as create queries and scripts.
Open SSMS and access the database engine with the default Windows authentication (or from the connection you want to access) :
Connect to the server and browse the databases in the object browser. In our case, we want to export the my_database database, which, as you can see, is available in the Databases directory :
Now that you know that the database is accessible on the server, we will start with the migration using MySQL Workbench.
2. Start with the migration in MySQL Workbench
MySQL Workbench is a unified visual tool for database architects, developers and administrators. MySQL Workbench provides data modeling, SQL development and comprehensive administration tools for server configuration, user management, backup and much more. MySQL Workbench is available on Windows, Linux and Mac OS X.
Start MySQL Workbench and access the migration wizard from the toolbar (in this tutorial, we use MySQL Workbench 8.0) :
After opening the wizard, click on start migration :
This will open the source selection form. Here you will need to select SQL Server as the source relational database management system (RDBMS). A relational database management system (RDBMS) is a collection of programs and capabilities that allow IT teams and others to create, update, manage and otherwise interact with a relational database, most of which use Structured Query Language (SQL) to access the database. In our case, as mentioned in the article, we will migrate from a Microsoft SQL Server database to a MySQL database, so it will be the source. In our example, we have the MS SQL Server configured on the same computer and it automatically authenticates with Windows Authentication, if your server is hosted remotely, you will need to change the parameters according to your needs. You can test the connection :
And if you are successful, you can continue with the next step of configuring the target RDBMS, which means MySQL. As we mentioned at the beginning of the article, we assume that you already have a MySQL server configured and running in the background, in our case we used Xampp's MySQL server, which allows you to start/stop it with a control panel. The idea is basically to know the credentials to access the running MySQL instance, which in Xampp is accessible with a rootuser and an empty password :
If you are connecting to another MySQL server, you can configure it with SSH keys, etc. Now that both connections are established, you can proceed to the next step which fetches a list of schemas from the source RDBMS. This will create a list that can be toggled with all databases in the SQL Server instance, where you must select the database you want to migrate (as well as the schema name mapping method) :
In our case, we only want to work with the my_database database, so we will extract only that database. Now, if you continue and everything works as expected, you will now see the Source Objects window, which basically allows you to filter which tables you want to migrate or not, normally we would like to migrate them all :
Click next and you will see the migration page, which allows you to pre-check the MySQL script for each table and if there are warnings or errors that you need to correct manually, they will be highlighted in the list. For example, in our case, we have a warning (when importing it will be an error) that specifies a problem with the migration, if we read the code we will see a syntax error of incompatibility with the VISIBLEkeyword. In our example, simply removing that keyword from the lines will allow us to import the scripts without any problem :
After manually correcting the warnings and applying the changes, you can finally export the database structure to a file (.sql) or create the database in the target RDBMS (MySQL Server). In our case, it is easier to import it directly to the server, so we will choose Create schema in target RDBMS (you can export it to a file if you wish) :
Before MySQL workbench starts with the migration, it will check again for errors and warnings; if there are still any, you must manually correct them again :
Click on Recreate object, this will take you to the previous step to build the schematic once again, click next and if everything is successful you will see, everything should be marked as correct :
Finally, all that is left is the data. We already migrated the database structure, so now we need to transfer all the data:
Since we are working with both servers on the same computer, we can make an online copy of the data from the SQL Server database to the MySQL database. Click next and the data migration will start :
Once finished, you will now have the original SQL database in MySQL format on the server. You can now access your MySQL server, where you will find the database available.
Introduction
To integrate any database with nodejs, you need a driver package or you can call it npm module which will provide you with a basic API to connect to the database and perform interactions. The same goes for mssql database, here we will integrate mssql with nodejs and perform some basic queries on SQL tables.
Remarks
We have assumed that we will have a local instance of the mssql database server running on the local machine. You can refer this document to do the same.
Also make sure that the appropriate user created with added privileges as well.
Connecting to SQL via. mssql npm module
We will start by creating a simple node application with a basic structure and then connecting to the local SQL server database and performing some queries on that database.
Step 1: Create a directory / folder with the name of the project you are trying to create. Initialize a node application with the npm init command which will create a package.json in the current directory.
mkdir mySqlApp
//folder created
cd mwSqlApp
//change to newly created directory
npm init
//answer all the question ..
npm install
//This will complete quickly since we have not added any packages to our app.
Step 2: Now we will create an App.js file in this directory and install some packages that we will need to connect to sql db.
sudo gedit App.js
//This will create App.js file , you can use your fav. text editor :)
npm install --save mssql
//This will install the mssql package to you app
Step 3: Now we will add a basic configuration variable to our application that will be used by the mssql module to establish a connection.
console.log("Hello world, This is an app to connect to sql server.");
var config = {
"user": "myusername", //default is sa
"password": "yourStrong(!)Password",
"server": "localhost", // for local machine
"database": "staging", // name of database
"options": {
"encrypt": true
}
}
sql.connect(config, err => {
if(err){
throw err ;
}
console.log("Connection Successful !");
new sql.Request().query('select 1 as number', (err, result) => {
//handle err
console.dir(result)
// This example uses callbacks strategy for getting results.
})
});
sql.on('error', err => {
// ... error handler
console.log("Sql database connection error " ,err);
})
Step 4: This is the easiest step, where we start the application and the application will connect to the SQL server and print some simple results.
node App.js
// Output :
// Hello world, This is an app to connect to sql server.
// Connection Successful !
// 1

Creating custom MySQL servers in VB.NET at runtime

I was wondering if it is possible to create custom MySQL servers in VB.NET while working in visual studio at runtime so that if the server already exists it connects and if it isn't there, the code creates the server. I have searched for this everywhere but couldn't find anything. I would appreciate it a lot if someone guides me to the right path.
You could certainly write some .net code to start a MySQL server on your Windows box when an attempt to connect fails. You simply get a cmd.exe console with administrator privileges and give the command net start mysql.
But MySQL must already be installed on the box for that to work.
You might investigate Sqlite. It provides SQL locally to a .net program, storing your tables in a file called whatever.db. It has very similar .net API access to MySQL's Connector/Net and SQL Server's connector. It's in a NuGet package.
I don't completely understand your "custom MySQL servers" requirement. Sqlite gives you a way to use SQL in your application without connecting to a shared server. That may do what you need.
MySQL does have a CREATE SERVER statement in its SQL dialect. The purpose of this statement is to create a connection to another, remote, MySQL server. With that connection you can use the FEDERATED storage engine to access tables in the remote server. Of course, there is no way to run this CREATE SERVER statement unless your program is already connected to a MySQL server.
With respect, your "task which states to create a server at runtime" doesn't make much sense. Is there more to this requirement? What workflow needs this step? Is it part of the installation of some application software on a new box?

How do I use a mysql db after creation with terminal?

I have created a mysql database and a table on terminal. The data is stored in /usr/local/mysql but my db is a folder and a table has .ibd extension.
How do I get a normal .db extension with a table inside?
The .db file extension is normally used by SQLite, which is a different database product than MySQL.
SQLite is an embedded database. You can write client programs that use the SQLite library to read and write .db files. But it's limited to read and write files that are located on the same computer where your client program runs.
MySQL is a client/server database. You shouldn't read and write the data file directly. The only program that does that is mysqld, the MySQL Server daemon.
You can write applications that use the MySQL client library to open a socket or TCP/IP connection to convey commands to the MySQL Server. This allows the MySQL Server to be on a different computer than where your client program runs. Your client program can request data remotely, over a network. Likewise, other client programs on yet other computers can also make network connections to the MySQL Server's computer. It supports many remote connections at the same time.
You should probably learn more about using MySQL. The manual has a tutorial section.

Gaining access to mysql given physical access to the machine

I own a machine running third party software. I input data into this software and it stores that data into its own mysql database. I'd like query the mysql database directly, but I don't know the credentials that the application is using.
I have read and write access for all files in the machine, including the files in the mysql data directory. Theoretically, I should be able to read the data directly from these files (.ibd and .frm files). But practically, I don't know where to start. I'm thinking that these data files are somewhat readable since encrypting them would destroy their index-ability.
Is this feasible? Or would I have to reverse engineer the data file format in order to read it?
Or even better - is there some config file that I can change which would implicitly trust all local connections similar to postgres?
You could read the mysql files directly, but even if they're now encrypted, the columns names might be weird and you could have to spend some time reading them.
Another point could be looking for config files from that software, that could have the login/password (very very low probability, but who knows?)
And the best would be:
make a backup of the mysql files
in another mysql instalation / computer (to not break your software), follow the reset mysql password guide
Try accessing it via the command line on the local machine:
shell> mysql db_name
(from MySQL documentation)
From here, you can create yourself an account if you need to connect from other client software.
Or have you already tried that?
If you have root access to the machine that MySQL is running on, then you can reset the MySQL root password by following the procedure at: http://www.cyberciti.biz/tips/recover-mysql-root-password.html. Once you've reset the root password, you can then login to MySQL as the root MySQL user, and access any of the databases, and query them. The only caveat to keep in mind is that changing the MySQL root password could potentially prevent your application from accessing the MySQL database, but that would be surprising as the application should be designed to connect to the database using a MySQL user account (with limited privileges) other than the root MySQL user.