centos下安装mysql5.7社区版

mysql分社区版和企业版,其中社区版为免费使用并且开源。
1、下载mysql5.7社区版(64位)

wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar
tar -xvf mysql-5.7.17-linux-glibc2.5-x86_64.tar
tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz

2、解压拷贝

mv mysql-5.7.17-linux-glibc2.5-x86_64 /usr/local/mysql

3、创建mysql用户

groupadd mysql
useradd -r -g mysql mysql

4、创建mysql的数据目录,该目录在初始化数据库的时候会用到

mkdir /usr/local/mysql/data /usr/local/mysql/log
chown -R mysql:mysql /usr/local/mysql

5、创建my.cnf文件

cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
vim /etc/my.cnf

[client]
port = 3306
socket = /tmp/mysql.sock

[mysqld]
server_id=1
port = 3306
user = mysql
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
pid-file = /usr/local/mysql/data/mysql.pid
max_connections = 1000
max_connect_errors = 1000
table_open_cache = 1024
max_allowed_packet = 128M
open_files_limit = 65535
#####====================================[innodb]==============================
innodb_buffer_pool_size = 1024M
innodb_file_per_table = 1
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_purge_threads = 2
innodb_flush_log_at_trx_commit = 1
innodb_log_file_size = 512M
innodb_log_files_in_group = 2
innodb_log_buffer_size = 16M
innodb_max_dirty_pages_pct = 80
innodb_lock_wait_timeout = 30
innodb_data_file_path=ibdata1:1024M:autoextend

#####====================================[log]==============================
log_error = /usr/local/mysql/log/mysql-error.log 
slow_query_log = 1
long_query_time = 1 
slow_query_log_file = /usr/local/mysql/log/mysql-slow.log

sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

6、初始化数据库

/usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql

7、修改目录权限

chown -R mysql:mysql /usr/local/mysql

8、配置启动文件

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

9、启动mysql

/etc/init.d/mysqld start

10、获取初始密码

cat ~/.mysql_secret
rm -rf ~/.mysql_secret

11、重设初始密码

/usr/local/mysql/bin/mysql -uroot -p

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '********';

mysql> grant all privileges on *.* to root@'%' identified by '123456';

mysql> flush privileges;

ps:远程连接mysql,报can’t connect to mysql server on ‘localhost’ (10060)
请检查一下iptables,默认可能禁用了3306端口

vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

重启iptables

service iptables restart

PS:

1、创建用户

CREATE USER 'username'@'host' IDENTIFIED BY 'password';

说明:
username:你将创建的用户名
host:指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost,如果想让该用户可以从任意远程主机登陆,可以使用通配符%
password:该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器

2、授权

GRANT privileges ON databasename.tablename TO 'username'@'host'

说明:
privileges:用户的操作权限,如SELECT,INSERT,UPDATE等,如果要授予所的权限则使用ALL
databasename:数据库名
tablename:表名,如果要授予该用户对所有数据库和表的相应操作权限则可用*表示,如*.*

注意:
用以上命令授权的用户不能给其它用户授权,如果想让该用户可以授权,用以下命令:

GRANT privileges ON databasename.tablename TO 'username'@'host' WITH GRANT OPTION;

3、设置与更改用户密码

SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword');

如果是当前登陆用户用

SET PASSWORD = PASSWORD("newpassword");

4、撤销用户权限

REVOKE privilege ON databasename.tablename FROM 'username'@'host';

说明:
privilege, databasename, tablename:同授权部分

注意:
假如你在给用户’pig’@’%’授权的时候是这样的(或类似的):GRANT SELECT ON test.user TO ‘pig’@’%’,则在使用REVOKE SELECT ON *.* FROM ‘pig’@’%’;命令并不能撤销该用户对test数据库中user表的SELECT 操作。相反,如果授权使用的是GRANT SELECT ON *.* TO ‘pig’@’%’;则REVOKE SELECT ON test.user FROM ‘pig’@’%’;命令也不能撤销该用户对test数据库中user表的Select权限。

具体信息可以用命令SHOW GRANTS FOR ‘pig’@’%’; 查看。

5、删除用户

DROP USER 'username'@'host';

2 Comments

Comments are closed.