To install MySQL on a system, the commands depend on the operating system you’re using. Here are the installation steps for common systems:
On Ubuntu/Debian:
- Update your package index:
sudo apt-get update
- Install MySQL:
sudo apt-get install mysql-server
- Start MySQL service:
sudo systemctl start mysql
- Secure MySQL installation (set a root password and configure settings):
sudo mysql_secure_installation
- Check MySQL status to confirm it’s running:
sudo systemctl status mysql
- Install PHP and required extensions:
sudo apt install php-fpm php-mysql
On CentOS/RHEL:
- Install MySQL 8 repository:
sudo yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
- Install MySQL:
sudo yum install mysql-server
- Start MySQL service:
sudo systemctl start mysqld
- Check MySQL status:
sudo systemctl status mysqld
- Secure MySQL installation:
sudo mysql_secure_installation
- Install PHP and required extensions:
sudo yum install php php-fpm php-mysql
Install phpMyAdmin:
Step 1: Download phpMyAdmin ZIP File
- Go to the official phpMyAdmin download page:
- Visit the phpMyAdmin Downloads page.
- Download the latest stable version (ZIP):
- Choose the
.zip
version and download it. Alternatively, you can use thewget
command to download it directly on your server:
wget [Enter zip file link here]
Step 2: Extract the ZIP File
- Extract the downloaded ZIP file:
- First, navigate to the directory where you want to install phpMyAdmin. For example:
cd /var/www/html
- Then extract the ZIP file:
sudo unzip phpMyAdmin-*.zip
- Rename the folder (optional):
If you’d like to rename the folder tophpmyadmin
for easier access, run:
sudo mv phpMyAdmin-*-all-languages phpmyadmin
Step 3: Set Permissions
- Set the appropriate permissions so the web server can access phpMyAdmin:
sudo chown -R www-data:www-data /var/www/html/phpmyadmin
sudo chmod -R 755 /var/www/html/phpmyadmin
Step 4: Access phpMyAdmin
- Open your browser and visit
http://your_server_ip/phpmyadmin
. - Log in using your MySQL username and password.
Alternative (if phpMyAdmin is installed in another directory):
If phpMyAdmin is located somewhere else (for example, /usr/share/phpmyadmin
), create a symbolic link to the web root:
sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin
MySQL User Create:
Step 1: Log in to MySQL as Root
- Open a terminal and log in to MySQL as the root user:
sudo mysql -u root -p
You’ll be prompted to enter the root password.
Step 2: Create the MySQL User
- Run the following SQL command to create a new user. Replace
username
andpassword
with the desired username and password:
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
localhost
means the user can only log in from the local machine. To allow the user to log in from any IP, you can use %
instead of localhost
: CREATE USER 'username'@'%' IDENTIFIED BY 'password';
Step 3: Grant Full Permissions
- Grant the new user full permissions on a specific database (replace
database_name
with the name of your database):
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';
If you want the user to have full permissions on all databases, use the following:
GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' WITH GRANT OPTION;
The WITH GRANT OPTION
allows the user to grant privileges to others.
- Flush privileges to apply the changes:
FLUSH PRIVILEGES;
Step 4: Verify Permissions
To verify that the user has the correct permissions, you can log in as the new user:
mysql -u username -p
Then, you can check the granted permissions using the following command:
SHOW GRANTS FOR 'username'@'localhost';
Step 5: Exit MySQL
After completing the process, exit MySQL:
EXIT;
