Tuesday, February 22, 2011

mysql on linux

1. Use command line client - mysql
Genral syntax
mysql -u DBUSER -h DBSERVERNAME -p

So at a shell prompt you type all one single line to connect to database server install on localhost for root user:
mysql -u root -h localhost -p

Supply the password when prompted for password.

2. Use PHP to connect to MySQL

http://www.cyberciti.biz/faq/how-to-connect-to-my-mysql-database-server-using-command-line-and-php/


$link = mysql_connect("localhost", "USERNAME", "PASSWORD");
mysql_select_db("DATABASE");

$query = "SELECT * FROM TABLE";
$result = mysql_query($query);

while ($line = mysql_fetch_array($result))
{
foreach ($line as $value)
{
print "$value\n";
}
}

mysql_close($link);


3. Mysql list databases
http://www.cyberciti.biz/faq/mysql-command-to-show-list-of-databases-on-server/

mysql> show databases;
output

+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
+--------------------+
2 rows in set (0.00 sec)


Use database
mysql> use test;

List tables
mysql> show tables;

4. Create a mysql database
http://www.cyberciti.biz/faq/howto-linux-unix-creating-database-and-table/
Create database
mysql>CREATE DATABASE books;

Use database
USE books;

Create Table
mysql>CREATE TABLE authors (id INT, name VARCHAR(20), email VARCHAR(20));

Add data
mysql>INSERT INTO authors (id,name,email) VALUES(1,"Vivek","xuz@abc.com");

Display all rows
mysql>SELECT * FROM authors;

5. Drop database / table
Drop database
mysql>DROP DATABASE atomstore;

Thursday, February 17, 2011

PHP Memcache

PHP Memcache Manual
http://php.net/manual/en/book.memcache.php
Memcache module provides handy procedural and object oriented interface to memcached, highly effective caching daemon, which was especially designed to decrease database load in dynamic web applications.

Note: there are two *distinct* memcache PHP implementations
1) pecl-memcache
2) pecl-memcached

Using Memcache vs Memcached with PHP
http://stackoverflow.com/questions/1442411/using-memcache-vs-memcached-with-php

Memcache vs Memcached
http://stackoverflow.com/questions/1825256/memcache-vs-memcached


Comparison
http://code.google.com/p/memcached/wiki/PHPClientComparison

Install on Fedora
yum install php-pecl-memcache
fedora 100% |=========================| 2.1 kB 00:00
http://rpm.livna.org/fedora/7/i386/repodata/repomd.xml: [Errno 12] Timeout:
Trying other mirror.
http://livna.cat.pdx.edu/fedora/7/i386/repodata/repomd.xml: [Errno 14] HTTP Error 404: Not Found
Trying other mirror.


Check
ps -eaf | grep memcache

Check if memcache installed
phpinfo();

Check if memcache is running
telnet localhost 11211

Connection Refused


10 baby step to install Memcached Server and access it with PHP
http://www.webdeveloperjuice.com/2010/01/25/10-baby-steps-to-install-memcached-server-and-access-it-with-php/
Step1 Install libevent ,libmemcached and libmemcached devel (dependency)
yum install libevent
Setting up Install Process
Parsing package install arguments
Package libevent - 1.3b-1.fc7.i386 is already installed.
Nothing to do

yum install libmemcached libmemcached-devel
Setting up Install Process
Parsing package install arguments
No package libmemcached available.
No package libmemcached-devel available.
Nothing to do

Step2 Install Memcached Server
yum install memcached
Setting up Install Process
Parsing package install arguments
Resolving Dependencies
--> Running transaction check
---> Package memcached.i386 0:1.2.3-7.fc7 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================
Package Arch Version Repository Size
=============================================================================
Installing:
memcached i386 1.2.3-7.fc7 updates 50 k

Transaction Summary
=============================================================================
Install 1 Package(s)
Update 0 Package(s)
Remove 0 Package(s)

Total download size: 50 k
Is this ok [y/N]: y
Downloading Packages:
(1/1): memcached-1.2.3-7. 100% |=========================| 50 kB 00:00
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing: memcached ######################### [1/1]

Installed: memcached.i386 0:1.2.3-7.fc7
Complete!

Step3 Start Memcached server
memcached -d -m 512 -l 127.0.0.1 -p 11211 -u nobody
(d = daemon, m = memory, u = user, l = IP to listen to, p = port)

Step4 Check your memcached server is running successfully
ps -eaf | grep memcached
nobody 1888 1 0 07:29 ? 00:00:00 memcached -d -m 512 -l 127.0.0.1 -p 11211 -u nobody
root 1897 1827 0 07:30 pts/1 00:00:00 grep memcached

Step 5: Connect Memcached server via telnet
telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.

