The day-to-day maintenance of the database is an important responsibility of the system administrator. Its content mainly includes the following parts:
First, backup system data
The SYBASE system's backup and recovery mechanism guarantees the possibility of retrieving data when the system fails. SQL Server provides two different types of recovery mechanisms: one is the automatic recovery of the system, which is automatically performed every time the system is started, ensuring that transactions completed before the system is paralyzed are written to the database device, while unfinished transactions are rolled back, and the other is a manually completed recovery, which is done by DUMP and LOAD commands to perform manual backup and recovery work. Therefore, regular backup of the transaction log and database is a very important day-to-day maintenance work. 1. Backup Database
Each database should be unloaded after it is created to provide a mount basis. After this, the periodic table is scheduled to be discharged. such as unloading the database every Friday. It is recommended that you remove database cycles from a general database system once a week.
In addition to unloading the database on a scheduled basis, you will need to unload the database every time you run an operation that does not have a log. For example:
• The DUMP TRAN with No_log (because of the disk overflow of the database) is forced to run every time;
• Allow a select into/bulkcopy to make a quick copy each time you use sp_dboption, or create a permanent table with the SELECT INTO command, or use the WRITETEXT command.
The command to unload the database is:
DUMP DATABASE database_name
To Dump_device
database_name is the name of the database to be unloaded, Dump_device is the name of the device being unloaded. Using system procedure Sp_helpdevice can obtain the information of the equipment.
The following command is used to unload the database my_db:
DUMP DATABASE my_db
To Db_bk_dev
2. Backup transaction log
If the transaction log is on the same device as the database, the transaction log should not be backed up separately from the database. This is the case with the master database and the user database less than 4M. Databases and logs for general database systems are placed on separate devices, so you can use the DUMP TRAN command to back up the logs separately.
The cycle of backing up the transaction log directly affects how much data is recovered, so it is recommended to back up daily.
The command format for backing up the transaction log is:
DUMP TRANsaction database_name
[To Dump_device]
[With truncate_only| With No_log| With No_truncate]
Where database_name is the name of the database to back up the transaction, Dump_device is the backup device name and can be backed up to the device only if the WITH TRUNCATE_ONLY or with NO_LOG clause is included.
Note: If you always use the dump datebase (backing up the database and its logs) without the dump TRAN, the transaction log will not refresh and become very large.
For master and small databases, you should run the dump TRANsaction command to refresh the log after each run of dump datebase.
The following command backs up the transaction log of the database db160 to the backup device:
DUMP TRANsaction db160
To Db_log_bk_dev
With TRUNCATE_ONLY
3. The interaction between the backup database and its log
It is meaningless to unload the transaction log before at least one database is unloaded. The following figure shows the relationship between the backup database and its logs
If a non-hardware failure occurs at Tuesday 5:01, all that is required is to mount tape 5 (see the next section: Data recovery), because tape 5 is just backed up by 5:00, so only the loss of data within one minute between backup and Mount.
But what if it expires at Tuesday 4:49? In this case, you want to mount the tape 1 (unloaded at Friday 5:00). Then, in turn, mount the tape 2,3 and 4. In this way, the system will revert to the state of Tuesday 10:00, most of the work in Tuesday was lost. This example shows the importance of frequently discharging transactions.
Second, if the system fails to restore the database system
If the user database stores a device that is not valid and the database is corrupted or inaccessible, the database can be recovered by loading the most recent database backup and subsequent transaction log backups. Suppose that the current transaction log exists on a device that is not destroyed, and it is unloaded with the DUMP TRANsaction command with the NO_TRUNCATE option.
To recover the database, do the following:
1. If the log exists on a separate device, unload the corrupted or inaccessible user database transaction log with the dump TRANsaction command with the NO_TRUNCATE option.
2, use the following query to check the equipment used to distribute damaged database. The same space block must be assigned for the same purpose.
The following query shows the usage and size of the MYDB device assigned to the database:
SELECT Segmap,size from sysusages
WHERE dbid =
(SELECT dbid from sysdatabases WHERE name = "MyDB")
3, check the output of the query. The ' 3 ' in the Segmap column represents the data allocation, and ' 4 ' represents the log assignment. The size column represents the number of 2K blocks of data. Note the order, use, and size section of this information. For example, the output is:
Segmapsize
--------------------
310240//actual size is: 20M
35120//actual size is: 10M
45120//actual size is: 10M
31024//actual size is: 2M
42048//actual size is: 4M
4, with the drop database command to delete the damaged device databases. If the system complains, use the DROPDB option of the DBCC DBREPAIR command.
5, delete the database, use Sp_dropdevice to delete the damaged equipment.
6. Initialize the new database device with DISK INIT.
7. Reconstruction of the database. Copy all rows from the old sysusages table with the CREATE DATABASE command and include the first logical device.
For the previous example, the command is:
CREATE DATABASE MyDB
On datadev1=20,datadev2=10
LOG on logdev1=10
8. Rebuild the remaining entrances with ALTER DATABASE command. In this example, more space is allocated on the DATADEV1, and the command is:
ALTER DATABASE mydb on datadev1=2
9. Reload the database with load db, then load the log in front of the TRAN.
The LOAD DATABASE command syntax is:
LOAD DATABASE database_name
From Dump_device
The syntax for the LOAD TRANsaction command is:
LOAD TRANsaction database_name from Dump_device
The default permissions for unloading the database and transaction logs are owned by the database owner and can be passed to other users; the permissions to mount the database and transaction are also owned by the database owner, but not delivered.
Second, the production of user information table, and the Information table authorization;
Another daily business for the system maintainer is to create a new information table for the user and authorize it. You are already talking about creating tables and how to authorize them, but write the command syntax here.
• The command to create the table is:
CREATE TABLE table_name
(column_1 datatype [NULL | Not NULL | IDENTITY],
Column_2 ...
)
Go
ALTER TABLE table_name
ADD PRIMARY KEY (column_list)
Go
• The command format for deleting a table is:
DROP TABLE table_name
Go
• The command format that is authorized for the table is:
GRANT {all|permission_list}
On table_name to USER_NAME
Go
• The command format for the recall permission is
REVOKE {all|permission_list}
On table_name from USER_NAME
Go
Third, monitor the operation of the system, timely treatment of system errors;
Another daily job of the system administrator is to monitor the operation of the system. There are several main aspects:
1, monitoring the current user and process information
Using System procedures: sp_who
Description: This command displays all registered users and process information for the current system, as shown in the following table for information about a system.
Spidstatusloginamehostnameblkdbnamecmd
---------------------------------------------------------------
1runningsascosysv0masterselect
2sleepingnull0masternetwork HANDLE
3sleepingnull0masterdeadlock Tune
4sleepingnull0mastermirror HANDLER
5sleepingnull0masterhousekeeper
6sleepingnull0mastercheckpoint Sleep
Display from left to right: process number, current state, registered user name, host name, block count, database name, and current command.
If the total number of processes is found to be close to the maximum number of connections (using System procedures: sp_configure "user conn" view), inactive or unrelated processes should be dropped to ensure that the system is operating normally, and also to monitor illegal users or users who use databases that are not within their own use.
2, monitoring the target occupancy space situation
Using System procedures: sp_spaceused
Description: This procedure displays the number of rows, pages of data, and the space occupied by a target or all targets in the current database. The following table is the information for a database log table:
Namerow_totalreserveddataindex_sizeunused
------------------------------------------------------------
Syslogsnot avail32kb32kb0kbnot Avail
The main objectives of daily monitoring are: User database, database log table (syslogs), and billing raw data tables. If you find that the footprint is too large, you want to dump the log table, you should expand the space or clear the garbage data for other targets.
3. Monitor SQL Server Statistics
Using System procedures: sp_monitor
Description: Sp_monitor Displays the history statistics for SQL Server, and the following table is a statistic for a system:
Last_runcurrent_runseconds
---------------------------------------------------------------
May 1:27pmmay 3:01pm5678
Cpu_busyio_busyidle
---------------------------------------------------------------
16 (6) -0%0 (0) -0%5727 (5672)-99%
Packets_receivedpackets_sentpacket_errors
---------------------------------------------------------------
21 (17) 100 (97) 0 (0)
Total_readtotal_writetotal_errorsconnections
--------------------------------------------------------
785 (366) 311 (113) 0 (0) 3 (2)
The above table gives the information of the last time, the time, the interval seconds, the CPU occupancy, the IO occupancy, the receiving and contracting, the system reading and writing in the system.
To ensure the system data security, the cycle change user password;
In order to ensure the security of the system data, the system administrator must carry out a series of security measures according to the actual situation of the system. Among them, the periodic change user password is the more common and very effective measure.
Changing the user password is accomplished by invoking the system procedure sp_password. The syntax for sp_password is:
Sp_password Caller_password,new_password [, Loginame]
Where Caller_password is the login password (old password), New_password is the new password, loginame is the login name.