My sql administration

27
MySQL Administration Mohd Yasin Abd Karim [email protected]

description

mysql administration with command

Transcript of My sql administration

Page 1: My sql administration

MySQLAdministration

Mohd Yasin Abd [email protected]

Page 2: My sql administration

Configuring Monitoring, Starting & Stopping Managing Users and Connection Performing backups Others

Introduction to MySQL Administrator

Page 3: My sql administration

Terminology

Page 4: My sql administration

Mysqladmin or mysql GUI

◦ MySQL Administrator◦ MySQL Workbench◦ phpMyAdmin

Tools

Page 5: My sql administration

OS packages place files in many areas and varies◦ e.g. /usr/lib, /var/lib, /var/log, /etc

Source rpm, yum, .tar.gz, exe Online/repo update

◦ #yum update mysql-* For Ms Windows Environment

Local folder normally Program Files folder

MySQL Installation

C:\>cd \local\mysql\binC:\local\mysql\bin>

Page 6: My sql administration

Start MySQL process before create database To configure MySQL start at boot time

Or using GUI tools such as OS Services Management

Starting MySQL

#chkconfig mysqld on

Page 7: My sql administration

After boot time using the services commands

Remember to restart mysqld process every time after configuration changed

Starting & Stopping the MySQL Server

#service mysqld start#service mysqld stop#Service mysqld restart

Page 8: My sql administration

to know whether your MySQL server is alive

Should get response process ID numbers Or

The "mysqld is alive" message tells you that your MySQL server is running ok. If your MySQL server is not running, you will get a "connect ... failed" message.

Check process

#pgrep mysqld

#mysqladmin –u root –p ping

Page 9: My sql administration

my.cnf◦ Watch out for /etc/my.cnf, /etc/mysql/my.cnf

To get the server listening on all interfaces, use 0.0.0.0 as the bind address. i.e.:--bind-address=0.0.0.0

MySQL Configuration

Page 10: My sql administration

Configured to listen TCP/IP Port (default 3306)

Additional Instances◦ Different Ports◦ Different IP’s using default Port

Local connection using Socket

MySQL Ports & Socket

Page 11: My sql administration

Login to MySQL server

Create a database

List all databases on the MySQL server

MySQL Client

#mysql –h hostname –u root -p

msql> create database [databasename];

msql> show databases;

Page 12: My sql administration

Swicth to a database

To see all the tables in the db

To delete a db

MySQL Client

msql> use [db_name];

msql> show tables;

msql> drop database [databasename];

Page 13: My sql administration

To see database’s field formats

To delete a table

Show all data in a table

MySQL Client

msql> drop table [table name];

msql> SELECT * FROM [table name];

msql> describe [table name];

Page 14: My sql administration

SHOW TABLES; SHOW WARNINGS; SHOW STATUS; FLUSH STATUS; SHOW VARIABLES; SHOW VARIABLES LIKE ‘%size%’; SHOW VARIABLES LIKE ‘sort_buffer_size’; SHOW GLOBAL STATUS;

SHOW Commands

Page 15: My sql administration

to check version number of your MySQL server

MySQL Check Version

C:\local\mysql\bin>mysqladmin -u root version

Page 16: My sql administration

According to the /etc/my.cnf Usually located in subdirectory

/var/lib/mysql/ directory Example : test database

◦ /var/lib/mysql/test

Location of MySQL Databases

Page 17: My sql administration

Root or superuser account is used to create and delete database

New installation MySQL set password

Creating a MySQL ‘root’ Account

#mysqladmin –u root password new-password

Page 18: My sql administration

to know what else you can do with "mysqladmin", you should run the "-?"

Mysqladmin tool

#mysqladmin –?

Page 19: My sql administration

Creating new user

Administering Users

# mysql -u root -pmysql> use mysql;mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'));mysql> flush privileges;

Page 20: My sql administration

Change a user password from unix shell

Change user password from MySQL prompt

# mysqladmin -u username -h hostname.org -p password 'new-password'

# mysql -u root -pmysql> SET PASSWORD FOR 'user'@'hostname' = PASSWORD('passwordhere');mysql> flush privileges;

Page 21: My sql administration

Update root password

Allow the user “bob” to connect to server from localhost using password ‘passwd’

# mysqladmin -u root -p oldpassword newpassword

# mysql -u root -pmysql> use mysql;mysql> grant usage on *.* to bob@localhost identified by 'passwd';mysql> flush privileges;

Page 22: My sql administration

Give user privileges for a db.

# mysql -u root -pmysql> use mysql;mysql> INSERT INTO db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES ('%','databasename','username','Y','Y','Y','Y','Y','N');mysql> flush privileges;

or

mysql> grant all privileges on databasename.* to username@localhost;mysql> flush privileges;

Page 23: My sql administration

To update info already in table

Delete a rows from table

Update database permissions/privileges

mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',Update_priv = 'Y' where [field name] = 'user';

mysql> DELETE from [table name] where [field name] = 'whatever';

mysql> flush privileges;

Page 24: My sql administration

Dump all databases for backup

Dump one database for backup

Dump a table from a database

Backup

# mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql

# mysqldump -u username -ppassword --databases databasename >/tmp/databasename.sql

# mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql

Page 25: My sql administration

Restore database / table from backup

Restore

# mysql -u username -ppassword databasename < /tmp/databasename.sql

Page 26: My sql administration

Example

Create Table

mysql> create table [table name] (personid int(50) not null auto_increment primary key,firstname varchar(35),middlename varchar(50),lastnamevarchar(50) default ‘yasin');

mysql> CREATE TABLE [table name] (firstname VARCHAR(20), middleinitial VARCHAR(3), lastname VARCHAR(35),suffix VARCHAR(3),officeid VARCHAR(10),userid VARCHAR(15),username VARCHAR(8),email VARCHAR(35),phone VARCHAR(25), groups VARCHAR(15),datestamp DATE,timestamp time,pgpemail VARCHAR(255));

Page 27: My sql administration

[email protected]

Thanks