Step 6: Check current status of Memcached Server on telnet prompt
stats
STAT pid 1888
STAT uptime 153
STAT time 1297945907
STAT version 1.2.3
STAT pointer_size 32
STAT rusage_user 0.054991
STAT rusage_system 0.027995
STAT curr_items 0
STAT total_items 0
STAT bytes 0
STAT curr_connections 1
STAT total_connections 2
STAT connection_structures 2
STAT cmd_get 0
STAT cmd_set 0
STAT get_hits 0
STAT get_misses 0
STAT evictions 0
STAT bytes_read 7
STAT bytes_written 0
STAT limit_maxbytes 536870912
STAT threads 4
END

Step 7: Exit telnet
quit
Connection closed by foreign host.

Step 8: Install PHP client to access Memcached Server
pecl install memcache
It will make “memcache.so”, you have to just put it on your /etc/php.ini file.

WARNING: channel "pecl.php.net" has updated its protocols, use "channel-update pecl.php.net" to update
Skipping package "pecl/memcache", already installed as version 3.0.2
No valid packages found
install failed

Step 9: Restart your apache server
service httpd restart

Step 10: Open your favorite editor to type below code and execute it, it will cache your data into Memcached server and access it back for you

$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect"); //connect to memcached server
$mydata = "i want to cache this line"; //your cacheble data
$memcache->set('key', $mydata, false, 100); //add it to memcached server
$get_result = $memcache->get('key'); //retrieve your data
var_dump($get_result); //show it

Thursday, February 10, 2011

Symfony Upgrade Issues

1. XXXPeer::doselectrs()
Fatal error: Call to undefined method XXXPeer::doselectrs() in /root/workspace/ProjectName/lib/model/XXXPeer.php on line 37
That method belongs to Propel 1.2, and you are using Propel 1.3, which was introduced into symfony in symfony version 1.2
http://www.propelorm.org/wiki/Documentation/1.3/Upgrading#NewPropelMethodSignatures

New Propel Method Signatures
Another change is related to PDO's lack of a ResultSet correlate. The generated Peer doSelectRS() method has been renamed doSelectStmt(). PDO does not have a ResultSet class, so the doSelectStmt() will return an executed statement.

Solved:
Change doSelectRS() to doSelectStmt()

2. PDOStatement::next()
Fatal error: Call to undefined method PDOStatement::next() /root/workspace/ProjectName/lib/model/XXXPeer.php on line 39
same link as above

Solved:
change

$rs = AuthorPeer::doSelectRS(new Criteria());
while($rs->next()) {
$a = new Author();
$a->hydrate($rs);
}


to

// example of how to manually hydrate objects
$stmt = AuthorPeer::doSelectStmt(new Criteria());
while($row = $stmt->fetch(PDO::FETCH_NUM)) {
$a = new Author();
$a->hydrate($row);
}


3. PDOStatement::getInt()
Fatal error: Call to undefined method PDOStatement::getInt() in /root/workspace/ProjectName/lib/model/XXXPeer.php on line 41

tried:
http://www.symfony-project.org/blog/2008/05/30/how-do-i-use-propel-1-3-in-symfony-1-1

$ cd /path/to/project/root/
$ svn co http://svn.symfony-project.com/plugins/sfPropelPlugin/branches/1.3/ plugins/sfPropelPlugin

no luck

4. stdClass::getName()
Fatal error: Call to undefined method stdClass::getName() in /root/workspace/PHPLIB_ResourceServiceAdapter/ResourceService/service/search/RES_ResourceConditionManager.php on line 54

5. Enable the Compatibility with the Version 1.0
/admerch/apps/cm/config/settings.yml

.settings:
compat_10: true

Wednesday, February 9, 2011

Symfony Upgrade from 1.0 to 1.3

The upgrade from symfony 1.0 projects to 1.2 requires that projects first be upgraded to version 1.1, then 1.2 and finally 1.4. Before beginning the process ensure that the multiple version configuration has been completed. The following instructions will only serve as the base for the upgrade. For full details of the upgrade process and the changes there in visit the following pages:

http://www.symfony-project.org/installation/1_1/upgrade
http://www.symfony-project.org/installation/1_2/upgrade
http://www.symfony-project.org/tutorial/1_4/en/upgrade

1. Change directory into the root of project to upgrade
2. Copy the symfony skeleton file from the 1.1 symfony library
cp /opt/symfony/symfony11/lib/task/generator/skeleton/project/symfony

3. Copy the project skeleton file from the 1.1 symfony library
cp /opt/symfony/symfony11/lib/task/generator/skeleton/project/config/ProjectConfiguration.class.php config/

4. Edit the config/ProjectConfiguration.class.php file and ensure that the require_once points to the valid path and that the configuration and includes from the older config.php are set.

