Home>Article>Database> mysql 安装与主从配置_MySQL

mysql 安装与主从配置_MySQL

WBOY
WBOY Original
2016-06-01 13:27:06 1109browse

bitsCN.com 我是ssh远程 配置的;略微有些不同,大家自己斟酌。1.安装MySQLsudo yum install mysql mysql-server
sudo chgrp -R mysql /var/lib/mysql
sudo chmod -R 770 /var/lib/mysqlsudo service mysqld start 第一次安装设置密码:mysql_secure_installation
回车,根据提示输入Y
输入2次密码,回车
根据提示一路输入Y
最后出现:Thanks for using MySQL!

2.创建一个新的用户test在MySQL上mysql>grant all privileges on mysql .* to test@192.168.100.206 identified by 'mysql'; 这里顺便提下取消授权的命令 为revoke 再将语句中的to 换为from 达到撤销已赋权限的目的 删除用户的命令为drop user 用户名@‘%’ 以下内容为在MySQL上建立的一个test的数据库操作的3.MySQL主从配置 grant all on *.* to user1@192.168.100.205 identified by "mysql";192.168.100.205 slave(从服务器)的IP MySQL从服务器上登陆MySQL主服务器sudo mysql -uuser1 -pmysql -h192.168.100.206
4.主服务器my.cnf配置

[mysqld]
server-id=1
log-slow-queries=mysql-slow.log
log-error=mysql.err
log=mysql.log
log-bin=mysql-bin
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

从服务器my.cnf配置
[mysqld]
port=3306
server-id=2
expire_logs_day=7
log-slow-queries=mysql-slow.log
log-error=mysql.err
log=mysql.log
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql

#character-set-server = utf8

#collation-server =utf8_general_ci 此行和上一行为改变编码为UTF8 ,

#如果要改的话一定要注意所有的编码都要改
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

5.mysql服务器备份(主服务器)#mysqldump -uroot -pmysql test -l -F '/tmp/test.sql'此命令备份不用另外读锁
-F即flush logs-l 表示数据库只能进行读的操作
Tips:可以通过binlog日志恢复 ,还有flush logs 的作用很重要,也可以说flush作用很大进行操作完成前最好刷新下日志。6.MySQL的恢复:mysql -uroot -pmysql test -v -fmysql> show slave status/G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.100.206
Master_User: user1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 262
Relay_Log_File: mysqld-relay-bin.000006
Relay_Log_Pos: 407
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes #这两行YES代表成功了
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 262
Relay_Log_Space: 708
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)

以下为用到的一些命令及笔记

mysql> insert into t1 values(4),(5),(6),(7),(8);插入

为表创建索引:create index in_id on t1(id);
查看索引 show index from t1;
desc t2;查看表结构
出入一个一千万行的数据
insert into t2 values(1),(2),(3),(4);
insert into t2 select * from t2;

查看多少行
select count(*) from t2;

实时 监控命令:watch -nl ls -lh 1S刷新一次

alt+F2开启第二个终端
改变定界符:/d // 这样使用 ;就不是结束,不会开始执行
换回来 /d ;

free -m 内存使用

建立索引 create index idx_of_c3 on part_tab(c3)

默认索引文件名:ibdatal 在data目录下


mysql 表复制
mysql> create table t2 like t1;
复制表数据(表结构相同的情况下);
mysql>insert into t2 select * from t1;

我是看的这个视频http://pan.baidu.com/s/1lCASk

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn