lost and found ( for me ? )

CentOS6 64bit : install mySQL and access to mySQL with MySQL-python

I’m newbie to Database.
# uname –ri
2.6.32-279.5.2.el6.x86_64 x86_64

# cat /etc/redhat-release
CentOS release 6.3 (Final)

install mySQL with yum groupinstall
# yum groupinstall -y "MySQL Database server"

configuration file ( /etc/my.cnf )
I added the last line. ( character … )
# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
character-set-server = utf8

start mySQL
# /etc/init.d/mysqld start
Initializing MySQL database:  WARNING: The host 'centos6-3.localdomain' could not be looked up with resolveip.
This probably means that your libc libraries are not 100 % compatible
with this binary MySQL version. The MySQL daemon, mysqld, should work
normally with the exception that host name resolving will not work.
This means that you should use IP addresses instead of hostnames
when specifying MySQL privileges !
Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h centos6-3.localdomain password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

[  OK  ]
Starting mysqld:  [  OK  ]


[ set up ]

initialize
# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
     SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] Y
New password: test
Re-enter new password: test
Password updated successfully!
Reloading privilege tables..
... Success!

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n
... skipping.

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!


confirm you can access to mySQL with root user
# mysql -u root -p
Enter password:test
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.1.61 Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye


create a database
# mysql -u root –p


create database named test
mysql> create database test;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)


choose the database named “test”
mysql> use test
Database changed


create table named “test”
mysql> create table test(num int,name varchar(20));
Query OK, 0 rows affected (0.06 sec)


show tables
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test           |
+----------------+
1 row in set (0.00 sec)


register data in test table
mysql> insert into test value(1,'foo');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test value(2,'bar');
Query OK, 1 row affected (0.00 sec)


search
mysql> select * from test;
+------+------+
| num  | name |
+------+------+
|    1 | foo  |
|    2 | bar  |
+------+------+
2 rows in set (0.00 sec)


update contents
mysql> update test  set name='foobar' where num=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0


check
mysql> select * from test;
+------+--------+
| num  | name   |
+------+--------+
|    1 | foobar |
|    2 | bar    |
+------+--------+
2 rows in set (0.00 sec)


create a use who have all privileges to access test DB
mysql> grant all privileges on test.* to test@localhost identified by 'pass';
Query OK, 0 rows affected (0.00 sec)


check whether the user “test” has been created or not
mysql> select user from mysql.user where user='test';
+------+
| user |
+------+
| test |
+------+
1 row in set (0.00 sec)

mysql> quit
Bye


access to mySQL with user “test”
# mysql -u test -p
Enter password:pass

mysql> use test

mysql> select * from test;
+------+--------+
| num  | name   |
+------+--------+
|    1 | foobar |
|    2 | bar    |
+------+--------+
2 rows in set (0.00 sec)

mysql> quit
Bye



[ connect to mySQL with MySQL-python ]
# python --version
Python 2.6.6


install MySQL-python
# yum install -y MySQL-python


just referred to http://zetcode.com/databases/mysqlpythontutorial/
many thx! :D

get MySQL version
# cat get_mySQL_version.py
#!/usr/bin/env python
import MySQLdb
import sys

con = None

try:
       con = MySQLdb.connect('localhost','test','pass','test')
       cur = con.cursor()
       cur.execute("SELECT VERSION()")

       data = cur.fetchone()
       print "Database version : %s " % data


except:
       print "error"
       sys.exit(1)

con.close()

# ./get_mySQL_version.py
Database version : 5.1.61

retrieve data
# cat retrieve_data_mySQL.py
#!/usr/bin/env python
import MySQLdb
import sys

con = None

try:
       con = MySQLdb.connect('localhost','test','pass','test')
       cur = con.cursor()
       cur.execute("SELECT * FROM test")

       numrows = int(cur.rowcount)

       for i in range(numrows):
               row = cur.fetchone()
               print row[0], row[1]

except:
       print "error"
       sys.exit(1)

con.close()

# ./retrieve_data_mySQL.py
1 foobar
2 bar

if you change python script

form
cur.execute("SELECT * FROM test")

to
cur.execute("SELECT * FROM test where num=1")

the result will be:
# ./retrieve_data_mySQL.py
1 foobar

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.