ini_set(‘include_path’, ‘/usr/lib/phplib/libs:’ . ini_get(‘include_path’));
define(‘__DEBUG__’, 0);
define(‘CONFIGPATH’, dirname(__FILE__) . ‘/config.xml’);
define(‘CONFIGTYPE’, ‘XML’);
require_once(‘/opt/symfony/symfony11/lib/autoload/sfCoreAutoload.class.php’);


5. Run the upgrade script
./symfony project:upgrade1.1

6. Once the upgrade script has completed remove any depreciated configuration files (logging, i8n, etc)
read the output, migrate the config file then remove files

7. All of the batch scripts in the project will have to be updated have the following header information in place of the existing.

require_once(dirname(__FILE__).'/../config/ProjectConfiguration.class.php');
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', false);
sfContext::createInstance($configuration);
$databaseManager = new sfDatabaseManager($configuration);
$databaseManager->loadConfiguration();


8. Access the application and ensure that it is in a basic working state (it easier to do this after each upgrade than waiting until the end)
1) Flash attributes
change
$sf_flash->has('good')
to
$sf_user->hasFlash('good')

change
$this->setFlash('error_msg', 'Sorry');
to
$this->getUser()->setFlash('error_msg', 'Sorry');

change
$sf_flash->get('error_msg')
to
$sf_user->getFlash('error_msg');

Problem 1)
[sfException]
Call to undefined method WebController::getContext

Solved: Turn off customized webcontroller  for now
Project/apps/app/config/factories.yml

all:
# controller:
# class: WebController


9. Edit the config/ProjectConfiguration.class.php file updating the symfony include to point to the 1.2 version.

require_once(‘/opt/symfony/symfony12/lib/autoload/sfCoreAutoload.class.php’);


10. Run the symfony 1.2 upgrade
symfony12 project:upgrade1.2

11. Upgrade the databases.yml into the new format
Old Format

all:
propel:
class: sfPropelDatabase
param:
dsn: mysql://username:password@localhost/example

New Format

dev:
propel:
param:
classname: DebugPDO

test:
propel:
param:
classname: DebugPDO

all:
propel:
class: sfPropelDatabase
param:
dsn: mysql:dbname=example;host=localhost
username: username
password: password
encoding: utf8
persistent: true
pooling: true
classname: PropelPDO


12. Rebuild the propel module by running the following

symfony12 propel:build-model


Problem 2)
build-propel.xml:479:1: Table 'xxxxx' does not have a primary key defined. Propel requires all tables to have a primary key.
build-propel.xml:465:22: Execution of the target buildfile failed. Aborting.

Solved: add primaryKey="true"
Project/config/admerch.schema.xml

13. If your application makes use database sessions update your factories.ymlas such
storage:
class: sfPDOSessionStorage
param:
session_name: session
db_table: session
db_id_col: sess_id
db_data_col: sess_data
db_time_col: sess_time
database: example

14. It is strongly recommended to disable any specialized classes, other than the user object, in the factories.yml file.

Problem 3)
500 | Internal Server Error | sfDatabaseException
Database "example" does not exist.
1.at () in SF_SYMFONY_LIB_DIR/database/sfDatabaseManager.class.php line 109 ...
2.at sfDatabaseManager->getDatabase('inpulse_ad')
in SF_ROOT_DIR/cache/cm/dev/config/config_factories.yml.php line 111 ...
3. at require('/root/workspace/admerch/cache/cm/dev/config/config_factories.yml.php')
in SF_SYMFONY_LIB_DIR/util/sfContext.class.php line 149 ...
4. at sfContext->loadFactories()
in SF_SYMFONY_LIB_DIR/util/sfContext.class.php line 76 ...
5.at sfContext->initialize(object('cmConfiguration'))
in SF_SYMFONY_LIB_DIR/util/sfContext.class.php line 59 ...

Solved.
/admerch/apps/cm/config/factories.yml
replace

storage:
class: sfPDOSessionStorage
param:
session_name: session
db_table: session
db_id_col: sess_id
db_data_col: sess_data
db_time_col: sess_time
database: example

use default session

storage:
class: sfSessionStorage
param:
session_name: symfony


15. The underlying propel connection has changed from creole to PDO. Any peer classes that directly hydrate data objects will have to be updated to use the PDO API. Please visit: http://www.symfony-project.org/installation/1_2/upgrade for the complete information.

Locate /admerch/config/propel.ini
change

propel.database = mysql
propel.database.createUrl = mysql://username:password@localhost/
propel.database.url = mysql://username:password@localhost/example

Replace with the following

propel.database = mysql
propel.database.driver = mysql
propel.database.url = mysql:dbname=example;host=localhost
propel.database.user = username
propel.database.password = password
propel.database.encoding = utf8


need to rebuild the object model:

symfony12 propel:build-model


