I am developing a server application, which uses mysql for some data storing. Should I create a connection to the mysql on the server's start and use it for all queries, or create connection on each query? Which is better/faster?
If you ask me, It's better to use only one connection, that way you can use session variables without any issues. Besides, with static variables, it's easy to permanently save the connection somewhere in the script.
Related
When get new data from the database, does this mean a new connection? mysql database
Eg:
SELECT * FROM employees WHERE ID=1
Does this mean a new connection on the database server?
Do I need a connection to perform a query?
Yes, absolutely.
Do I need a fresh connection to get the latest data?
No, you only need to query again.
Does every query need a new connection?
No, you only need one connection, then you can make several queries. Typically, you open the connection once when starting your program, and then do whatever you need; even closing the db is optional, modern languages do that for you when you exit the script, but it's nice to take care of it when you're done.
If you are not so familiar with SQL, this is an SQL Query. It is a kind of specifying the requirements of data which you are requesting from the database. Database Connection is a different thing.
I recommend you should dig in to SQL to understand bit more, it is very easy to learn..
Does this mean a new connection on the database server?
If you are already connected then no
If you are not connected then yes
If your connection hit a timeout on no activity, or similar then yes
You can see your connections (as a normal user) using:
show processlist
https://dev.mysql.com/doc/refman/8.0/en/show-processlist.html
You can see the connection ID and if you are looking to see if your connection is changing then that will help.
Say I have app.js as the main app on the server, and it imports(requires) 4 other scripts that route login, join, write article, delete article requests. I currently have connection instances through
var connection = mysql.createConnection(mysqlConfig) in EACH of the login.js, join.js, write.js, delete.js.
I have a feeling that this is bad practice. What should I do? Should I create a mysqlconnection.js that exports a connection instance, and require that in each of the scripts that require a connection?
Deciding the number of connections depends on your application. If you have a sporadically used application with very few calls to mysql, then a single connection might work. However, looking at your use case, it seems that multiple users will be trying to login, write, etc. In such a case I recommend using Connection Pooling. https://github.com/coopernurse/node-pool
Question 1:
I am using MySQL Connector /J to connect to MySQL. I am creating connection for every request. I need to use connection pool. Whether i need to choose c3p0 or i could use MysqlConnectionPool class provided by the connector library.
Question 2:
I may need to load balace / failover between two MySQL database servers. I could use jdbc:mysql://host,host2/dbname to do the failover automatically. I want to use connection pool and failover in combination. How should i acheive it.
I'd recommend using C3PO or something else. It'll integrate into a Java EE app server better, and it's database agnostic.
Your second question is a good deal more complicated. Load balancing is usually done with an appliance of some kind, like an F5 or ACE, that stands between the client and the load balanced instances. Is that how you're doing it? How do you plan to keep the data in synch if you load balance between the two? If the connections aren't "sticky", you'll expect to find INSERTed data in both instances.
Maybe this reference can help you get started:
http://www.howtoforge.com/loadbalanced_mysql_cluster_debian
I have a local app that uses SQLite. Whenever it has internet access it requests the whole database from the server and recreates the local one from that. Local and Server databases have the same structure, basically the point of the local one is to guarantee function even when no internet is available.
This is a very inefficient way of doing this.
My question is, how to ask for only data that is missing?
Should I send the last ID from each local table and have the server send data from that ID onward?
What happens if an existing ID was modified? This would mean that all data should be checked, but sending the whole database for checking and getting back the modifications or additions also seems stupid.
The configuration is Local SQLite, Server MySQL. I could probably change the server to SQLite if it's recommended.
EDIT:
Multiple clients make requests to the same server MySQL Database, PHP processes the request and replies.
How would you tackle this?
Thank you.
I'd either timestamp the rows in the database and fetch by date, or use rsync (or librsync or similar) to synchronize the database files.
What is the best way to handle mysql database users connection in PHP?
I have a web server running a PHP application on MySQL. I have created a database user for the application: dbuser1 with limited access - only for query, insert and update tables. No alter table.
Now the question is, should i use the same dbuser1 widely in my scripts, so if there are 100 current people using my system and hence 100 scripts running parallel they all connect to the database with the same dbuser1? or should i create a few users and assign each script a different user or load-balance between the dbusers ?
Just use the same user. As long as that user has appropriate access rights (and it sounds like you've got that covered), you'll be ok.
You might want to check that your MySQL installation is configured to allow enough concurrent connections to support your expected usage load, but the defaults should be fine for most sites.