1. Check whether MySQL is installed
Check whether the current system has installed mysql
Before executing the installation command, execute the query command first
1. CentOS6
rpm -qa|grep mysql
If there is an old version package of mysql-libs as follows:
Please execute the uninstall command first:rpm -e --nodeps mysql-libs
2. CentOS7
Execute the query first:rpm -qa|grep mariadb
If mariadb exists delete:rpm -e --nodeps mariadb-libs
2. Install MySQL
1. Send the compressed package to the server
I am using mysql-5.7.16 here, if you need other versions, you can download it yourself
Here is the compressed package of mysql-5.7.16:
Link: https://pan.baidu.com/s/1zNmv-0Io2BQmp0e3go72Gg
Extraction code: wjud
2. Unzip the file
tar -zxv mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar.gz
3. Installation (executed in order)
rpm -ivh mysql-community-common-5.7.16-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.16-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.16-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.16-1.el7.x86_64.rpm
Check the installed version:mysqladmin --version
View mysql users:id mysql
4. MySQL service initialization
Execute the following command to initialize:
mysqld --initialize --user=mysql
Use the mysql system user to generate a password for the root user in safe mode and mark the password as expired. After logging in, you need to set a new password
View password:cat /var/log/mysqld.log
5. Modify the initialization password
To modify MySQL, the MySQL service must first be started, and the password can only be changed after logging in with the initial password:
Start the MySQL service:systemctl start mysqld.service
Close the MySQL service:systemctl stop mysqld.service
change Password:ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
6. MySQL autostart
Check whether mysql is self-starting ( default self-starting )
systemctl list-unit-files|grep mysqld.service
If it is not enabled, you can run the following command to set self-starting
systemctl enable mysqld.service
7. Modify the character set problem
Test the use of mysql:
There is an encoding problem, the reason: check the mysql encoding ( show variables like '%character%';
), and find that the default encoding is latin1
To modify the encoding, you need to modify the mysql configuration file:vi /etc/my.cnf
Add a line of encoding configuration under mysqld: character_set_server=utf8
save and exit, restart the mysql service:systemctl restart mysqld
How does the character set of the generated library table change?
Modify the character set of the database:alter database mydb character set 'utf8';
Modify the character set of the data table:alter table mytbl convert to character set 'utf8';
8. Remote access to MySQL
Next, use the host navicat client to access the MySQL service, and report the following error: ( make sure the firewall is turned off )
Reason: By default, mysql does not allow remote connections. Only allow localhost connections
implement:select host,user,authentication_string,select_priv,insert_priv from mysql.user;
solution:
- Create a new user (omitted here)
- Modify root user access rights
9. Grant root remote access
Authorization command:
Grant authority 1, authority 2,... authority n on database name. table name to user name@user address identified by 'connection password';
If the user is found to have no such permission, a new user will be created directly.
for example
grant all privileges on *.* to root@'%' identified by 'root'; #授予通过网络方式登录的的root用户,对所有库所有表的全部权限,密码设为root.
extension:
View current user permissions:show grants;
View the global permissions of the current user:select * from user ;
View the permissions of a table for a user:select * from tables_priv;
Command to revoke permissions:
revoke authority 1, authority 2, ... authority n on database name. table name from user name@user address;
To revoke all permissions of the whole database and tables:REVOKE ALL PRIVILEGES ON mysql.* FROM li4@localhost;
Take back the permission to insert, delete, modify and query all tables under the mysql library:REVOKE select,insert,update,delete ON mysql.* FROM li4@localhost;
The user must log in again to take effect
10. Test connection
Connection test: