Archive - ก.ค. 24, 2007

วันที่

การใช้งาน MySQL (2)

ต้องการสร้างฐานข้อมูลใหม่
shell # mysqladmin create newdb -u root -p
Enter password:

นำเข้าฐานข้อมูลจากไฟล์
shell # mysql newdb /path/to/file.sql -u root -p
Enter password:

Backup ฐานข้อมูล
shell # mysqldump dbname > /path/to/file_backup.sql -u root -p
Enter password:

การใช้งาน MySQL (1)

มาลองใช้คำสั่งของ MySQL กันนะครับ

root@sothorn:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9 to server version: 5.0.22-Debian_0ubuntu6.06-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| backupmon |
| mysql |
+--------------------+
3 rows in set (0.00 sec)

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> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| func |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| proc |
| procs_priv |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
17 rows in set (0.00 sec)

mysql>
-----

การเปลี่ยนรหัสผ่าน user root ของ MySQL

เปลี่ยนรหัสผ่านให้ user root โดยการใช้คำสั่งเหล่านี้

shell# mysql -u root -poldpassword
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword');

หรือ

shell# mysql -u root -poldpassword
mysql> UPDATE mysql.user SET Password = PASSWORD('newpassword') WHERE User = 'root';
mysql> FLUSH PRIVILEGES;

-poldpassword คือใช้ option -p แล้วตามด้วยรหัสผ่านเดิม

สิ่งที่ต้องทำอันดับแรก set รหัสผ่านให้กับ root

user ที่ชื่อ root นี้ คนละคนกับ root ของลีนุกซ์นะครับ 
root นี้ใช้ติดต่อฐานข้อมูล ทำไมต้องเปลี่ยนรหัสผ่าน ก็เพราะเขายังไม่ set รหัสผ่านมาให้ ทำให้ไม่ปลอดภัยก็แค่นี้เอง คำสั่งนี้ใช้สำหรับ set password ให้ user root ใช้ครั้งแรกเท่านั้น

root@sothorn:~# mysqladmin -u root password yourpassword

yourpassword คือ รหัสผ่านของท่านนะครับ
อย่าใส่ yourpassword ตามผมนะครับ