How to Connect to MySQL Database

How to connect to MySQL from the command line

See here http://dev.mysql.com/doc/refman/5.0/en/connecting.html

mysql -u USERNAME -pPASSWORD -h HOSTNAMEORIP DATABASENAME 

The options above means:

-u: username
-p: password (**no space between -p and the password text**)
-h: host
last one is name of the database that you wanted to connect.

Look into the link, it's detailed there!


As already mentioned by Rick, you can avoid passing the password as the part of the command by not passing the password like this:

mysql -u USERNAME -h HOSTNAMEORIP DATABASENAME -p

People editing this answer: PLEASE DONOT ADD A SPACE between -p and PASSWORD

How do I connect to mySQL database as a (Windows) client?

You can connect with various tools. I suggest starting with MySQLWorkbench (http://www.mysql.com/products/workbench/).

If you want to connect from code, you need only have the proper library for the language you are coding in. They are called mySQL connectors. You can find them here (https://www.mysql.com/products/connector/). With the connector in your app, you can make api calls to connect to and access the database.

How to connect to MySQL database from a different computer?

Check MYSQL tutorial

  • Get your IP address: first You need to know what the IP address you are connecting from.
  • Granting Access: Granting access to a user from a remote host is fairly simple and can be accomplished from just a few steps. First you will need to login to your MySQL server as the root user.

For Linux (check this site for windows) you can do this by typing the following command:

 # mysql -u root -p
mysql> GRANT ALL ON fooDatabase.* TO fooUser@'1.2.3.4' IDENTIFIED BY 'my_password';

Now you can test your connection remotely. You can access your MySQL server from another server by placing the IP instead of localhost.

Sample Image

How to connect to mySql database on Web through Local Java App

You just need to do one modification in

Connection con=DriverManager.getConnection(  
"jdbc:mysql://localhost:3306/","admin","pass@123");

just repace localhost by your website name

Connection con=DriverManager.getConnection(  
"jdbc:mysql://www.myexample.com:3306/","admin","pass@123");

also go to cpanel of your your website & goto remote access in database sections.

Over there you need to assign your IP address to get access on live hosted database.

If your ISP uses DHCP then you need to make an entry for remote access with %
as with DHCP your IP will get change again and again and each time IP changes you need to make an entry for that in your remote access.

With '%' in remote access, it allows any IP address to use that database.

Connect to MySQL database using python

Please read always the official documentation

Your cooenction stirng has to have this form(if you do it this way=

 mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="testpaaword",
database="testdb"
)

FLASK: How to establish connection with mysql server?

Firstly if you want to do dabatabase operation you can do in celery task, but you dont have to connect db with celery.
You can connect flask with db, and install celery in your project and make db operation in your celery task.

Sample:

app.py

from flask import Flask
from flaskext.mysql import MySQL

app = Flask(__name__)
mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = ''
app.config['MYSQL_DATABASE_PASSWORD'] = ''
app.config['MYSQL_DATABASE_DB'] = ''
app.config['MYSQL_DATABASE_HOST'] = ''
mysql.init_app(app)

tasks.py

@celery.task
def db_connect_things():
conn = mysql.connect()
cursor =conn.cursor()
sql_query = """select from where """
cursor.execute(sql_query)
...

celery_config.py

from celery import Celery
celery = Celery(__name__)
celery = Celery('tasks', broker=) # rabbit,redis, ..
celery.conf.update({'CELERY_ACCEPT_CONTENT': ['pickle', 'json', 'msgpack', 'yaml']})
celery.conf.add_defaults(...)

celery.conf.update(CELERYBEAT_SCHEDULE={
'db_connect_things': {
'task': 'application.lib.tasks.db_connect_things',
'schedule': crontab(minute=0, hour='*/12'),
}})

class ContextTask(celery.Task):
...


Related Topics



Leave a reply



Submit