9/07/2554

ติด ตั้ง MySQL on CentOS

ใช้คำสั่ง yum install mysql-server เพื่อติดตั้งโปรแกรม หลังจากติดตั้งเสร็จก็มา

เริ่มใช้งาน MySQL
หลังจากที่ติดตั้งเสร็จแล้ว ใช้คำาสั่งให้ MySQL ทำางานตอนบูท ด้วยคำาสั่ง chkconfig และให้ service
ของ MySQL ทำางานด้วยคำาสั่ง service ชื่อ service ของ MySQL คือ mysqld
[root@server1 ~]# chkconfig mysqld on
[root@server1 ~]# service mysqld start
Initializing MySQL database: [ OK ]
Starting MySQL: [ OK ]
จะเห็นว่าถ้าเราสั่ง ให้ MySQL ทำางานเป็นครั้งแรกก็จะมีการสร้างฐานข้อมูลตั้งต้นของ MySQL โดยดูได้จาก
ข้อความ Initializing MySQL database: จะมีข้อความนี้ครั้งแรกครั้งเดียว และ MySQL เริ่มทำางาน MySQL ทำางาน
แล้วก็สามารถใช้งาน MySQL ได้
ตั้งรหัสผ่านให้กับ user root ที่จะเข้าใช้งานฐานข้อมูล
เพื่อการรักษาความ ปลอดภัยของการเข้าถึงฐานข้อมูล มีความจำาเป็นจะต้องตั้งรหัสผ่านให้กับ user root ซึ่ง
เป็น user ที่จะติดต่อฐานข้อมูล โดยการใช้คำาสั่ง mysqladmin -u root password mypassword โดยที่ mypassword
คือ ผ่าน ต่อไปนี้การติดต่อฐานข้อมูลจะใช้ user root และรหัสผ่าน คือ mypassword
[root@server1 ~]# mysqladmin u
root password mypassword
[root@server1 ~]# mysql u
root p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.0.45 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
ออกจากการใช้งาน MySQL
mysql> \q
Bye
[root@server1 ~]#
หรือ
mysql> exit
Bye
[root@server1 ~]#
การใช้งาน user และ password ของ mysql
แบบแรก ให้ mysql ถามรหัสผ่าน
[root@server1 ~]# mysqladmin u
root password mypassword
[root@server1 ~]# mysql u
root p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.0.45 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
แบบที่ 2 ใส่รหัสผ่านต่อจากออปชัน -p
[root@server1 ~]# mysql u
root pmypassword
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.0.45 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
ถ้าใส่รหัสผ่านผิดก็จะมีข้อความเตือน
[root@server1 ~]# mysql u
root p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using
password: YES)
[root@server1 ~]#
การสร้างฐานข้อมูล MySQL
สร้างฐานข้อมูลใหม่และดูชื่อฐานข้อมูล
[root@server1 ~]# mysqladmin creat newdb u
root p
Enter password:
[root@server1 ~]# mysql u
root p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.0.45 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show databases;
++
| Database |
++
| information_schema |
| mysql |
| newdb |
| test |
++
4 rows in set (0.00 sec)
mysql>
การนำาเข้าฐานข้อมูล และ backup ฐานข้อมูล
นำาเข้าฐานข้อมูลจากไฟล์ด้วย mysql newdb < /path/to/file.sql -u root -p
[root@server1 ~]# mysql newdb < /root/file.sql u
root p
Enter password:
Backup ฐานข้อมูลด้วยคำาสั่ง dump
[root@server1 ~]# mysqldump dbname > /root/file_backup.sql u
root p
Enter password:
การเปลี่ยน password ของ user root ที่ใช้งานฐานข้อมูล MySQL
การเปลี่ยนรหัสผ่าน user root ของ MySQL แบบนี้ ท่านต้องจำารหัสผ่านเดิมได้
โดยการใช้คำาสั่งเหล่า นี้
[root@server1 ~]# mysql u
root p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.0.45 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with A
Database changed
mysql> UPDATE mysql.user SET Password = PASSWORD('newpassword') WHERE
User = 'root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 3 Changed: 0 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
หรือ
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword‘);
โดยที่ newpassword เป็นรหัสผ่านใหม่ที่ต้องการเปลี่ยน ถ้าต้องการเปลี่ยนรหัสผ่านของผู้ใช้งานคนอื่น ก็
ให้ where ที่ user นั้นๆ
การอนุญาตให้ user ติดต่อฐานข้อมูลจากโฮสอื่น
โดยปกติ MySQL จะไม่อนุญาตให้ user root ติดต่อเข้ามาใช้ฐานข้อมูลจากเครื่องอื่น ถ้าจะทำาก็ทำาได้แต่ไม่
ควรทำา ทางที่ดีควรเพิ่ม user ใหม่เข้าไปและอนุญาตให้เฉพาะ user นั้นติดต่อฐานข้อมูลจากโฮสอื่นได้
mysql> grant all privileges on *.* to sothorn@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> update user set Password=password('password') where
User='sothorn';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
จากคำาสั่งสามารถ อธิบายได้ว่า อนุญาตให้ user sothorn สามารถติดต่อฐานข้อมูลที่อยู่บนเครื่องนี้ ได้จากทุก
เครื่อง (%) โดยสามารถใช้งาน ได้ทุกฐานข้อมูลทุกตาราง (*.*) ถ้าอนุญาตบางฐานข้อมูลก็ใช้ ชื่อ db.* เช่น mydb.*
ทดสอบติดต่อฐานข้อมูล จากเครื่องอื่น
root@amdx2:~# mysql h
192.168.2.111 u
sothorn p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.0.45 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
วิธีแก้ปัญหาเมื่อลืม รหัสผ่านของ root ใน MySQL
มีความเป็นไปได้มากกับ การลืมรหัสผ่านของ user root ของ MySQL ไม่ต้องตกใจครับมีวิธีการแก้ปัญหา
ดังคำาสั่งดังต่อไปนี้
[root@server1 ~]# /etc/init.d/mysqld stop
[root@server1 ~]# /usr/bin/mysqld_safe user=
root skipgranttables
&
[root@server1 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.45 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> UPDATE mysql.user SET Password = PASSWORD('newpassword‘) WHERE
User = 'root';
mysql> FLUSH PRIVILEGES;
mysq> \q
[root@server1 ~]# mysqladmin shutdown
[root@server1 ~]# /etc/init.d/mysqld start
จะเห็นว่าเราต้องหยุด การทำางานของ MySQL ก่อน แล้วจึงสั่งให้ MySQL ทำางานอีกครั้งหนึ่งโดยใช้
ออปชัน skipgranttables
หลังจากนั้นก็สามารถ เข้าใช้งาน MySQL ได้โดยไม่ต้องใส่รหัสผ่าน เมื่อเข้า
มาได้แล้วก็ทำาการ เปลี่ยนรหัสผ่านโดยใช้คำาสั่ง update
การอัพเกรด MySQL เป็นเวอร์ชันล่าสุด
CentOS 5.2 มาพร้อมกับ MySQL เวอร์ชัน 5.0.45 ถ้าต้องการเวอร์ชันที่ใหม่กว่านี้สามารถดาวน์โหลดได้ที่
http://dev.mysql.com/downloads/mysql/5.1.html หัวข้อ
Linux x86 generic RPM (dynamically linked) downloads หรือ
Linux AMD64 / Intel EM64T generic RPM downloads
ขึ้นอยู่ CPU ที่ท่านใช้อยู่ โดยดาวน์โหลดทุกไฟล์ในหัวข้อที่ตรงกับ CPU ที่ท่านใช้งานอยู่ ดาวน์โหลดมา
เรียบร้อยแล้วให้ดำาเนินการ ดังนี้
1) ถ้ามีฐานข้อมูลอยู่ให้สำารองข้อมูลด้วยคำาสั่ง mysqldump เพื่อความปลอดภัย
2) หยุดการทำางานของ MySQL ด้วยคำาสั่ง /etc/init.d/mysqld stop
3) ลบ MySQL เวอร์ชันที่ติดมากับ CentOS5.2 โดยใช้คำาสั่ง yum remove mysql-server
4) ไฟล์ที่ดาวน์โหลดมามีดังนี้
[root@server1 mysql_install]# ls
MySQL-client-5.1.32-0.glibc23.i386.rpm MySQL-server-5.1.32-0.glibc23.i386.rpm
MySQL-debuginfo-5.1.32-0.glibc23.i386.rpm MySQL-shared-5.1.32-0.glibc23.i386.rpm
MySQL-devel-5.1.32-0.glibc23.i386.rpm MySQL-shared-compat-5.1.32-0.glibc23.i386.rpm
MySQL-embedded-5.1.32-0.glibc23.i386.rpm MySQL-test-5.1.32-0.glibc23.i386.rpm
5) ติดตั้งโดยใช้คำาสั่ง rpm -Uvh --force MySQL-*.rpm
[root@server1 mysql_install]# rpm Uvh
force
MySQL*.
rpm
Preparing... ########################################### [100%]
1:MySQLsharedcompat
########################################### [ 13%]
2:MySQLdevel
########################################### [ 25%]
3:MySQLclient
########################################### [ 38%]
4:MySQLdebuginfo
########################################### [ 50%]
5:MySQLembedded
########################################### [ 63%]
6:MySQLserver
########################################### [ 75%]
7:MySQLshared
########################################### [ 88%]
8:MySQLtest
########################################### [100%]
ติดตั้งเสร็จระบบจะสตา ร์ท MySQL ให้เองทันที เราก็สามารถเข้าใช้งานฐานข้อมูลได้เลยโดยใช้ ผู้ใช้ และ
รหัสผ่านเดิม
[root@server1 mysql_install]# mysql u
root p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.32 MySQL Community Server (Copyright 20002008
MySQL
AB, 2008 Sun Microsystems, Inc. All rights reserved. Use is subject
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
ข้อควรระวัง
ไฟล์ start script ที่อยู่ใน /etc/init.d/ ใน MySQL เวอร์ชันนี้จะชื่อ mysql ไม่ใช่ mysqld เหมือนเวอร์ชันเก่า
เพราะฉะนั้นเวลาสั่ง สตาร์ท mysql เวอร์ชันนี้ ต้องใช้คำาสั่ง /etc/init.d/mysql start หรือ service mysql start
ถ้าหากไม่คุ้นเคยก็สามารถเปลี่ยนชื่อไฟล์จาก /etc/init.d/mysql เป็น /etc/init.d/mysqld ได้เช่นกัน

0 ความคิดเห็น:

แสดงความคิดเห็น