Bài tập làm quen với hệ quản trị cơ sở dữ liệu Informix chi tiết minh họa từng dòng lệnh trên ubuntu
DAY The DB-Access Utiltiy Creating the demonstration database Putting the DB-Access utility to work 2.1 Invoke DB-Access and connect to the stores database 2.2 To look at the list of tables in the database To look at the columns in the orders table: 2.3 Running a simple query against the table to view data 2.4 This switches from editing mode to interactive mode Creating Simple Test Data Start the dbaccess utility, and select the stores_demo database Create a table called big Insert one row of data into the table: Select the data from the table Which values are assigned to the columns? Change the insert statement by choosing the Use-Editor option from the menu Change the editor from vi to gedit Insert more data in to the table big by executing the following statement Select the data from the table Which values are assigned to the columns? What is the meaning of the data type serial(1) ? - Ý nghĩa kiểu liệu serial(1): lần insert trường id tăng thêm đơn vị Execute the insert statement from exercise again Count the number of rows in the table Execute the insert statement until you have a table with about million rows Leave dbaccess and create a file called sumbig.sql which contains the following statement 10 Execute the SQL script from exercise in batch mode using the command 11 Using the Unix tool time you can also measure the execution time of the select statement: DAY Module ‘Memory in an DBMS’ Exercises Open a terminal window Restart the DBMS using onmode and oninit to empty thebuffer pool Use the onstat-utility to verify that your DBMS is up and running What is the page size of your buffer pool pages? How many pages can be cached in the buffer pool of your DBMS? Use onstat –z to reset the values that are shown to you by the onstat-utility The values reported by onstat –g buf should now be Open a second terminal window Start dbaccess Execute the following SQL statement: select sum(id) from big Monitor the number of pages read from disk by the select-statement in the other terminal window Use onstat –z again to reset the values that are shown to you by the onstat-utility The values shown by onstat –g buf should be again Re-run the select-statement from exercise Monitor the number of pages read from disk and the cache-hit rate in the other terminal There should by a significant change Explain the reasons for this change Module ‘Disks in a DBMS’ Exercises Open a terminal window Which dbspaces exist in your DBMS? Open a second terminal window and start dbaccess Connect to the demo database and create a table friends Insert some rows of data so that both dbspaces are used Go to the other terminal window and clear the reported numbers using onstat –z Using onstat –D we can monitor how many pages are read from which dbspace Execute the command and convince yourself Since we cleared statistics, everything should by zero 14 We saw that locking may have a serious negative impact on the user experience of your DBMS Discuss strategies to minimize the negative impact DAY Module ‚Monitoring Dynamic Server’ Exercises In this exercise, you will use the SMI, the onstat and oncheck commands to answer a series of questions 1.2 Querying the sysmaster DatabaseCreate your Informix environment of the server instance you created in the previous exercise.Create the stores_demo database and connect to the stores_demo databases.Open a second window Do not close the connection in the first window Using sysmastertables and views, execute SQL commands to obtain the following information: 1.2 The databases in the current system The thread are currently running: Poll threads started: The poll threads are running: soctcppoll The thread are currently running: Vgu 1.3 A list of all chunks that are online VPS are running: 13 VP class are running: cpu, aio, lio, pio, adm, soc, msc, fifo CPU VP Class are running: 1.4 The table names in the demo database VP CPU Class: 21 Sqlexec thread: 1.5 The number of extents allocated in each of the tables in the demo database Poll thread: SOC poll: SOC poll is running 1.9 The IBM Informix session id and the UNIX process id (PID) of your dbaccess session Ready: Waiting: 32 Sleep: 14 1.10 The status of your dbspaces and chunks The thread area running on new CPU: 33 1.11 The status of your logical logs Using o n c h e c k Use oncheck to the following: 1.12 Display a disk utilization report of the items table in your database 1.13 Check and correct any indexes and rowids in your database that are inconsistent Module 7: Virtual Processors and Threads Exercises In this exercise you will use the onstat utility to monitor virtual processor and thread activity on your server This utility is discussed in detail in the next module Follow the instructions below and answer the questions provided You will need the stores database that you created in a previous module for this exercise If this database does not exist, create it with the dbaccessdemo command 1.1 Open a terminal and connect to the stores demo database by executing the command dbaccess stores_demo at the command line 1.2 Open another terminal and run onstat -g ath at the command line This onstat option lists all threads What threads are currently running? How many poll threads are started? On what VPs are the poll threads running? Can you identify your session thread? What is the status of this thread? 1.3 Run onstat -g glo at the command line This onstat option shows the VPs that have been started for this server How many VPs are running? What are the VP classes that are running? How many CPU VPs are running? Is the dbspace that holds the logical and physical logs mirrored? How can the onstat –g glo report help you answering this question? 1.4 An sqlexec thread is a session thread that runs on a CPU VP From the output of onstat -g ath above, count the number of sqlexec threads and the number of CPU VPs In a separate (third) terminal, run dbaccess and connect to a database in the server Run onstat -g ath again and count the number of sqlexec threads and the number of CPU VPs How does the output illustrate multithreading? 1.5 Run onstat -g ath Count the number of poll threads How many are associated with shared memory? How many with TLI or SOC? What VPs are they executing on? Count the number of listener threads How many are associated with shared memory? How many with TLI or SOC? On what VPs are they executing? 1.6 The number of threads in the ready, wait, or sleep queue can be monitored using onstat If the ready queue increases over time, it might indicate that additional VPs need to be allocated Run onstat -g rea (if there is no activity on your system, there will be no threads in the ready queue), onstat -g wai, and onstat -g sle 1.7 Allocate an additional CPU VP by executing onmode -p cpu (make sure you run onstat -g glo before the new VP is allocated) Run onstat -g glo again to identify the new CPU VP Are there any threads running on the new CPU VP? Start an additional connection to the demo database Check the onstat -g glo report to see whether this session runs on the new CPU VP Restart your server instance Is the CPU VP that we just added still running? DAY Module 8: Disk Architecture Exercises Use dbaccess and the command-line utility oncheck to complete the following exercises 1.1 We need a fresh server instance Use the server you recreated in a former lab exercise Do not forget to setup the right environment! 1.2 Execute oncheck -pe | more This is called the extent report Reserved pages: 12 Chunk free-list page: tblspace tblspace: physical log: 25000 logical log database tblspace sysmaster database sysutils database 1.3 Create a new database using the Query language option of dbaccess: CREATE DATABASE new_db; 1.4 Execute oncheck -pe | more What structures did you create during step #3, and how much space did they take? 1.5 Create a table in your new database using the Query language option of dbaccess: CREATE TABLE new_table(col1 CHAR(800)); 1.6 Execute oncheck -pT new_db:new_table | more This is the tblspace disk utilization report The first extent is pages (8 pages is the default extent size), the row size is 800 bytes, and the unused data bytes per page total is 48 (explained later) Seven pages are unused; page is allocated as a bitmap page The first extents size: The maxium row size: 800 The unused bytes per data page: 412 1.7 Insert a row into your new table using the Query Language option of dbaccess: INSERT INTO new_table VALUES ("1st row"); 1.8 Execute oncheck -pT new_db:new_table | more Notice that the number of pages used is and that there is one row Also notice that the unused data bytes per page still totals 48 What the used pages contain? 1.9 Insert more rows into new_table Use the same or different values Execute: oncheck -pT new_db:new_table | more ♣ Why are there used pages? ♣ Why is the total number of unused data bytes 96? 1.10 The following is a calculation of the byte usage per data page: Utilized by Space used (bytes) data = (rows) * 800 (bytes) 4000 header 24 time stamp slot table = 5(rows) * 4(bytes) 20 total 4048 unused bytes = 4096 - 4048 48 1.11 Alter the table and add an additional column using the Query language option of dbaccess: ALTER TABLE new_table ADD (col2 CHAR( 3980)); UPDATE new_table SET col2 = "Hello"; 1.12 Execute oncheck -pT new_db:new_table | more What is the new type of page that is allocated and why? 1.13 Create a new table using the Query language option of dbaccess: CREATE TABLE tab2 (col1 CHAR( 512)); 1.14 Create an index on tab2 using the Query language option of dbaccess: CREATE INDEX idx_tab2 ON tab2(col1); 1.15 Insert a row into tab2 using the Query language option of dbaccess: INSERT INTO tab2 values ("1"); 1.16 Execute oncheck -pe How has the distribution of extents changed? 1.17 Execute oncheck -pT new_db:tab2 | more Pages have been used for the table: Pages have been used for the index: Can not create index, so that no used Unused data byte are there the index: 1.18 The following is a calculation of the byte usage per index page: Utilized by Space used (bytes) key 512 header 24 timestamp slot table rowid delete flag total 549 unused bytes = 4096 - 292 3547 1.19 Insert more rows into tab2 using different values for each row (for example, 2, 3, 4, 5, 6, 7, 8) 1.20 Execute oncheck -pT new_db:tab2 | more What is different about the space that has been allocated to the index? Module 9: Managing and Monitoring Disk Space Exercises In this exercise, you will create various spaces and add chunks to these spaces using the onspaces command Use cooked files with names like chunk1, chunk2, etc to perform the tasks that follow 1.1 Edit your configuration file to enable mirroring on your server by setting MIRROR to a value of Since we not want to mirror the root dbspace you have to remove or comment the parameters MIRRORPATH and MIRROROFFSET from your configuration file Set the value of DBSPACETEMP to tempdbs Bring your server offline and back online 1.2 Add a tempdbs dbspace using one of the new cooked files you created Do not mirror this dbspace Make the initial chunk 5000 kilobytes Run onstat –d to verify your changes 1.3 Create a dbspace named logspace Make the initial chunk 200,000 kilobytes (This dbspace will be used in a future lab exercise) Do not mirror the dbspace Run onstat –d to verify your changes 1.4 Create a dbspace named dbspace1 Make the initial chunk 8000 kilobytes Mirror the dbspace Run onstat –d to verify your changes 1.5 Add an 8000-kilobyte chunk to dbspace1 Note that this chunk must be mirrored because the dbspace is mirrored Run onstat –d to verify your changes 1.6 Using onstat -d, determine the number of pages used in dbspace1 1.7 Run oncheck -pe | more and examine the contents of dbspace1 1.8 Create the demonstration database again in your dbspace1 dbspace by entering (verify that your environment is set correctly): dbaccessdemo7 stores_demo -log -dbspace dbspace1 -nots Answer N to abort when you are prompted by the script 1.9 Run oncheck -pe | more again and examine the contents of dbspace1 What changes you see from the extent report you displayed in Step 1.7? 2.1 Create an sbspace named sbspace1 Make the initial chunk 8000 kilobytes Run onstat –d to verify your changes 2.2 Start dbaccess and create the following table: create table blobtes( id int, image blob ); 2.3 The Informix RDBMS contains function that can be used to insert blobs into the table in a comfortable way Execute insert into blobtest values(1, filetoblob('/home/informix/, 'client')); where anyfile is placeholder What happend? 2.4 The RDBMS does not know where to store the blob Open the configuration file of the RDBMS and change the SBSPACENAME parameter to sbspace1 to define athe default SBSpace Restart the server 2.5 Re-execute the insert statement from exercise 2.3 This time an error message should not occurr 2.6 It is also possible to copy blob data to the file system Execute the following statement in dbaccess select lotofile(image, '/home/informix/tmp', 'client') from blobtest ; 2.7 Leave dbaccess and inspect the files in your home directory A new file should exist 3.1 From time to time you will need tables for intemediate results Temp tables are a good choice Execute create temp table temptest( id int ) and insert one row 3.2 While still in dbaccess open a second terminal an execute oncheck -pe | more and examine the contents of tempdbs 3.3 Leave dbaccess, execute oncheck -pe | more and examine the contents of tempdbs What happened? DAY Module 10: Managing Shared Memory Exercise In this exercise, you will use the onstat, onmode, and oninit commands to view the server status and to change the operating mode of the server Perform the following tasks and answer the questions provided 1.1 Bring your server from Online mode to Quiescent mode 1.2 Run the onstat - command to verify the current operating mode of your server 1.3 Bring your server from Quiescent mode to Offline mode 1.4 Bring your server from Offline mode to Quiescent mode and immediately run onstat - to view the status of the server as it is coming up 1.5 Run dbaccess and try to select a database 1.6 Bring the server to Single-User mode Try starting a dbaccess session Did it work? 1.7 After verifying that the server is in Single-User mode, bring the server directly to Offline mode 1.8 Using one of the above commands, determine the build version and build number for your instance What command did you use? 1.9 Start the Service Control Manager of your Microsoft Windows system Identify the service that correponds to your Informix server Stop the service Use onstat – in a Console window to verify that IDS is offline Right click the service entry, choose the ‚General’ and type ‚j’ in the field ‚Start Parameters’ Start the service Monitor the state of your Informix server in a console window Exercise In this exercise, you will modify the configuration of your server and view the effects using onstat commands 2.1 Run the onstat -g seg command How many segments are allocated in your server for the resident portion of shared memory? the virtual portion? the message portion? 2.2 List the type of shared memory segment (resident, virtual, or message) and the size of each segment listed Type Size 2.3 Choose one configuration parameter that you think is most likely to cause an increase in the size of the resident portion of shared memory Change the parameter, then reinitialize shared memory ♣ What parameter did you change? ♣ How much larger is your resident portion as a result of this change? 2.4 Choose one configuration parameter that you think is most likely to cause an increase in the size of the virtual portion of shared memory Change the parameter, then reinitialize shared memory ♣ What parameter did you change? ♣ How much larger is your virtual portion as a result of this change? 2.5 Modify the size configured for the SHMADD parameter by doubling it Reinitialize shared memory and run onstat -g seg to view the size of the segments ♣ What effect did your change have on the size of the shared-memory segments? ♣ What needs to happen in order to see the effects of the change in SHMADD? 2.6 Without reinitializing your database server, modify your server to force the resident portion to always be in memory ♣ What command did you use? ♣ What command can you use to determine that forced residency is imposed on the resident-portion segments? 2.7 Bring your server to Offline mode and then back up to Online mode ♣ Is forced residency still imposed on your resident-portion segments? ♣ What you need to to force residency of the resident-portion segments each time the server is initialized? Module 11 : The Shared Memory Buffer Pool Exercises In this exercise, you will monitor your server as you perform various tasks related to reading pages into the shared-memory buffer pool, checkpoints, and flushing buffer-pool pages to disk Additional information is also provided for you to read as you perform the required tasks 1.1 Bring your server to Offline mode and back up to Online mode This allows you to start with a clean shared-memory buffer pool 1.2 Run the command onstat -B | more This command lists all of the pages that are currently in the shared-memory buffer pool 1.3 Open a second window, set the correct environment and execute oncheck –pe | more to get the dbspace-report In the first window (which lists the pages in the buffer pool) you find the pagenum column E.g 1:472 means page 472 in the chunk with id Since you find the page location as well in dbspace report, you know which pages are cached Why you suppose these pages were read into shared memory when you started up your server? 1.4 Run the command dbaccessdemo bufftest to create the demo database If necessary, use the -dbspace option to specify a target dbspace for the database Answer N to the prompt to copy example files 1.5 Run the onstat -B | more command again See if you can identify the new pages that are written to the shared memory buffer 1.6 Run the onstat -m command to view the last few lines of your message log When did the last checkpoint occur on your server? The message log provides you with a lot of information about checkpoints In addition to telling you the time each checkpoint occurred, it also shows how long it took to complete the checkpoint In a separate message-log entry, information about where the checkpoint log record is written The loguniq value is the unique ID number of the logical log where the checkpoint record was written and the logpos value is an encoded address indicating the page number and byte offset within that page indicating the exact location of the checkpoint record 1.7 Run the onstat -l command to view the log status report What is the unique ID number of the current logical log? What logical log contains the last checkpoint record? 1.8 Run the onmode -l command to advance to the next logical log Run onstat -l to verify that the current logical log has changed DAY 10 Module 10: Physical and Logical Logging Exercises In this exercise, you will add logical logs to the logspace dbspace that you created in the last module and drop the logical logs located in the rootdbs dbspace This moves the logical log from rootdbs to logspace To this, you will perform an archive using the ontape utility 1.0 Since you have to backup your logical logs you have to a full system backup Go to the configuration file and change the parameters TAPEDEV and LTAPEDEV to /home/informix/backup.000 and /home/informix/backup.001 Create both files and use chmod 660 to give them appropriate access permissions Execute ontape –s and a level backup 1.1 Add four new logical logs to the logspace dbspace 1.2 Look at the status of the logical logs What does the A flag indicate in the new logical logs? 1.3 Drop the logical logs located in the rootdbs dbspace The current log in use in the rootdbs dbspace must be switched to the next logical log located in your logspace dbspace using the onmode command described earlier in this module There must be at least three usable logs in a system at any given time 1.4 Use onstat –l to verify that you moved the logs from rootdbs to logspace Moving the Physical Log Move the physical log to the dbspace logspace Use onparams or modify the configuration file How can you verify that the physical log was moved to the new dbspace? Module ‘Fault Tolerance’ Exercises For this lab exercise you may use any Informix DBMS server that is up and running There are no additional requirements Open dbaccess and connect to your logged database testdb Understand and execute the following statements: drop table simple; create table simple( id int ); begin work; insert into simple values(0); commit work; begin work; insert into simple values(1); Open another terminal and execute the following Unix command to locate all oninit processes: ps aux | grep oninit Locate the smallest process number, say x, in the second column of the list Execute the following Unix command kill -9 x Where x is the process number you located in the previous step This stops your DBMS immediately The DBMS is not shutdown correctly This procedure mimics a server crash To verify that the system crashed look at the last lines of the message log: onstat -m Which line indicates the crash? Restart your DBMS oninit Start dbaccess, connect to testdb and execute select * from simple What is the status of the table simple? Is that what you expected? What you think: Which components of the DBMS had been used to achieve this status? Look at the message log to get some more insight into the recovery process: The first output line of onstat -m tells you the location of the message log Open this file and locate the records that contain the word 'Recovery' Try to understand the recovery process of the DBMS at least a little Module 12: Fault Tolerance Exercises In this exercise, you will start with the stores_demo database in a mirrored dbspace (dbspace1) You will bring one chunk of the mirrored pair offline Because of mirroring, you will still be able to view and insert data! You will bring the chunk back online Recovery will occur automatically The exercise requires that you configured your DBMS correctly Set the environment for the server instance by executing /yourname.sh Start your server and use onstat -d to verify that you have a dbspace called dbspace1, which is mirrored (check the M-flag in the chunk report) Create the demo database in datadbs1: dbaccessdemo7 -dbspace datadbs1 -nots Use dbaccess to verify that you can select data from the customers table in the demo database The chunk report of onstat -d contains the absolute filename of the primary chunk of dbspace1 Delete the file that represents the primary chunk of dbspace1 This procedure mimics a disk crash It is very likely that the DBMS did not detect the crash so far: onstat -d and onstat -m should report no unexpected behavior Use dbaccess to select all customer data Does this still work? Is that what you expected? Create a new table simple and insert data: create table simple(id int); insert into simple values(23); Does this work? Is that what you expected? Execute onstat -d and onstat -m see if the DBMS detected the failure already All data we used so far was kept in the buffer pool Therefore the server did not need to access the primary chunk This is the reason why the DBMS did not detect the disk crash The DBMS is forced to access the primary chunk if we sync the buffer pool with dbspace1: onmode -c (force checkpoint) 10 Check onstat -d and onstat -m again 11 We now bring the primary chunk back again: Recreate the removed chunk with exactly the same file name as in the onstat –d report and and set permissions touch chmod 660 Bring the chunk online and sync it with the mirror chunk onspaces -s dbspace1 -p absolute_path_primary -o 12 Check onstat -d and onstat -m to verify that the primary is no longer marked as down Do an ls –lh * in the directory that contains the chunks It is another proof that recovery was successful if mirror chunks and primary chunks have the size 13 To verify that the data is really copied to the primary chunk, remove the mirror chunk and restart your DBMS Start dbaccess and select data from the table simple that we created while the primary was down [...]... Unix command env - INFORMIXDIR: /opt/IBM /informix - ONCONFIG: onconfig.ol _informix1 170_1 - INFORMIXSQLHOSTS:/opt/IBM /informix/ etc/sqlhosts.ol _informix1 170_1 2 Use Nautilus to locate the file referenced by the environment variable ONCONFIG in the directory INFORMIXDIR/etc and open it in a text editor 3 Locate the parameter DBSERVERALIASES in the configuration file and add the alias shm _informix1 170 to... shutdown your current server instance using onmode –yuk 2 Check your environment and write the values of the variables INFORMIXDIR and ONCONFIG ONFORMIXDIR: /opt/IBM /informix ONCONFIG=onconfig.ol _informix1 170_1 3 Locate the file referenced by the environment variable ONCONFIG in the directory INFORMIXDIR/etc using Nautilus and open it in a text editor This is the configuration file of your current pre-configured... a new file yourname.sh to set environment variables for your new server Use the following settings: export ONCONFIG=onconfig.yourname export INFORMIXSERVER=yourname export INFORMIXSQLHOSTS=/sqlhosts.yourname The remaining variables, like INFORMIXDIR, are inherited 5 Go to your terminal window and execute the script using the command /yourname.sh The changes to your environment... Locate the parameter DBSERVERALIASES in the configuration file and add the alias shm _informix1 170 to this parameter Save your configuration file 4 Locate the file referenced by the environment variable INFORMIXSQLHOSTS Add the appropriate connectivity information in a second line of the file Remember: The syntax of the sqlhosts file requires an entry in the last column of a rows, although it is not needed... the shared memory configuration 5 To make your new server alias become effective shutdown and restart your database server 6 Start dbaccess and choose the menu item Connection Choose your new alias shm _informix1 170 and press the return key, when asked for the user name The connection should be established DAY 5 Module ‘Configuring a Database Server’ Exercises In this exercise, you will configure and... column number: create index idx1 on big(number) Does it make sense if the DBMS uses this index in the following query? select count(id) from big where number=0 Explain! DAY 4 Module 1: Overview of IBM Informix Dynamic Server Exercise Match the term on the left with the correct definition on the right _D_ chunk A Physical contiguous pages on a disk that are assigned to a specific tblspace _H_ virtual... shuts the DBMS down oninit restarts it 8 Reset the onstat-results again by using onstat –z 9 Try to execute the following SQL statement: 10 Try to use the internet to resolve this problem DAY 3 Module ‘Sequential Reads’ Exercises 1 Create a table of significant size, as you did in the lab ‘Creating Simple Test Data’ 2 Monitor the current status of the allocated extents using oncheck –pt like we did in... file and use new unused port 8 In your home directory, create the file sqlhosts.yourname Add a line to the file that configures network connectivity for your new server 9 Use Nautilus to locate the $INFORMIXDIR/etc directory Copy the file onconfig.std to your configuration file onconfig.yourname Open this file using a text editor We have to change some configuration parameters: ♣ Set ROOTPATH to the... to a number that is different from the number you found in 3 Remember: The SERVERNUM is the key to distinguish the old and to new server instance from each other ♣ Set DBSERVERNAME to the same value as INFORMIXSERVER (which should be yourname) ♣ Set the TAPEDEV and LTAPEDEV parameters to /dev/null These parameters specify the name of the devices to use for backing up the database server and logical logs,... terminal, run dbaccess, and execute select * from simple At first glance the result might appear bizarre Try to find reason for the result Module ‘Locking’ Exercises For this lab exercise you may use any Informix DBMS server that is up and running There are no specific requirements 1 Start dbaccess and create a database with logging: create database testdb with log;