MySQL: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
(5 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
This is OK though, in that administrative tools are provided to create, backup, and restore a database. | This is OK though, in that administrative tools are provided to create, backup, and restore a database. | ||
For a system user to access a mysql database, | For a system user to access a mysql database, they needs a mysql database user name and password. | ||
From there, | From there, they can use user (non-root) commands to create, backup, restore, and delete a database. | ||
[[File:CPanel Databases.jpg|right|thumb]] | [[File:CPanel Databases.jpg|right|thumb]] | ||
Line 20: | Line 20: | ||
For this exercise, we will use: | For this exercise, we will use: | ||
Database user name: " | Database host: "localhost" | ||
Database user password: " | Database user name: "db_user_name" | ||
Database name: " | Database user password: "db_user_pw" | ||
Database name: "db_name" | |||
'''Create Database User''' | '''Create Database User''' | ||
$ sudo mysql | $ sudo mysql | ||
mysql> CREATE USER ' | mysql> CREATE USER 'db_user_name'@'localhost' IDENTIFIED BY 'db_user_pw'; | ||
''Query OK, 0 rows affected (0.02 sec)'' | ''Query OK, 0 rows affected (0.02 sec)'' | ||
Line 35: | Line 36: | ||
+------------------+ | +------------------+ | ||
| debian-sys-maint | | | debian-sys-maint | | ||
| | | db_user_name | | ||
| mysql.infoschema | | | mysql.infoschema | | ||
| mysql.session | | | mysql.session | | ||
Line 45: | Line 46: | ||
'''Give user 'CREATE' and 'SELECT' permissions''' | '''Give user 'CREATE' and 'SELECT' permissions''' | ||
mysql> GRANT CREATE, SELECT ON * . * TO ' | mysql> GRANT CREATE, SELECT ON * . * TO 'db_user_name'@'localhost'; | ||
Query OK, 0 rows affected (0.01 sec) | Query OK, 0 rows affected (0.01 sec) | ||
Line 51: | Line 52: | ||
''Bye'' | ''Bye'' | ||
== Database User Creates Database== | ==Database User Creates Database== | ||
$ mysql -h localhost -u | $ mysql -h localhost -u db_user_name -p | ||
Enter password: '' | Enter password: ''db_user_pw'' | ||
Welcome to the MySQL monitor. Commands end with ; or \g. | Welcome to the MySQL monitor. Commands end with ; or \g. | ||
Your MySQL connection id is 23 | Your MySQL connection id is 23 | ||
Line 66: | Line 67: | ||
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. | Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. | ||
mysql> CREATE DATABASE | mysql> CREATE DATABASE db_name; | ||
Query OK, 1 row affected (0.00 sec) | Query OK, 1 row affected (0.00 sec) | ||
Line 73: | Line 74: | ||
| Database | | | Database | | ||
+--------------------+ | +--------------------+ | ||
| | | db_name | | ||
| information_schema | | | information_schema | | ||
| mysql | | | mysql | | ||
Line 84: | Line 85: | ||
Bye | Bye | ||
== | ==Backup Database to a file== | ||
The "--no-tablespaces" option is often used for MediaWiki backup/restore | The "--no-tablespaces" option is often used for MediaWiki backup/restore | ||
$ mysqldump --no-tablespaces -h localhost -u | $ mysqldump --no-tablespaces -h localhost -u db_user_name -p db_name | gzip > backup_db_name.sql.gz | ||
==Delete a Database== | |||
Used to test backup and restore scripts... | |||
$ mysqladmin -u db_user_name -p drop db_name | |||
==Create Wiki Database using 'mysqladmin'== | |||
$ mysqladmin -u db_user_name -p create db_name | |||
(required before importing dump) | |||
==Restore Database from file== | |||
$ mysql -u db_user_name -p db_name < backup_db_name.sql | |||
==Bash Script to backup a database using 'mysqldump'== | |||
NOTE: Set the file permissions to 700, so other's don't see password | |||
#!/bin/bash | |||
# backup 'db_name' database to a filename containing date | |||
HOSTNAME=localhost | |||
USERID=db_user_name | |||
USERPW="db_user_pw" | |||
DBNAME=db_name | |||
FILENAME="${DBNAME}_backup_$(date +'%b_%d_%Y')" | |||
mysqldump --no-tablespaces -h $HOSTNAME -u $USERID -p$USERPW $DBNAME | gzip > $FILENAME |
Latest revision as of 10:13, 13 June 2024
As part of a MediaWiki Overhaul, I had the opportunity to learn a bit more about MySQL administration.
When a database is created, a directory to manage the database is created in /var/lib/mysql. The /var/lib/mysql directory is owned by mysql, with NO access allowed for others. This is OK though, in that administrative tools are provided to create, backup, and restore a database.
For a system user to access a mysql database, they needs a mysql database user name and password. From there, they can use user (non-root) commands to create, backup, restore, and delete a database.
![](/wiki/images/thumb/1/19/CPanel_Databases.jpg/300px-CPanel_Databases.jpg)
Note: Many shared hosting sites use a "CPanel" webpage interface, allowing their users to create and interact with their databases.
Create Database User
Without a "CPanel" interface, we need root access to create an initial database user. The username: Must be unique for the database Is case sensitive Doesn't necessarily require Linux username as part of the name
For this exercise, we will use: Database host: "localhost" Database user name: "db_user_name" Database user password: "db_user_pw" Database name: "db_name" Create Database User $ sudo mysql mysql> CREATE USER 'db_user_name'@'localhost' IDENTIFIED BY 'db_user_pw'; Query OK, 0 rows affected (0.02 sec) List the Database Users mysql> SELECT user FROM mysql.user; +------------------+ | user | +------------------+ | debian-sys-maint | | db_user_name | | mysql.infoschema | | mysql.session | | mysql.sys | | root | | user | +------------------+ 7 rows in set (0.00 sec) Give user 'CREATE' and 'SELECT' permissions mysql> GRANT CREATE, SELECT ON * . * TO 'db_user_name'@'localhost'; Query OK, 0 rows affected (0.01 sec) mysql> quit Bye
Database User Creates Database
$ mysql -h localhost -u db_user_name -p Enter password: db_user_pw Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 23 Server version: 8.0.37-0ubuntu0.22.04.3 (Ubuntu) Copyright (c) 2000, 2024, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> CREATE DATABASE db_name; Query OK, 1 row affected (0.00 sec) mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | db_name | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> quit Bye
Backup Database to a file
The "--no-tablespaces" option is often used for MediaWiki backup/restore $ mysqldump --no-tablespaces -h localhost -u db_user_name -p db_name | gzip > backup_db_name.sql.gz
Delete a Database
Used to test backup and restore scripts... $ mysqladmin -u db_user_name -p drop db_name
Create Wiki Database using 'mysqladmin'
$ mysqladmin -u db_user_name -p create db_name
(required before importing dump)
Restore Database from file
$ mysql -u db_user_name -p db_name < backup_db_name.sql
Bash Script to backup a database using 'mysqldump'
NOTE: Set the file permissions to 700, so other's don't see password #!/bin/bash # backup 'db_name' database to a filename containing date HOSTNAME=localhost USERID=db_user_name USERPW="db_user_pw" DBNAME=db_name FILENAME="${DBNAME}_backup_$(date +'%b_%d_%Y')" mysqldump --no-tablespaces -h $HOSTNAME -u $USERID -p$USERPW $DBNAME | gzip > $FILENAME