How to connect to remote mysql from iphone application? - iphone

i am newbie to iphone programming. I am working on an iphone project.
My question is:
How to connect my iphone application to a remote server mysql database directly?
There are many ways to connect through PHP scrip/ json/xml/ webservices.
But i dont need that. I would like to have direct connetion to mysql of remote server without any intermediate?

Basically you would need to build a mysql client lib for iOS and then use that from Objective-C. This blog post should get you started. As stated by peko you should really reconsider that you are doing the right thing. The mysql network protocol isn't exactly made for mobile clients where connections can drop, be unavailable, etc.

You shouldn't do this because someone could sniff the traffic and get the username and password for your database, or inject sql-code.

Here's a MySql objective-C client for iPhone. It was traditionally available only for OSX but they just released it for iOS:
http://www.karlkraft.com/index.php/2010/09/17/mysql-for-iphone-and-osx/
I haven't used it. I would still strongly recommend exposing via services. I would also be interested in why you need to directly connect to the DB and why services isn't appropriate :)

Related

Can create a remote server with MongoDB? How?

My question, to be more clear, it is to create a server with mongodb on a cloud hosting (for example) and access it through another server.
Example:
I have a mobile app.
I hosted my mongoDB a cloud hosting (ubuntu).
I want to connect my app to the db on the server cloud.
Is it possible? How?
I'm joining this learning and my question was exactly MongoDB to create a server in a way that I could access it remotely.
Out of "localhost"? Different from all the tutorials I've seen.
From what you are describing, I think you want to implement a 2-Tier-Architecture. For practically all use cases, don't do it!
It's definitely possible, yes. You can open up the MongoDB port in your firewall. Let's say your computer has a fixed IP or a fixed name like mymongo.example.com. You can then connect to mongodb://mymongo.example.com:27017 (if you use the default port). But beware:
Security You need to make sure that clients can only perform those operations that you want to allow, e.g. using MongoDB integrated authentication, otherwise some random script kiddie will steal you database, delete it, or fill it with random data. Many servers, even if they don't host a well-known service, get attacked thousands of times per day. Also, you probably want to encrypt the connection so people can't spy on the connection. And to make it all worse, you will have to store the database credentials in your client app, which is practically impossible to do in a truly secure way.
Software architecture There is a ton of arguments against this architecture, but 1) alone should be enough. You never want to couple your client to the database, be it because of data migrations, software updates, security considerations, etc.
3-Tier
So what to do instead? Use a 3-Tier-Architecture: Host a server of some kind on mymongo.example.com that then connects to the database. That server could be implemented in nginx/node.js, iis/asp.net, apache/php, or whatever. It could even be a plain old C application (like many game servers).
The mongodb can still reside on yet a different machine, but when you use a server, the database credentials are only known to the server, not to all the clients.
Yes, it is possible. You would connect to MongoDB using the ip address of your host, or preferably using it's fully qualified hostname rather than "localhost". If you do that, you should secure your MongoDB installation otherwise anyone would be able to connect to your MongoDB instance. At an absolute minimum, enable MongoDB authentication. You should read up on MongoDB Security.
For a mobile application, you would probably have some sort of application server in front of MongoDB, e.g. your mobile application would not be connecting to MongoDB directly. In that case only your application server would be connecting to MongoDB, and you would secure MongoDB accordingly.

Running PHP server and MySQL on iOS device

Is there any possibility of running a PHP server (lighttpd, apache, cherokee, etc) and a MySQL server (or sqlite) on a non-jailbroken iOS device? I know about http://www.becomekodiak.com/ and they seem to be running a server (or just an interpreter?), but there is no database connection.
You could use cocoahttpserver, iPhoneHTTPServer3, SimpleWebSocketServer, MultithreadedHTTPServer3 or MongooseDaemon to realize the Server. The content generation is being made by your code and you could use CoreData or any other mobile database solution to save and provide the data.
It's no PHP, but you could implement it in Objective C.

How to connect and read/write file to a local computer on iphone?

