MySQL5.7在安装完后,第一次启动时,会在root目录下生产一个随机的密码,文件名为
.mysql_secret
登录时需要用随机密码登录,然后通过以下命令修改密码
- SET PASSWORD = PASSWORD('new password');
************************************************************************
多实例
- mysql -uroot -P3307 -S /home/data/mysql/3307/data/mysql.sock -p
- SET PASSWORD = PASSWORD('E*8i58kEuspRNJZPyXdNy2mQ');
创建用户
- create user [email protected] identified by 'E*8i58kEuspRNJZPyXdNy2mQ';
- GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' WITH GRANT OPTION;
- create user [email protected] identified by 'root';
- GRANT PROCESS ON *.* TO 'zabbix'@'127.0.0.1' identified BY 'zabbix';
- GRANT PROCESS ON *.* TO 'zabbix_monitor'@'localhost' identified BY 'root';
一、创建一个用户
- create user xxxxx@localhost identified by 'xxxxxxx';
二、开启mysql远程连接
进入mysql
- mysql -u root -p mysql # 第1个mysql是执行命令,-u 用户名 -p root密码
在mysql控制台执行:
- grant all privileges on *.* to 'test'@'%' identified by 'xxxxxxx' with grant option; # 下面会详细介绍grant 命令
- # test是用户名,%代表任意主机,'xxxxxxx'指定的登录密码(这个和本地的root密码可以设置不同的,互不影响)
- flush privileges;# 重载系统权限
允许3306端口
- iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 3306-j ACCEPT
- # 查看规则是否生效
- iptables -L -n # 或者: service iptables status
- # 此时生产环境是不安全的,远程管理之后应该关闭端口,删除之前添加的规则
- 上面iptables添加/删除规则都是临时的,如果需要重启后也生效,需要保存修改:
- service iptables save # 或者: /etc/init.d/iptables save
- vi /etc/sysconfig/iptables # 加上下面这行规则也是可以的
- -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
- /usr/bin/mysqladmin -uroot password "xxxxxxxx'
- /usr/bin/mysql_secure_installation
三、创建用户权限
- grant select on dmc_db.* to admin; 赋予用户查询权限 dmc_db是库名称 * 可以指定表的权限 admin 是用户 这句户就是说给 admin这个用户赋予 dmc_db 这个库所有的查询权限
- grant update on dmc_db.* to admin; 赋予用户更新权限
- grant insert on dmc_db.* to admin; 赋予用户插入权限
- grant delete on dmc_db.* to admin; 赋予用户删除权限
- grant select,update,insert,delete on dmc_db.* to admin; 一次性赋予增删改查权限
- grant select,update,insert,delete on dmc_db.* to 'test'@'%' identified by 'xxxxxxx'; # 这句户是指 开启远程主机 并且指定密码
四、 回收远程主机的权限
- revoke all privileges on *.* from test;
- update user set authentication_string=password('123qwe') where user='root' and Host = 'localhost'; # mysql5.7 修改root密码
更改密码
- update mysql.user set authentication_string=password('asdf123123') where user='zabbix' and Host = 'localhost';
- flush privileges;
一份 my.cnf 的配置
- [client]
- port=3306
- socket = /var/lib/mysql/mysql.sock
- default-character-set=utf8
- [mysqld]
- ########basic settings########
- datadir=/var/lib/mysql
- socket=/var/lib/mysql/mysql.sock
- # Disabling symbolic-links is recommended to prevent assorted security risks
- symbolic-links=0
- log-error=/var/log/mysqld.log
- pid-file=/var/run/mysqld/mysqld.pid
- server-id = 20160907
- bind_address = 0.0.0.0
- autocommit = 0
- skip_name_resolve = 1
- max_connections = 800
- max_connect_errors = 1000
- transaction_isolation = READ-COMMITTED
- explicit_defaults_for_timestamp = 1
- join_buffer_size = 134217728
- tmp_table_size = 67108864
- tmpdir = /tmp
- max_allowed_packet = 16777216
- sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"
- interactive_timeout = 1800
- wait_timeout = 1800
- read_buffer_size = 16777216
- read_rnd_buffer_size = 33554432
- sort_buffer_size = 33554432
- ########log settings########
- log-error = /home/data/mysql/3307/data/error.log
- slow_query_log = 1
- slow_query_log_file = /home/data/mysql/3307/data/slow.log
- log_queries_not_using_indexes = 1
- log_slow_admin_statements = 1
- log_slow_slave_statements = 1
- log_throttle_queries_not_using_indexes = 10
- expire_logs_days = 90
- long_query_time = 2
- min_examined_row_limit = 100
- ########replication settings########
- master_info_repository = TABLE
- relay_log_info_repository = TABLE
- log_bin = mysql-bin.log
- binlog_format = row
- sync_binlog = 1
- gtid_mode = on
- enforce_gtid_consistency = 1
- log_slave_updates
- relay_log = relay.log
- relay_log_recovery = 1
- binlog_gtid_simple_recovery = 1
- slave_skip_errors = ddl_exist_errors
- ########innodb settings########
- [mysqld-5.7]
- innodb_buffer_pool_dump_pct = 40
- innodb_page_cleaners = 4
- innodb_undo_log_truncate = 1
- innodb_max_undo_log_size = 2G
- innodb_purge_rseg_truncate_frequency = 128
没有评论