Problem 4)
Error importing addon/propel/builder/SfPeerBuilder.php
Execution of target "om" failed for the following reason: /opt/symfony/symfony12/lib/plugins/sfPropelPlugin/lib/vendor/propel-generator/build-propel.xml:465:22: Execution of the target buildfile failed. Aborting.
[phing] /opt/symfony/symfony12/lib/plugins/sfPropelPlugin/lib/vendor/propel-generator/build-propel.xml:465:22: Execution of the target buildfile failed. Aborting.


Solved
http://blog.vacs.fr/index.php?post/2009/02/08/Symfony-cryptic-error%3A-Error-importing-plugins/sfPropelPlugin/lib/propel/builder/SfPeerBuilder.php

http://oldforum.symfony-project.org/index.php/m/59416/
missing addon/propel/builder/SfPeerBuilder.php file

propel.builder.peer.class = addon.propel.builder.SfPeerBuilder
propel.builder.object.class = addon.propel.builder.SfObjectBuilder



create a new symfony12 project

propel.builder.peer.class = plugins.sfPropelPlugin.lib.builder.SfPeerBuilder
propel.builder.object.class = plugins.sfPropelPlugin.lib.builder.SfObjectBuilder
propel.builder.objectstub.class = plugins.sfPropelPlugin.lib.builder.SfExtensionObjectBuilder
propel.builder.peerstub.class = plugins.sfPropelPlugin.lib.builder.SfExtensionPeerBuilder
propel.builder.objectmultiextend.class = plugins.sfPropelPlugin.lib.builder.SfMultiExtendObjectBuilder
propel.builder.mapbuilder.class = plugins.sfPropelPlugin.lib.builder.SfMapBuilderBuilder


Problem 5)
500 | Internal Server Error | PropelException
Unable to find adapter for datasource [example].

Solved.
ProjectName/config/example.schema.xml

database name="example"

ProjectName/config/database.yml

all:
example:
class: sfPropelDatabase
param:
dsn: mysql:dbname=example;host=hostname
username: username
password: password
encoding: utf8
persistent: true
pooling: true
classname: PropelPDO



16. Edit the config/ProjectConfiguration.class.php file updating the symfony include to point to the 1.3 version.

require_once(‘/opt/symfony/symfony13/lib/autoload/sfCoreAutoload.class.php’);


17. Before switching your project to 1.3 run the following in your project directory
validate project

symfony13 project:validate


http://www.symfony-project.org/tutorial/1_4/en/deprecated
This will identify any areas of your project that need to be modified due to removal of old features. As a note it’s pretty good at finding things but a full test of the application will be needed to validate the upgrade.

18. Upgrading from 1.2 to 1.3 is as simple as executing the following in your project directory

symfony13 project:upgrade1.3
symfony13 propel:build --all-classes


Problem 6)
run symfony13 propel:build --all-classes
Cannot fetch TableMap for undefined table: location

19. Before switching your project to 1.4 run the following in your project directory

symfony14 project:validate

This will identify any areas of your project that need to be modified due to removal of old features. As a note it’s pretty good at finding things but a full test of the application will be needed to validate the upgrade.

20.Once the project successfully validates under 1.4 run the following to complete the upgrade.

symfony14 project:upgrade1.4

Symfony: Import Project

1. Can't find PHPLIB_***
/UX_inPulseAd/config/config.php

Solved:

ini_set('include_path', '/root/workspace/phplib:' . ini_get('include_path'));


2. [sfCacheException]
Unable to read cache file "/root/workspace/ProjectName/cache/appName/dev/config/config_config_handlers.yml.php"

Warning: copy(/root/workspace/UX_inPulseAd/cache/cm/dev/config/config_autoload.yml.php) [function.copy]: failed to open stream: Permission denied in /opt/symfony/symfony10/lib/cache/sfFileCache.class.php on line 553

Warning: chmod() [function.chmod]: No such file or directory in /opt/symfony/symfony10/lib/cache/sfFileCache.class.php on line 559

Fatal error: Class 'sfDatabaseConfigHandler' not found in /root/workspace/UX_inPulseAd/cache/cm/dev/config/config_config_handlers.yml.php on line 25

Solved:
The permissions of the project files and directories can be broken if you use a checkout from an SVN repository. The project:permissions task fixes directory permissions, to change the log/ and cache/ permissions to 0777, for example (these directories need to be writable for the framework to work correctly).

http://www.symfony-project.org/book/1_2/16-Application-Management-Tools


symfony12 project:permissions


doesn't use symfony10 project:permissions
[pakeException]
Task "project:permissions" is not defined.

or use
chmod -R 777 cache/
3. Memcache
Notice: Memcache::connect() [memcache.connect]: Server localhost (tcp 11211, udp 0) failed with: Connection refused (111) in /root/workspace/PHPLIB_CommonAdapter/CommonAdapter/support/COM_MemcacheCache.php on line 30

comments out in config.xml
localhost
11211

4. CVS out of sync
Refresh the directory in eclipse