i have a problem with local network connection. i'm writing an iphone application and i need to read/write files to a computer. Both devices connected on the same network.
if it's possible, i want to get connected computers ip list, select one of them and read/write files like pdf, doc, txt etc.. if it's not possible to do, i will write the computer ip which i want to connect. There is no problem, both of solution is OK.
But i dont know what do i do after get the computer's ip ?
i found this chat client/server on local, but i got it very complicated.
Anyone have any idea about this ?
You'll need to have a server running on the computer, which can show files and allow for files to be read and created.
Easiest is to run a webdav service on the computer, Apache provides the mod_dav module for this purpose.
The iPhone app then becomes the client. I'd suggest using neon for this purpose. It's a C library that provides listing, reading and writing files on a remote webdav server.
That's how I would do it.
1) Find the network address of the computer you want to connect to. For this you can make use of Bonjour. It's very easy to setup because Bonjour handles the resolving of address for you.
You just have to publish a service (e.g. _myprotocol._tcp) via the ´NSNetService` class which is available on iOS and OS X (Windows too)- in your case you would publish the service on your computer.
Then you search for the service with the NSNetServiceBrowser class.
When you found a service you can then resolve it. This actually gives you the network name of the other device.
2) Connect to the other device via a tcp socket. The CocoaAsyncSocket library is very good at this. This project also includes some examples. One example already provides a bonjour server and client implementation.
i found exactly what i want. The solution is here

How to implement node-postgres on a user's browser?

So I've tested this particular example on my local machine:
http://bjorngylling.com/2011-04-13/postgres-listen-notify-with-node-js.html
It worked! So now when I update a specific table, and am running my node.js file(from the tutorial) -I get an instant notification on my Terminal(mac)!! cool!
But how do I implement this onto a client's browser??
First of all, in the node.js script you'll notice that I have to connect to the database with my username and password:
pgConnectionString = "postgres://username:pswd#localhost/db";
I obviously can't have that floating around in the .js file the user's browser downloaded.
Plus I don't even know what scripts I'd have to include in the <head>. I can't find anything anywhere on how this is used in the real world.... All I see are neat little examples you can use in your command line.
Any advice, or guidance in the right direction would be awesome! Thanks.
You can't.
Node.js runs directly on your server, speaking directly to the native libraries on that machine. I'm not sure exactly what the postgres driver you are using does, but either it speaks to the postgres libraries OR it speaks directly with sockets on the local or a remote database server.
Neither of these methods can be used directly from a browser environment (it can't speak directly to the native libraries and it can't speak "raw" sockets).
What you can do is to have the web client speak to your own server process on a server (running node.js or similar), which would then speak to the database on behalf of the client.
Assuming you also need to database server to be able to initiate notifications to the client, you would need to use a bi-directional communication module like socket.io or similar.
You can do: combine your JS running on node.js which accesses Postgres listening for events with a node.js based WebSocket server, implement PubSub and push out to HTML5 browsers .. WebSocket capable ones.
Another option: use a generic WebSocket to TCP bridge like https://github.com/kanaka/websockify and implement the Postgres client protocol in JS to run in browser. That is doable, but probably not easy / for the faint hearted.

How to Access Database from server through a VPN

I am working on an iphone app in which i want to access database from server through a VPN
Edit as i search. connection To server through is VPN is difficult and i was not found any solid information about this SO now i divide my Question in to two parts and part 1 is important to do as compare to other
Question 1
i have a SQL server DATABASE and a live ip of this server and i want to establish connection and access database through iphone . mean i iphone app store, select data or perform any DDL or DML methods from it self?
e.g
when user givs its info iPhone apps saves it on SQL SERVER DATABASE
Question 2
how can i do this all or which mean of communication is best through VPN how can i do and how can i hit or live IP?
You need a setup with a VPN-Gateway, which your iPhone is connecting to and some infrastructure behind the VPN-Gateway: at minimum the server the database is running on. The VPN connection can be setup in the iPhone Settings (you need the IP of your VPN-Gateway, Account, Password...), this has to be done manually and can't be controlled by the App. Once the VPN is setup and activated you can access the database as you would do it with any other App.
Edit: Question 2: I'm not sure if VPN is the right choice here. Is your server publicly available or is it inside an intranet? If it's publicly available a SSL/TLS connection would be the better choice instead of a VPN.