CentOS 安装 Mysql

推荐用配置环境 OneinStack

安装MySQL

  1. 安装rpm包

[root@VM_0_16_centos jvm]# rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5. noarch.rpm Retrieving http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm Preparing... ################################# [100%] Updating / installing... 1:mysql-community-release-el7-5 ################################# [100%] ```

  1. 查看当前可用的mysql安装资源

[root@VM_0_16_centos jvm]# yum repolist enabled | grep "mysql.-community." mysql-connectors-community/x86_64 MySQL Connectors Community 65 mysql-tools-community/x86_64 MySQL Tools Community 69 mysql56-community/x86_64 MySQL 5.6 Community Server 412 ```

  1. 安装服务

    从上面的列表可以看出, mysql56-community/x86_64 和 MySQL 5.6 Community Server 可以使用。因此,我们就可以直接用yum方式安装了MySQL5.6版本了。
    [root@VM_0_16_centos jvm]# yum -y install mysql-community-server
    Loaded plugins: fastestmirror, langpacks
    Loading mirror speeds from cached hostfile
    Resolving Dependencies
    --> Running transaction check
    --> .............
    Replaced:
      mariadb-libs.x86_64 1:5.5.56-2.el7          
    
    Complete!
    
  2. MySQL安装完成后,进行相关配置

#安装成功后,将其加入开机启动
    [root@VM_0_16_centos jvm]# systemctl enable mysqld
    #启动mysql服务进程
    [root@VM_0_16_centos jvm]# systemctl start mysqld
    #配置mysql(设置密码)等
    [root@VM_0_16_centos jvm]# mysql_secure_installation
    
    Enter current password for root (enter for none): 
    OK, successfully used password, moving on...
    
    Setting the root password ensures that nobody can log into the MySQL
    root user without the proper authorisation.
    
    Set root password? [Y/n] y  #[设置root用户密码]
    New password: 
    Re-enter new password: 
    Password updated successfully!
    Reloading privilege tables..
     ... Success!
    
    By default, a MySQL installation has an anonymous user, allowing anyone
    to log into MySQL without having to have a user account created for
    them.  This is intended only for testing, and to make the installation
    go a bit smoother.  You should remove them before moving into a
    production environment.
    
    Remove anonymous users? [Y/n] y  #[删除匿名用户]
     ... Success!
    
    Normally, root should only be allowed to connect from 'localhost'.  This
    ensures that someone cannot guess at the root password from the network.
    
    Disallow root login remotely? [Y/n] y  #[禁止root远程登录]
     ... skipping.
    
    By default, MySQL comes with a database named 'test' that anyone can
    access.  This is also intended only for testing, and should be removed
    before moving into a production environment.
    
    Remove test database and access to it? [Y/n] y  #[删除test数据库]
     - Dropping test database...
    ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
     ... Failed!  Not critical, keep moving...
     - Removing privileges on test database...
     ... Success!
    
    Reloading the privilege tables will ensure that all changes made so far
    will take effect immediately.
    
    Reload privilege tables now? [Y/n] y  #[刷新权限]
     ... Success!
	 
    All done!  If you've completed all of the above steps, your MySQL
    installation should now be secure.
    
    Thanks for using MySQL!
	
    Cleaning up...
  1. 安装完成后其它命令

    	1:安装完成路径:
    
    		1、数据库目录
    		/var/lib/mysql/
    		2、配置文件
    		/usr/share/mysql(mysql.server命令及配置文件)
    		3、启动脚本
    		/etc/rc.d/init.d/(启动脚本文件mysql的目录) 
    		4、相关命令
    		/usr/bin(mysqladmin mysqldump等命令)
    		注:1~3安装server安装后存在,4mysqladmin mysqldump在client安装后存在
    
    		b、停止
    		service mysqld stop
    
    		c、重启
    		service mysqld restart 
    		y
    		d, 启动
    		service mysqld start
    
    		e, 强行关闭MySQL
    		killall mysqld
    
    		f, 查看是否启动成功,进程mysql启动,网络端口3306开启为ok.
    		ps aux | grep mysql
    
    		g, 登录
    		mysql -u root -p
    
    		f, 查看MySQL中all user
    		SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
    
  2. 设置MySQL允许远程机器用root用户连接MySQL服务器数据库

    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'youpassword' WITH GRANT OPTION;
    FLUSH PRIVILEGES;
    或
    UPDATE USER SET HOST = '%' WHERE HOST='127.0.0.1' AND USER='root'
    
    	[root@VM_0_16_centos ~]# mysql -u root -p mysql
    	Enter password: 
    	Reading table information for completion of table and column names
    	You can turn off this feature to get a quicker startup with -A
    
    	Welcome to the MySQL monitor.  Commands end with ; or \g.
    	Your MySQL connection id is 20
    	Server version: 5.6.41 MySQL Community Server (GPL)
    
    	Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
    
    	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.
    	#[youpassword是你要设置远程root的密码]
    	mysql> grant all privileges on *.* to 'root'@'%' identified by 'youpassword' with grant option; 
    	Query OK, 0 rows affected (0.00 sec)
    
    	mysql> flush privileges;
    	Query OK, 0 rows affected (0.00 sec)
    
    	mysql> exit; 
    	Bye
    
    
  3. 开放防火墙端口

    iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT;
    iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT;
    iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT;
    iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT;
    iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT;
    iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT;
    service iptables save;
    

参考文章:

爱音乐的孩子是小白-[CentOS] MySQL删除和安装